动态添加文本框并在按钮单击时捕获数据

时间:2013-05-02 19:28:51

标签: c# asp.net

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

   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);
}

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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)
{

}