1

我在按钮单击事件上创建一些动态文本框,在另一个按钮单击上我想使用 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("&nbsp&nbsp"));
                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 等

我做错了吗?请帮忙

4

2 回答 2

2

当您动态添加控件时,它不会在回发(button2 回发)之后添加到控件树中。单击 addDepartmentBtn 后,您需要在任何回发的 Page_Load 事件中再次添加它。

保存在 ViewState 中单击的按钮并在 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("&nbsp&nbsp"));
                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:";
        }
}
于 2012-12-04T05:54:43.530 回答
0

在调用 findcontrol 方法之前,您必须再次加载该控件,因为它是在每次回发时动态创建的。这些不像我们在 Page_Init 下创建的静态控件。在 Page_Load 事件下创建动态控件。

于 2012-12-04T05:58:49.760 回答