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

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.

Leave a Comment