【问题标题】:ASP.NET dynamic creation of text box using asp:Panel and access the text box using FindControlASP.NET 使用 asp:Panel 动态创建文本框并使用 FindControl 访问文本框
【发布时间】:2016-11-28 17:12:08
【问题描述】:

我正在使用 ASP Panel 在按钮单击时动态创建文本框。我使用的代码如下,

<asp:TextBox ID="text_number" runat="server"></asp:TextBox>
<asp:Button ID="button1" runat="server" OnClick="button1_Click"></asp:Button>
<asp:Panel ID="mypanel" runat="server"></asp:Panel>
<asp:Button ID="button2" runat="server" OnClick="button2_Click"></asp:Button>


protected void button1_Click(object sender, EventArgs e)
{
    int n = Convert.ToInt32(text_number.Text);
    for (int i = 0; i < n; i++)
    {
        TextBox MyTextBox = new TextBox();
        MyTextBox.ID = "newtext_" + (i + 1);
        mypanel.Controls.Add(MyTextBox);
        MyTextBox.CssClass = "textblock";
        Literal lit = new Literal();
        lit.Text = "<br />";
        mypanel.Controls.Add(lit);
    }
}

点击button1后,文本框被创建,然后我在文本框中输入值并点击button2。单击 button2 时,应读取文本框中的所有值并将其存储在 C# 后面的列表中。我使用的代码如下,

protected void button2_Click(object sender, EventArgs e)
{
    int n = Convert.ToInt32(text_number.Text);
    for (int i = 0; i < n; i++)
    {
        TextBox control = (TextBox) FindControl("newtext_" + (i+1));
        mylist.Add(control.Text);
    }
}

但是,每当我单击 button2 时,我在面板中添加的所有文本框都会从网页中消失,并且我在 FindControl 中得到空对象引用错误。我可以理解,当按下按钮 2 时面板中的所有文本框控件都会被清除,这就是它们在网页中消失的原因,也是出现空对象错误的原因。这里有什么问题?有没有其他方法可以在单击按钮时动态创建“n”文本框,然后在第二次单击按钮时从中获取值而不会出现任何此类问题?

【问题讨论】:

    标签: c# html asp.net


    【解决方案1】:

    因为您是动态创建控件,所以您需要在每次页面加载时重新生成它们,否则控件及其内容将会丢失。

    您需要做的是将文本框的数量存储到Session 或ViewState 中并重新创建它们。

    protected void Page_Load(object sender, EventArgs e)
    {
        //check if the viewstate exists and if so, build the controls
        if (ViewState["boxCount"] != null)
        {
            buildControls(Convert.ToInt32(ViewState["boxCount"]));
        }
    }
    
    protected void button1_Click(object sender, EventArgs e)
    {
        //adding controls moved outside the button click in it's own method
        try
        {
            buildControls(Convert.ToInt32(text_number.Text));
        }
        catch
        {
        }
    }
    
    protected void button2_Click(object sender, EventArgs e)
    {
        //check if the viewstate exists and if so, build the controls
        if (ViewState["boxCount"] != null)
        {
            int n = Convert.ToInt32(ViewState["boxCount"]);
    
            //loop al controls count stored in the viewstate
            for (int i = 0; i < n; i++)
            {
                TextBox control = FindControl("newtext_" + i) as TextBox;
                mylist.Add(control.Text);
            }
        }
    }
    
    private void buildControls(int n)
    {
        //clear the panel of old controls
        mypanel.Controls.Clear();
    
        for (int i = 0; i < n; i++)
        {
            TextBox MyTextBox = new TextBox();
            MyTextBox.ID = "newtext_" + i;
            MyTextBox.CssClass = "textblock";
            mypanel.Controls.Add(MyTextBox);
    
            Literal lit = new Literal();
            lit.Text = "<br />";
            mypanel.Controls.Add(lit);
        }
    
        //save the control count into a viewstate
        ViewState["boxCount"] = n;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-08
      • 1970-01-01
      • 1970-01-01
      • 2011-09-10
      • 2013-08-09
      • 2010-10-24
      • 1970-01-01
      • 2015-03-05
      相关资源
      最近更新 更多