从更新面板内表获取动态添加文本框的值 [英] Get the value of dynamically added text box from table inside update panel

查看:142
本文介绍了从更新面板内表获取动态添加文本框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是控件ASPX。

 <asp:UpdatePanel runat="server" ID="panel" ChildrenAsTriggers="false" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:PlaceHolder runat="server" ID="holder"></asp:PlaceHolder>
            <asp:Button runat="server" ID="bt" Text="Add" onclick="AddTxt" />
            <asp:Button runat="server" ID="btn1" Text="Refresh" onclick="btn1_Click" />
        </ContentTemplate>

在添加按钮被点击我创建一个动态表。下面是C#code

When the button Add is clicked i am creating a dynamic table. Below is the C# code

protected void AddTxt(object sender, EventArgs e)
{
    int tblRows = 3;
    int tblCols = 3;

    Table tbl = new Table();

    for (int i = 0; i < tblRows; i++)
    {
        TableRow tr = new TableRow();
        for (int j = 0; j < tblCols; j++)
        {
            TableCell tc = new TableCell();
            TextBox txtBox = new TextBox();
            txtBox.ID = "txt" + i.ToString() + j.ToString();
            //txtBox.TextChanged += new EventHandler(txt_TextChanged);
            tc.Controls.Add(txtBox);

            tr.Cells.Add(tc);

            if (Session["ctls"] != null)
            {
                Session["ctls"] += ";t";
            }
            else
            {
                Session["ctls"] = "t";
            }
        }

        tbl.Rows.Add(tr);
    }

    holder.Controls.Add(tbl);

    panel.Update();

}

在部分回发上的刷新按钮,点击会出现我不能够获得它是由用户在文本框内更新的价值。该控件将有空文本。

When the partial postback occurs on click of Refresh button i am not able to get the value which is updated by user inside the text box. The controls will have empty text.

protected void Page_Load(object sender, EventArgs e)
{
    foreach (Control c in holder.Controls)
    {
        if (c is TextBox)
        {

        }

    }
}

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    if (Session["ctls"] != null)
    {
        string[] ctls = Session["ctls"].ToString().Split(';');
        foreach (string ctlType in ctls)
        {
            if (string.Compare(ctlType, "t") == 0)
            {
                holder.Controls.Add(new TextBox());
            }
        }
    }
}

可能任何一个请帮助/给我一个提示如何围绕动态控制工作更新面板内。

Could any one please help/give me a hint how to work around dynamic controls inside update panel.

推荐答案

现在的问题是,当你动态地创建控件(表,行,单元格,文本框)在 AddTxt 方法,你都分配一个 ID 价值的文本框,但是当你动态地在的OnInit 事件创建控件,那么你没有创建 ID 价值可言,因此的ViewState 不能连接到什么用户在输入从而失去了。

The problem is that when you dynamically create the controls (table, rows, cells, textbox) in the AddTxt method you are assigning an ID value to the textbox, however when you dynamically create the controls in the OnInit event, then you are not creating ID value at all, thus the ViewState cannot be wired up to what the user typed in and thus lost.

如果您创建具有相同 ID文本框值( txtBox.ID =TXT+ i.ToString()+ j.ToString (); )作为其他动态控件创建逻辑,那么你的的ViewState 价值将是preserved并在正确应用到文本框它的OnInit 正在重建。

If you create the textbox with the same ID value (txtBox.ID = "txt" + i.ToString() + j.ToString();) as in the other dynamic control creation logic, then your ViewState value would be preserved and properly applied to the textbox upon it being recreated in OnInit.

请注意:的ViewState 在很大程度上依赖于 ID 为了服务器控件的值的控制得宜准值全部或部分回发之间。

Note: ViewState is heavily dependent upon the ID values of server controls in order to properly associate values of controls between full and partial postbacks.

这篇关于从更新面板内表获取动态添加文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆