我有一种方法可以在每次单击按钮时创建文本框。但是我正在尝试以另一种方法访问ID。有些东西似乎超出了范围,这里是代码:Textbox
public partial class _Default : System.Web.UI.Page
{
TextBox box = new TextBox();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["count"] = Convert.ToInt32(ViewState["count"]) + 1;
Label1.Text = ViewState["count"].ToString();
int test = int.Parse(string.Format("{0}", ViewState["count"]));
for (int j = 0; j < test; j++)
{
TableRow r = new TableRow();
box.ID = "Textbox" + j;
TableCell c = new TableCell();
c.Controls.Add(box);
r.Cells.Add(c);
table1.Rows.Add(r);
Response.Write(box.ID);
}
if (test == 4)
{
Button1.Visible = false;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write(box.ID);
}
我想Button2_Click
打印出来box.ID
以确保它能够访问它。