我正在根据条件向我的页面动态添加控件。这些控件中有一个按钮,我还为单击事件附加了一个事件处理程序。现在在这个事件处理程序中,我正在尝试访问我的动态生成控制,但得到一个例外。这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
String Method = Request.QueryString["Method"];
String Tag = Request.QueryString["Tag"];
if (Method=="ADD" && Tag=="METHOD")
{
//6
TableCell cell11 = new TableCell();
cell11.Text = "NEXTLEVEL";
TableCell cell12 = new TableCell();
TextBox txt6 = new TextBox();
txt6.ID = "txt6";
cell12.Controls.Add(txt6);
TableRow row6 = new TableRow();
row6.Cells.Add(cell11);
row6.Cells.Add(cell12);
container.Rows.Add(row6);
TableCell cell14 = new TableCell();
Button submit = new Button();
submit.ID = "SubmitButton";
submit.Text = "Submit";
submit.Click += new EventHandler(submit_Click);
cell14.Controls.Add(submit);
TableRow row7 = new TableRow();
row7.Cells.Add(cell14);
container.Rows.Add(row7);
}
void submit_Click(object sender, EventArgs e)
{
ModifySessionAnalyzer msa = new ModifySessionAnalyzer();
TextBox txt6= (TextBox)Page.FindControl("txt6") as TextBox;
##String message = txt6.Text;##
}