Read input value from dynamic created input controls

  • Thread starter Thread starter Melissa
  • Start date Start date
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());
}
}
 
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
---------------------------
 
Back
Top