Read input value from dynamic created input controls

M

Melissa

Hi all,

I need help on reading input value from dynamic created input controls. What
is the control ID to use after input is posted back to the server? My code
below does not get anything although the input values were displayed
correctly in the Textbox on the screen after posted back.

Thanks in advanced!

Here is the sample code:

if (!IsPostBack)
{
TextBox oTextBox;
for (int i=0; i<5; i++)
{
oTextBox = new TextBox();
oTextBox.ID = "test" + i.ToString();
Form.Contronls.Add(oTextBox);
}
}
else
{
for (int i=0; i<5; i++)
{
// Request Does NOT find these fields???
Response.Write(Request["test" + i.ToString()].ToString());
}
}
 
G

Gaurav Vaish \(a.k.a. MasterGaurav\)

Hi Melissa,
I need help on reading input value from dynamic created input controls.
What
is the control ID to use after input is posted back to the server? My code
below does not get anything although the input values were displayed
correctly in the Textbox on the screen after posted back.

Thanks in advanced!

Here is the sample code:

if (!IsPostBack)
{
TextBox oTextBox;
for (int i=0; i<5; i++)
{
oTextBox = new TextBox();
oTextBox.ID = "test" + i.ToString();
Form.Contronls.Add(oTextBox);
}
}


You should not add the controls like this.
Create a class-level variable of type TextBox and override the method
CreateChildControls()

Example:

public class MyPageOrCustomControl
{
private System.Collections.Generic.List<TextBox> tboxes = new
System.Collections.Generic.List<TextBox>();

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
DoMyWork();
}

private void DoMyWork()
{
foreach(TextBox tb in tboxes)
{
Response.Write(tb.ID);
}
}

protected override void CreateChildControls()
{
for(int i = 0; i < 5; i++)
{
tbox = new TextBox();
tbox.ID = "test" + i.toString();
Controls.Add(tbox);
}
}

}


--
Happy Hacking,
Gaurav Vaish
http://blogs.mastergaurav.com
http://eduzine.edujini-labs.com
---------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top