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

I have a basic HTML form that gets inserted into a server side div tag based on how many records exist in the database. This HTML form comes out just fine, and everything looks good. But on my action page I cannot seem to access the input elements from the code behind. I have tried using the Request scope, but I have come up empty on that approach. Any other suggestions?

All of the below suggestions are great, and normally that is what I would do. But these forms are being built on the fly after the page is being compiled, so runat='server' did not do anything for me. It just passed that along to the HTML page.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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;

Method 3

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.

Method 4

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().

Method 5

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.

Method 6

You say you’re creating a form dynamically – do you really mean a <form> and its contents, because ASP.NET takes issue with multiple forms on a page and it’s already creating one überform for you?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x