【问题标题】:Dynamically add a new text box when clicking a button单击按钮时动态添加新文本框
【发布时间】:2012-11-09 09:27:18
【问题描述】:

我正在使用此代码

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Button ID="addnewtext" runat="server" Text="Add" onclick="addnewtext_Click" width="76px" />

aspx.cs页面代码:

TextBox tb;
static int i = 0;
protected void addnewtext_Click(object sender, EventArgs e)
{
    tb = new TextBox();
    tb.ID = i.ToString();

    PlaceHolder1.Controls.Add(tb);
    i++;
}

每次单击按钮时,我都想添加另一个文本框。

【问题讨论】:

    标签: c# asp.net dynamic controls


    【解决方案1】:

    原因: 当您再次单击按钮时,它会回发到服务器端并删除以前添加的动态文本框

    解决方案: 要再次添加它,您需要这样做

     TextBox tb;
    static int i = 0;
    protected void addnewtext_Click(object sender, EventArgs e)
    {
            i++;
        for(j=0;j<=i;j++)
        {
        tb = new TextBox();
        tb.ID = j.ToString();
    
        PlaceHolder1.Controls.Add(tb);
        }
    
    }
    

    这意味着您需要再次创建添加的文本框...因为您正在向页面动态添加控件...

    这样的文章可能会对你有所帮助:Retaining State for Dynamically Created Controls in ASP.NET applications

    【讨论】:

    • @Cyber​​Dude - 我把它留给了 OP,但您可以查看更多详细信息:codeproject.com/Articles/3684/…
    • 不,这不是另一个问题,这是您回答中的一个基本问题。您似乎对静态变量的了解与他对回发和视图状态的了解一样多。产生另一个更大问题的解决方案不是解决方案。
    • @Pranay Rana 感谢您的回复,如果我有 2 个添加按钮和两个占位符并想添加文本框。
    • @rohanpanchal - 您可以检查帖子中的链接...即使有 2 个按钮和 2 个占位符,您也需要在回发时重新创建动态添加的控件...。
    【解决方案2】:

    让我们使用列表视图

    <asp:ListView ID="lvDynamicTextboxes" runat="server" 
      ItemPlaceholderID="itemPlaceholder">   <LayoutTemplate>     <table>       <asp:PlaceHolder ID="itemPlaceholder" 
            runat="server"></asp:PlaceHolder>     </table>   </LayoutTemplate>   <ItemTemplate>     <tr>       <asp:TextBox ID="txtText" runat="server">       </asp:TextBox>     </tr>   </ItemTemplate>      
    </asp:ListView>
    
    <asp:Button ID="btnAddTextBox" runat="server" 
      Text="Add" onclick="btnAddTextBox_Click" />
    

    还有一些代码

    private void BindListView()
    {
        //get the current textbox count     int count = 1;
        if (ViewState["textboxCount"] != null)
            count = (int)ViewState["textboxCount"];
    
        //create an enumerable range based on the current count     IEnumerable<int> enumerable = Enumerable.Range(1, count);
    
        //bind the listview     this.lvDynamicTextboxes.DataSource = enumerable;
        this.lvDynamicTextboxes.DataBind();
    }
    
    private void IncrementTextboxCount()
    {
        int count = 1;
        if (ViewState["textboxCount"] != null)
            count = (int)ViewState["textboxCount"];
    
        count++;
        ViewState["textboxCount"] = count;
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
       {
            this.BindListView();  
        }
    }
    
    protected void btnAddTextBox_Click(object sender, EventArgs e)
    {
        this.IncrementTextboxCount();
        this.BindListView();
    }
    

    现在从这些添加的文本框中提取值

    private IList<string> GetValues()
    {
        List<string> values = new List<string>();
        TextBox txt = null;
        foreach (ListViewItem item in this.lvDynamicTextboxes.Items)
        {
            if (item is ListViewDataItem)
           {
                txt = (TextBox)item.FindControl("txtText");
                values.Add(txt.Text);
            }
        }
        return values;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-02
      • 2013-07-09
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多