How to access HTML form input from ASP.NET code behind [closed] How to access HTML form input from ASP.NET code behind [closed] asp.net asp.net

How to access HTML form input from ASP.NET code behind [closed]


If you are accessing a plain HTML form, it has to be submitted to the server via a submit button (or via JavaScript post). This usually means that your form definition will look like this (I'm going off of memory - make sure you check the HTML elements are correct):

<form method="POST" action="page.aspx">    <input id="customerName" name="customerName" type="Text" />    <input id="customerPhone" name="customerPhone" type="Text" />    <input value="Save" type="Submit" /></form>

You should be able to access the customerName and customerPhone data like this:

string n = String.Format("{0}", Request.Form["customerName"]);

If you have method="GET" in the form (not recommended - it messes up your URL space), you will have to access the form data like this:

string n = String.Format("{0}", Request.QueryString["customerName"]);

This of course will only work if the form was 'Posted', 'Submitted', or done via a 'Postback' (i.e., somebody clicked the 'Save' button, or this was done programmatically via JavaScript).

Also, keep in mind that accessing these elements in this manner can only be done when you are not using server controls (i.e., runat="server"). With server controls, the id and name are different.


What I'm guessing is that you need to set those input elements to runat="server".

So you won't be able to access the control

<input type="text" name="email" id="myTextBox" />

But you'll be able to work with

<input type="text" name="email" id="myTextBox" runat="server" />

And read from it by using

string myStringFromTheInput = myTextBox.Value;


It should normally be done with Request.Form["elementName"].

For example, if you have <input type="text" name="email" /> then you can use Request.Form["email"] to access its value.