I have some code which creates a series of dynamic controls, this code runs in the page_Load event and works fine.... controls get created and values persist durign post back.
I am now trying to access the values of some of those controls... and it's working in 1 instance but not in another...
Here's my code
Int32 i = 0;
Int32 fibCount;
Boolean Complex = false;
foreach (Control p in this.Panel_VariableData.Controls)
{
fibCount = 0;
if (((TextBox)p.FindControl("Textbox_Section_" + Convert.ToString(i))).Text=="Fib")
{
foreach (TextBox c in p.Controls.OfType<TextBox>())
{
{
if (c.ID.Substring(0, "Textbox_Question".Length) == "Textbox_Question")
{
if (c.Text.Length > 0)
{
fibCount++;
}
}
}
}
}
if (fibCount > 1)
{
Complex = true;
break;
}
i++;
}
This section works fine :
if (((TextBox)p.FindControl("Textbox_Section_" + Convert.ToString(i))).Text=="Fib")
the Text value of the controls returns as expected and when it equals "Fib" the if statements code block is entered - Note that the text box that it is accessing is a dynamic control
This section though
if (c.Text.Length > 0)
{
fibCount++;
}
the Text value of the control is never picked up despite the fact that I know the control has text in it and that on post back the value persists.
So why does it work for one control and not another?
Thanks Danny
Additional information
I just tested this
String s = Request.Form["ctl00$ContentPlaceHolder1$Textbox_Question_7_1"];
In order for you to access dynamically created controls, you need to recreate for each subsequent request. That includes the LinkButton control, as the Command event can't be executed unless the control exists and has the event handler assigned to
the event. Furthermore, the text box needs to be created before the LoadViewState method is called, so that the text property can be updated, which means that it needs to be created in Page_PreInit or Page_Init.
Danny Seager
Member
13 Points
36 Posts
Accessing the text of dynamic text boxes in page load
Feb 28, 2013 10:48 AM|LINK
I have some code which creates a series of dynamic controls, this code runs in the page_Load event and works fine.... controls get created and values persist durign post back.
I am now trying to access the values of some of those controls... and it's working in 1 instance but not in another...
Here's my code
Int32 i = 0; Int32 fibCount; Boolean Complex = false; foreach (Control p in this.Panel_VariableData.Controls) { fibCount = 0; if (((TextBox)p.FindControl("Textbox_Section_" + Convert.ToString(i))).Text=="Fib") { foreach (TextBox c in p.Controls.OfType<TextBox>()) { { if (c.ID.Substring(0, "Textbox_Question".Length) == "Textbox_Question") { if (c.Text.Length > 0) { fibCount++; } } } } } if (fibCount > 1) { Complex = true; break; } i++; }This section works fine :
if (((TextBox)p.FindControl("Textbox_Section_" + Convert.ToString(i))).Text=="Fib")the Text value of the controls returns as expected and when it equals "Fib" the if statements code block is entered - Note that the text box that it is accessing is a dynamic control
This section though
if (c.Text.Length > 0) { fibCount++; }the Text value of the control is never picked up despite the fact that I know the control has text in it and that on post back the value persists.
So why does it work for one control and not another?
Thanks
Danny
Additional information
I just tested this
and this returns the value correctly....
gopalanmani
Star
7828 Points
1321 Posts
Re: Accessing the text of dynamic text boxes in page load
May 09, 2013 05:10 AM|LINK
Hi,
In order for you to access dynamically created controls, you need to recreate for each subsequent request. That includes the LinkButton control, as the Command event can't be executed unless the control exists and has the event handler assigned to the event. Furthermore, the text box needs to be created before the LoadViewState method is called, so that the text property can be updated, which means that it needs to be created in Page_PreInit or Page_Init.
Gopalan Mani
My Tech blog
gopalanmani
Star
7828 Points
1321 Posts
Re: Accessing the text of dynamic text boxes in page load
May 09, 2013 05:11 AM|LINK
Gopalan Mani
My Tech blog