Dynamically add a new text box on button click

后端 未结 2 372
陌清茗
陌清茗 2020-12-10 20:00

I have textbox \"tb1\" on my page and I want users to be able to add another if they need to input more data. I have the following code:

VB:

相关标签:
2条回答
  • 2020-12-10 20:13

    You can't do that easily using server-side code. You have to use jQuery/JavaScript for this purpose. Please read the asp.net Page and control life cycle from the MSDN.

    0 讨论(0)
  • 2020-12-10 20:28

    You could build your own TextBoxCollection CompositeControl which would enable you to achieve the functionality you require.

    I have put together a basic example of how it could be achieved.

    Control

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace ASPNetWebApplication.Controls
    {
        public class TextBoxCollection : CompositeControl
        {
            private List<string> TextBoxes
            {
                get
                {
                    if (ViewState["TextBoxes"] == null)
                    {
                        ViewState["TextBoxes"] = new List<string>();
                    }
    
                    return (List<string>)ViewState["TextBoxes"];
                }
                set
                {
                    ViewState["TextBoxes"] = value;
                }
            }
    
            protected override void CreateChildControls()
            {
                foreach (string textBox in TextBoxes)
                {
                    Controls.Add(new TextBox() { ID = textBox });
                    Controls.Add(new Literal() { Text = "<br/>" });
                }
            }
    
            public TextBox GetTextBox(string id)
            {
                return (TextBox)Controls.Cast<Control>().Where(t => (t.ID == null ? "" : t.ID.ToLower()) == id.ToLower()).SingleOrDefault();
            }
    
            public void AddTextBox(string id)
            {
                TextBoxes.Add(id);
            }
        }
    }
    

    Example Markup

    Note: Change Assembly="ASPNetWebApplication" to the name of your assembly.

    <%@ Register Assembly="ASPNetWebApplication" Namespace="ASPNetWebApplication.Controls" TagPrefix="ASPNetWebApplication" %>
    
    ...
    
    <ASPNetWebApplication:TextBoxCollection ID="TextBoxCollection1" runat="server" />
    <br />
    <asp:Button ID="AddTextBoxesButton" runat="server" OnClick="AddTextBoxesButton_Click" Text="Add Some Text Boxes" />
    <asp:Button ID="GetTextBoxValuesButton" runat="server" OnClick="GetTextBoxValuesButton_Click" Text="Get TextBox Values" Visible="false" />
    <br />
    <asp:Literal ID="TextBoxEntriesLabel" runat="server"></asp:Literal>
    

    Example Codebehind

    protected void AddTextBoxesButton_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 10; i++)
        {
            TextBoxCollection1.AddTextBox(string.Format("TextBox{0}", i));
        }
    
        AddTextBoxesButton.Visible = false;
        GetTextBoxValuesButton.Visible = true;
    }
    
    protected void GetTextBoxValuesButton_Click(object sender, EventArgs e)
    {
        TextBoxEntriesLabel.Text = string.Empty;
        for (int i = 0; i < 10; i++)
        {
            string textBoxId = string.Format("TextBox{0}", i);
            TextBoxEntriesLabel.Text += string.Format("TextBox: {0}, Value: {1}<br/>", textBoxId, TextBoxCollection1.GetTextBox(textBoxId).Text);
        }
    }
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题