ASP.NET : Reading dynamically generated HTML element value in codebehind

I have an asp.net page where i have the below markup.Basically this markup is generated from codebehind by reading records from a table and looping thru them.For each record in table,there will be a div block.

Basically this form is to read/show settings for a user.The settings entries are stored in a table.

<div id='divContainer' runat='server'>

 <div id='div1' runat='server'>
    <table>
      <tr>
        <th>Name</th>
        <td><input type='text' id='txtName1' value='something' /></td>
      </tr>
       </table>
 </div>
 <div id='div2' runat='server'>
    <table>
      <tr>
        <th>Domain name</th>
        <td><input type='text' id='txtName2' value='something' /></td>
      </tr>
     </table>
 </div>
 <div id='div3' runat='server'>
    <table>
      <tr>
        <th>URL</th>
        <td><input type='text' id='txtName3' value='something' /></td>
      </tr>
      </table>
 </div>
 <div id='div4' runat='server'>
    <table>
      <tr>
        <th>Some other value is enabled ?</th>
        <td><input type='checkbox' id='chk4'  /></td>
      </tr>
      </table>
 </div>

</div>

The id's of each input element will be unique. Now in codebehind i want to read the values of each input elment to save the changes user made.How can i read the elements here ? Since the mark up is generated in codebehind as a string and appended the the INNER HTML of the external div,I cant read values like we do for a control which we drag and drop in the IDE.

Any thoughts ? Thanks in advance

Answers


If these are being sent back to the page in a standard HTTP POST then you can read the values in the Request.Form NameValueCollection.

Essentially, all of the server controls that become form elements get translated into standard HTML form elements just as you have there, albeit with more markup generated by .NET to help it identify them. Then it automatically maps their values back to the server controls for you on the postback, but the values themselves are still just in a standard HTTP POST, which you can access manually.

(This is also a common method used when posting a form from one ASP .NET application to another.)


So the brunt of the story is that when you dynamically add a control after Page_Init then POSTBACK values can not be inserted back into those controls.

CF: http://www.15seconds.com/issue/020102.htm and http://msdn.microsoft.com/en-us/library/ms178472.aspx

Some of the other answers here suggest "oh, add a runat=server to the control" but when you create it in the codebehind, and not in the Page_Init, then that makes ZERO difference.

Let me know if that's not how you're creating the controls or if that's not how you're using them and I'll revise this answer on more details. It really all boils down to how you're trying to access the values.


Generally, you'd place those input controls you're creating dynamically (in this case, a TextBox) inside something like a panel control (the container). Then after the user has posted their data, you'd loop that container panel.Controls collection and retrieve each TextBox text.

Be aware that some caveats apply when working with dynamically created controls because ASP is of stateless nature.

This page shows how to implement this:

Adding Dynamic Rows in ASP.Net GridView Control with TextBoxes


If you want to grab your values for the generated controls you have to do 2 things.

  1. Generate the input controls with a runat='server' tag for each control (otherwise they will not be included in the Request.Forms collection.) This is probably the step your missing.

    <input type='text' id='txtName1' runat='server' value='something' />

  2. Grab your values from the Request.Form collection on postback

    string txtValue1 = Request.Form["txtName1"];

It really should be that easy. I tested this against your code using a DIV as the container and a simple javascript to inject the control string into the innerHTML. If your getting any issues you may have to debug and see if the dynamic control ID has changed due to inserting them into naming container or something.


I didn't test it but I can suggest that:

Add your dynamic controls with runat="server" tag inside another controls with runat="server"(such as panel control). Then you can access them like this:

Textbox t = (Textbox)panel1.controls.findControl("dynamicControlId");

Need Your Help

Defining constructors in a class that uses pointers (C++)

c++ class pointers

Okay, so this is my first question on this site, and I'm fairly new to C++. I am trying to create a class of functions that puts the members of various campus clubs into their respective clubs. I was

Reducing Dimension For SVM in Sensor Network

machine-learning neural-network svm dimensionality-reduction

I am looking for some suggestions on a problem that I am currently facing.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.