tags:

views:

717

answers:

4

I have added some dynamic textboxes upon a button click. I need the values in the click event of another button click. How can I achieve that?

A: 

You need to save a reference to those textboxes, so you can access them inside your button click handle method.

Enreeco
+1  A: 

Have you tried using the FindControl(string id) method on the ParentControl (for example, a panel) to find them?

protected void btnDoStuff_click(Object sender, EventArgs args)
{
   TextBox txtBox = (TextBox) pnlDynamicButtons.FindControl("txtBox1");
}
Kirschstein
I tried it. But it is not able to find the textboxes using FindControl
Roshan
+2  A: 

First of all, when you are adding dynamic controls, you'll have to add them at every postback. Then and then only, if you have the ID of the control you added, you can find it using the FindControl method as described by Kirschstein.

[Edit] Roshan, you have to add the controls dynamically (ideally in the Init event) on every postback. Provide them an ID. This will be the ID using which you'll be able to access them everytime. You can add the controls to any container control (asp:Panel, div with runat="server" attribute, etc.) [/Edit]

Kirtan
How can I do that?
Roshan
A: 

While adding dynamic controls, you must add them during each postback, you could save reference to them while creating them by adding txtbox id to List for example.

You could use that List later to get dynamically created textboxes and retrieve their values.

for (int i = 0; i < listTextBox.Count; i++)
{
     TextBox txt = ((TextBox)(listTextBox[i]));
}

If you need to get their values before postback, while creating them add an attribute to mark them as dynamically created

txtBox.Attributes.Add("isDynamic", "Y");

then before postback, loop through input control to get them.

var inputControls = document.getElementsByTagName("input");
for(var i=0 ; i<inputControls.length ; i++)
{
    if ( inputControls[i].getAttribute("isDynamic") == "Y" ) 
    {
       ...
    }
}
Ahmed