problem finding control and getting its values for dynamically created textboxes
-
Hi, I have a situation where I need to show data from database to textboxes. These textboxes has to be dynamically created and can be editable. I am able to create textboxes as follows:
string[] myData = liD1.ToArray(); //liD1 is a list that has values from database
public void showData()
{
for (int i = 0; i < myData.Length; i++)
{
if (txtMore.FindControl("txtD1StartTime" + i) == null)
{
txtStart1 = new TextBox();lblStart1 = new Label(); txtStart1.ID = "txtD1S" + i; lblStart1.ID = "lblD1S" + i; txtStart1.Text = myData\[i\].ToString(); lblStart1.AssociatedControlID = txtStart1.ID; lblStart1.Text = "Data" + i; txtMore.Controls.Add(lblStart1); txtMore.Controls.Add(txtStart1); } }
}
In my ascx page, I am loading those textboxes in as follows:
showData() function is loading data with dynamically created textboxes. This is working fine. Now If i edit textboxes and try to get new data, it is not giving me anything. Actually it couldn't even find that control. Code is as shown below:
public void getData()
{
for (int i = 0; i < myData.Length; i++)
{
TextBox t = txtMore.FindControl("txtD1S" + i) as TextBox;if (t != null) { string temp = t.Text; } }
}
Even though I have dynamically loaded textboxes from showData(), in getData(),it is not able to find any control. Please help.
Dhyanga
-
Hi, I have a situation where I need to show data from database to textboxes. These textboxes has to be dynamically created and can be editable. I am able to create textboxes as follows:
string[] myData = liD1.ToArray(); //liD1 is a list that has values from database
public void showData()
{
for (int i = 0; i < myData.Length; i++)
{
if (txtMore.FindControl("txtD1StartTime" + i) == null)
{
txtStart1 = new TextBox();lblStart1 = new Label(); txtStart1.ID = "txtD1S" + i; lblStart1.ID = "lblD1S" + i; txtStart1.Text = myData\[i\].ToString(); lblStart1.AssociatedControlID = txtStart1.ID; lblStart1.Text = "Data" + i; txtMore.Controls.Add(lblStart1); txtMore.Controls.Add(txtStart1); } }
}
In my ascx page, I am loading those textboxes in as follows:
showData() function is loading data with dynamically created textboxes. This is working fine. Now If i edit textboxes and try to get new data, it is not giving me anything. Actually it couldn't even find that control. Code is as shown below:
public void getData()
{
for (int i = 0; i < myData.Length; i++)
{
TextBox t = txtMore.FindControl("txtD1S" + i) as TextBox;if (t != null) { string temp = t.Text; } }
}
Even though I have dynamically loaded textboxes from showData(), in getData(),it is not able to find any control. Please help.
Dhyanga
You've already posted this question in QA: problem with dynamicall loaded textbox values[^] Don't post the same question in multiple places on the same site.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer