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 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.
Edit: thought of something else.
You say you're creating a form dynamically - do you really mean a <form> and its contents 'cause asp.net takes issue with multiple forms on a page and it's already creating one uberform for you.
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;
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().
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.
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().
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.
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.
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.
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.
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.
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;
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.
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.
Edit: thought of something else.
You say you're creating a form dynamically - do you really mean a <form> and its contents 'cause asp.net takes issue with multiple forms on a page and it's already creating one uberform for you.
Edit: thought of something else.
You say you're creating a form dynamically - do you really mean a <form> and its contents 'cause asp.net takes issue with multiple forms on a page and it's already creating one uberform for you.
Source: Stackoverflow.com