Handling Arrays of HTML Input Elements with Request.Form Like PHP - asp.net

How can I properly receive these Array of Inputs on asp.net?
<input type=hidden name='field[name][]' value='alex' />
<input type=hidden name='field[name][]' value='mark' />
<input type=hidden name='field[name][]' value='helen' />
<input type=hidden name='field[age][]' value='22' />
<input type=hidden name='field[age][]' value='30' />
<input type=hidden name='field[age][]' value='29' />
In php you can access field by $field = $_POST["field"]
$field["name"] and $field["age"] are simply arrays containing names and ages.

actually, it does exist with asp.net. - you can use
string[] MyTest = Request.Form.GetValues("MyTest");
or
string[] MyTest = Request.QueryString.GetValues("MyTest");

You can use a standard string split - this is all php does for you behind the scenes. PHP is not a strongly typed language however which means that it is a LOT easier for them to provide the "appearance" of this functionality.
The reality is that php is just allowing you to provide comma delimited input and it automatically splits it for you.
So... if you want to do this in asp.net, you can use the same input name a number of times and it will be returned with the request object as a comma delimited list. I would not recommend using this approach for user entered input, but it will work fine if you are contrrolling the input like combining a list into a hidden input from a jquery script.
To get your head around what is happening (with php too.. all web dev techs are using the same http rules), just try posting a form with an input (don't set runat server) that is there twice.
<input type="text" name="MyTest" value="MyVal1" />
<input type="text" name="MyTest" value="MyVal2" />
On pageload add this
if(IsPostBack)
{
Response.Write(Request["MyTest"]);
}
You should see
MyVal1,MyVal2
On the screen.
Now, if you want this into an array, you can:
string[] myvals = Request["MyTest"].Split(',');
if you want Integers or other datatypes (php doesn't know/care what a datatype is really), you will have to loop through it and parse it into another array/generic list.
I don't know what your wanting as an end result, but understanding what the browser posts back is the first step and the short answer is...
Yes, ASP.NET can do this to (just a little more manually).

These are not arrays of inputs. HTML doesn't have a concept of arrays.
These are simply groups of inputs with the same name.
What are you trying to achieve? Why are you not using server side controls?
Update:
You can access the Request.Forms collection - it will hold the posted form elements.
Alternatively, use server side controls - you will be able to access these by ID.

Related

Request.Form without Values

i have a problem
I have an A form with one with a hidden field that redirects to the other in a button.
In form B, i mus get the value of this hidden field by POST.
But when I do the Request.Form("Hidden_Field"), it only brings me the name of the field, when what I need is the VALUE.
any ideas?.
here is the code:
Form A:
<asp:HiddenField ID="SIGNSYS_OUTPUT" runat="server" Value="123" />
Server.Transfer("~/VerifyAccountBGBAResult.aspx", True)
Form B:
Me.Value = Request.Form("SIGNSYS_OUTPUT")
This Request returns me in the value "SIGNSYS_OUTPUT".
Server.Transfer does not transfer the information unless the form got posted first, I'll assume this is the case.
Request.Form gets the information based on the name of the control, example.
<input type="hidden" name="SIGNSYS_OUTPUT" value = "123" />
When you use runat="server" the name is generated by .net. You'll need to do a viewsource to get the proper name. It might end up being something like this.
Request.Form("ctl00$ContentPlaceHolder$SIGNSYS_OUTPUT")
In your case, I think using Server.Transfer might not be the best solution. You could look at other solution like using a session, the database, process on the page and send to result to the other page, ect..

ASP.NET Regular Expression - Dont allow website url

I am using ASP.net 4.5 with bootstrap.
Below is the code I have to allow only certain characters.
How can I change this to allow the characters a-zA-Z0-9_. /,&|()!- but not allow the user to put in http or www.
<input type="text" class="form-control" name="subject" id="subject" placeholder="Enter subject"
data-bv-message="The subject is not valid."
data-bv-notempty="true" data-bv-notempty-message="The subject is required and cannot be empty."
data-bv-regexp="true" data-bv-regexp-regexp="^[a-zA-Z0-9_. /,&|()!-]+$" data-bv-regexp-message="The subject can only consist of alphabetical, number, dot and underscore."
/>
I would say you might have to add groups for the words you don't want to have in your input value.
((!|www)|(!|http))
You can also take a look at this answer to a similar question, there you should get enough input to fix your expression

Can validate and converter be combined in jsViews data-link?

I can ensure data exists in an input element by including validate and required in jsViews' data-link attribute like this:
<input type="text" data-link="{validate activityCode required=true}">
And by following Boris Moore's example I can ensure the data returned to the model is cast as an integer instead of input's default type of string:
$.views.converters({
toInt: function(value) {
return parseInt(value); // simple example, without error checking
}
});
<input type="text" data-link="{:activityCode:toInt}">
Where I'm having a problem is combining both validation and converter. Nothing binds to the input element using this:
<input type="text" data-link="{validate activityCode:toInt required=true}">
Does anyone know of syntax that allows both validation and convertBack functionality to exist in the same data-link attribute?
You can use the syntax convert=... convertBack=... on any tag.
See two-way binding - convert and convertBack and using converters with other tags. (The second docs reference is for JsRender, so concerns convert only. But if using JsViews data-linking, then convertBack=... works in just the same way.)
Search for "convertBack=" and you'll find some examples, including this with radiogroup and this one with validate.

Classic ASP: How to see list of parameters passed from form submission?

I'm coming from Rails so I'm a little lost since this is my first time touching ASP.
I'm working on a legacy ASP app and I have a form in classic ASP and there are two inputs. How would I view and use the parameters so that I can use them to update my records? Are they stored in some sort of variable by default? And do I do this on the page of the form or the page after?
My input:
<input class="textboxsm" type="text" onkeypress="return numbersonly(window.event.keyCode,1)" onblur="poswarnings(1);updateTotals();" onfocus="rowfocus=0" value="2" maxlength="4" size="2" name="ia1" style="text-align:right;">
And this is the button that submits the form:
<input width="116" type="image" height="70" onmouseout="this.src='art/order_continue.gif'" onmouseup="this.src='art/order_continue.gif'" onmousedown="this.src='art/down_order_continue.gif'" onclick="return orderdone()" name="submitorder" alt="Done" src="art/order_continue.gif">
So how would I extract the value from my input?
Values sent via POST are stored in the Request.Forms collection. Items passed via query string are in the Request.QueryString collection.
Depending on your setup, you can access the values a number of different ways. Most commonly, people know what form fields to expect, so if you have:
<input type="text" name="Title" maxlength="200" size="90" />
Assuming your form method is POST, you would retrieve it on the page the form is posted to by:
strTitle = Request.Form("Title")
This does assume you have already defined strTitle, and the value is not null/empty/etc. and/or that you are checking for that later on...
You're pulling the value of the form item named "Title" from the Request.Form collection and assigning it to the variable strTitle (which should have been defined earlier) From there you can do whatever validation you need to do.
The only thing that would change if you were sending the request via GET instead of POST is you would use the Request.QueryString collection - like so:
strTitle = Request.QueryString("Title") 'Same assumption as before...

passing querystring parameters

what is the best way of passing querystring to another page, i would definitly avoid like using.......
<a href="Page2.aspx?WONumber=12345">
is there any better way?
i was thinking create a Baseclass with few prop and update the baseclass?
thanks.
It sounds like you want to take the querystring argument, and use it in subsequent pages.
If it's not desirable to pass-forward this querystring argument from your current page, perhaps it's called page1.aspx, without using another querystring parameter, you could:
store the value in Session. Consider Session["WoNumber"] = Request.QueryString["WONumber"].ToString();
store the value in Cookies. You could use something like: Response.Cookies["WoNumber"].Value = Request.QueryString["WONumber"].ToString();
It really depends on where you're getting the value from. You can build a URL using UriBuilder or if it's simple enough string concatenation could be OK (though you'd have to make sure to Server.UrlEncode the values).
If the value is a constant, as your example implies, then there is nothing wrong with putting it directly into a query string, although I would still use a proper named constant, eg.
<a href="Page2.aspx?WONumber=<%= TheMagicOrderNumber %>
with the constant defined in the code-behind:
protected const int TheMagicOrderNumber = 12345
If your objection is the maintainability of "magic string" URLS, and you'd be prepared to use a button instead of an anchor, you could do worse than
<form method="GET" action="Page2.aspx">
<input type="hidden" name="WONumber" value="12345" />
<input type="submit" value="Navigate" />
</form>
this method will generalise to a query string of any complexity with any number of parameters.
There is a great article I came across a few months ago when I was looking for enhanced security with querystrings...
http://devcity.net/Articles/47/1/encrypt_querystring.aspx
It's a very good article, and has a bonus the author offers code examples in C# and VB.NET.
There are times when I prefer to use querystrings over sessions... small number of session objects is ok, but too many and it starts to become a bit tedious to debug problems.
You can encrypt a querystring parameter, if security is your concern.
Or you can use other holders, such s p.cambell says above (session & cookie).
You could also store it in a database, and have the page you go to retrieve it onload.
Just depends on your application requirements.
Another thing I've done is to use <asp:panel>, basically using a single page as though it were multiple pages. In this way, I also have access to viewstate to hold my variables. (Whenever a user clicks 'next', or whatever they would click to goto the next page, I simply hide the panel they're on, and show the panel they want to go to [visible = true/false] property)

Resources