Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically add a new text box when clicking a button

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.cs page code:

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.

like image 287
rohan panchal Avatar asked Nov 21 '12 07:11

rohan panchal


2 Answers

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

like image 76
Pranay Rana Avatar answered Sep 20 '22 23:09

Pranay Rana


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 values from 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;
}
like image 41
sajanyamaha Avatar answered Sep 22 '22 23:09

sajanyamaha