我在按钮单击事件上创建一些动态文本框,在另一个按钮单击上我想使用 findcontrol 方法获取该文本框的数据
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:";
}
}
在按钮 2 上单击:
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;
}
}
}
但是我的查找控制方法总是显示 null 我检查了我的 html 页面视图源,它显示了所有正确的东西我的文本框名称和 id 是“Textbox_0”,Textbox_1 等
我做错了吗?请帮忙