Reputation: 361
I am creating some dyanamic textbox on a button click event and on another button click i want to fetch data of that textbox using findcontrol method
public void addDepartmentBtn_Click(object sender, EventArgs e)
{
int count = Convert.ToInt32(countTxtBx.Text);
lblErrorMsg.Text = "";
if (Convert.ToInt32(countTxtBx.Text) <= 5)
{
for (int i = 0; i < count; i++)
{
Label lb = new Label();
TextBox tb = new TextBox();
tb.ID = "Textbox_" + i;
lb.ID = "Label_" + i;
lb.Text = "Enter Departnment Name: " + Convert.ToInt32(i + 1);
pnlMain.Controls.Add(new LiteralControl("<br>"));
pnlMain.Controls.Add(lb);
pnlMain.Controls.Add(new LiteralControl("  "));
pnlMain.Controls.Add(tb);
pnlMain.Controls.Add(new LiteralControl("<br><br>"));
lblErrorMsg.Text = Convert.ToInt32(i + 1) + " Departments Created Successfully";
//string str = string.Empty;
//TextBox myTB = (TextBox)pnlMain.FindControl("Textbox_" + i);
//str = myTB.Text;
//Response.Write(str);
}
}
else
{
lblErrorMsg.Text = "You cannot create more than 5 Departments at once:";
}
}
On button2 click:
protected void Button2_Click(object sender, EventArgs e)
{
string alltextdata = null;
for (int i = 0; i < 5; i++)
{
Control controltxt = FindControl("Textbox_"+i);
if (controltxt != null)
{
TextBox txttemp = (TextBox)controltxt;
alltextdata = txttemp.Text;
}
}
}
but my find control method alway show me null i check my html page view source which show me every thing correct my textbox name and id is "Textbox_0",Textbox_1 etc
am i doing some mistake ? please help
Upvotes: 1
Views: 1292
Reputation: 327
Before calling your findcontrol method, you have to load that contro again as it is dynamically created on each and every postback. These are not like our static controls which are created under Page_Init. Dynamic controls are created under Page_Load event.
Upvotes: 0
Reputation: 13286
When you add the control dynamically, it is not added to the control tree after a postback (the button2 postback). You need to add it again in the Page_Load event in any postback after addDepartmentBtn was clicked.
Save that button was clicked in the ViewState and check it in Page_Load:
public void addDepartmentBtn_Click(object sender, EventArgs e)
{
ViewState["addDepartmentBtn_Clicked"] = true;
AddTextBoxes();
}
protected void Page_Load(object sender, EventArgs e)
{
if (Convert.ToBoolean(ViewState["addDepartmentBtn_Clicked"]) == true)
AddTextBoxes();
}
public void AddTextBoxes()
{
int count = Convert.ToInt32(countTxtBx.Text);
lblErrorMsg.Text = "";
if (Convert.ToInt32(countTxtBx.Text) <= 5)
{
for (int i = 0; i < count; i++)
{
Label lb = new Label();
TextBox tb = new TextBox();
tb.ID = "Textbox_" + i;
lb.ID = "Label_" + i;
lb.Text = "Enter Departnment Name: " + Convert.ToInt32(i + 1);
pnlMain.Controls.Add(new LiteralControl("<br>"));
pnlMain.Controls.Add(lb);
pnlMain.Controls.Add(new LiteralControl("  "));
pnlMain.Controls.Add(tb);
pnlMain.Controls.Add(new LiteralControl("<br><br>"));
lblErrorMsg.Text = Convert.ToInt32(i + 1) + " Departments Created Successfully";
//string str = string.Empty;
//TextBox myTB = (TextBox)pnlMain.FindControl("Textbox_" + i);
//str = myTB.Text;
//Response.Write(str);
}
}
else
{
lblErrorMsg.Text = "You cannot create more than 5 Departments at once:";
}
}
Upvotes: 2