Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How can access HTML controls in ASP.NET code behind?

You can access them from code behind by adding runat="server" on the html elements.

How get HTML TextBox value in ASP.NET code behind?

You can access the value of the control using Request object. Request["TextBoxID"] should give you the text in the textbox. Other option is - store the TextBox value in a Hidden Field onblur of the TextBox. Then access this hidden field value on the server.

What does runat server mean?

The runat="server" tag in ASP.NET allows the ability to convert/treat most any HTML element as a server-side control that you can manipulate via code at generation time. Some controls have explicit implementations, others simply revert to a generic control implementation.


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.


The simplest way IMO is to include an ID and runat server tag on all your elements:

<div id="MYDIV" runat="server" />

Since it sounds like these are dynamically inserted controls, you might appreciate FindControl().


Since you're using ASP.NET code-behind, add an id to the element and runat=server.

You can then reference the objects in the code behind.