C# 单击按钮时动态添加新文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13488006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:43:50  来源:igfitidea点击:

Dynamically add a new text box when clicking a button

c#asp.netdynamiccontrols

提问by rohan panchal

I'm using this code

我正在使用此代码

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

and aspx.cspage code:

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

On every click on the button I want to add another text box.

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

采纳答案by Pranay Rana

Reason:When you click button again than it do postback to serverside and it removes previously added dynamically textbox

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

Solution:To add it again you need to do like this

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

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

}

that means you need to create the added textbox again...because you are adding control dynamically to the page...

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

Article like this might help you : Retaining State for Dynamically Created Controls in ASP.NET applications

像这样的文章可能对您有所帮助:在 ASP.NET 应用程序中为动态创建的控件保留状态

回答by sajanyamaha

Lets go with a list view

让我们使用列表视图

<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" />

And some codes

还有一些代码

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

Now To extract valuesfrom these added textboxes :

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

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