1

我正在动态添加文本框并尝试在单击按钮时捕获在文本框中输入的数据。但发生的事情是,虽然我在文本框中输入了数据,但当我单击按钮时,页面正在加载并且控件正在重新创建。结果,我丢失了文本框中的数据。你能告诉我如何捕获输入到动态创建的文本框中的这些数据。我的示例代码如下:

   protected void Page_Load(object sender, EventArgs e)
    {
        Table tblTextboxes = new Table();
        for(int i=0;i<10;i++)
        {
            TableRow tr=new TableRow();
            TableCell tc=new TableCell();
            TextBox tb=new TextBox();
            tb.ID=i.ToString();
            tc.Controls.Add(tb);
            tr.Cells.Add(tc);

            TableCell tc1=new TableCell();
            LinkButton lnk=new LinkButton();
            lnk.ID=i.ToString()+tb.Text+"lnk";
            lnk.Text = "Show";
            lnk.Click+=new EventHandler(lnk_Click);
            tc1.Controls.Add(lnk);

            tr.Cells.Add(tc1);

            tblTextboxes.Rows.Add(tr);
        }
        placeTest.Controls.Add(tblTextboxes);
    }



void  lnk_Click(object sender, EventArgs e)
{
LinkButton lnk=sender as LinkButton;
Label lbl=new Label();
lbl.Text="The text is"+lnk.ID;
placeTest.Controls.Add(lbl);
}
4

2 回答 2

1

LinkButton ID每次输入文本TextBox并回发时都会更改。

动态创建控件时要确保的一件事是 - 您希望在回发时使用相同的 ID 重新创建它们。

更新的解决方案(从 TextBox 中检索文本)

protected void Page_Load(object sender, EventArgs e)
{
    var tblTextboxes = new Table();
    for (int i = 0; i < 10; i++)
    {
        var tr = new TableRow();
        var tc = new TableCell();
        var tb = new TextBox {ID = i.ToString()};
        tc.Controls.Add(tb);
        tr.Cells.Add(tc);

        var tc1 = new TableCell();
        // This is a fix for - lnk.ID=i.ToString()+tb.Text+"lnk";
        var lnk = new LinkButton {ID = i + "lnk", Text = "Show"};
        lnk.Click += lnk_Click;
        tc1.Controls.Add(lnk);

        tr.Cells.Add(tc1);

        tblTextboxes.Rows.Add(tr);
    }
    placeTest.Controls.Add(tblTextboxes);
}

void lnk_Click(object sender, EventArgs e)
{
    var lnk = sender as LinkButton;
    var lbl = new Label();
    lbl.Text = "LinkButton ID: " + lnk.ID;

    // Get number value from string
    string id = Regex.Replace(lnk.ID, @"[^\d]", "");

    // Retrieves a TextBox control by ID    
    var control = FindControlRecursive(Page, id);

    if (control != null)
    {
        var textbox = control as TextBox;
        lbl.Text += "; TextBox Text: " + textbox.Text;
    }

    placeTest.Controls.Add(lbl);
}

public Control FindControlRecursive(Control root, string id)
{
    if (root.ID == id)
        return root;

    return root.Controls.Cast<Control>()
        .Select(c => FindControlRecursive(c, id))
        .FirstOrDefault(c => c != null);
}
于 2013-05-02T19:44:13.867 回答
0

基于MSDN,我建议使用Page类的Init事件。查看标题视图状态和动态添加的控件以获取解释。另外,我建议在所有现有控件的末尾添加动态控件。首先创建表格,然后添加文本框。

我修改了你的代码。它对我有用。我使用VS 2012,.Net 4.5

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write(DateTime.Now.ToString());

}
protected void Page_Init(object sender, EventArgs e)
{

    Table tblTextboxes = new Table();
    for (int i = 0; i < 10; i++)
    {
        TableRow tr = new TableRow();
        TableCell tc = new TableCell();
        TextBox tb = new TextBox();
        tb.ID = i.ToString();
        tc.Controls.Add(tb);
        tr.Cells.Add(tc);

        //TableCell tc1 = new TableCell();
        //LinkButton lnk = new LinkButton();
        //lnk.ID = i.ToString() + tb.Text + "lnk";
        //lnk.Text = "Show";
        //lnk.Click += new EventHandler(lnk_Click);
        //tc1.Controls.Add(lnk);

        //tr.Cells.Add(tc1);

        tblTextboxes.Rows.Add(tr);
    }
    placeTest.Controls.Add(tblTextboxes);

}

protected void Button1_Click(object sender, EventArgs e)
{

}
于 2013-05-02T20:02:21.283 回答