Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello developers,

I need bit of your help.
I have an asp table which consist of 3 fields.. 'Name','Age' and 'Gender', these 3 fields are horizontally aligned and right below them there is a button 'Add More'
What i want is whenever a user clicks 'Add More' all 3 textboxes with the field names are added below and a 'Remove' button is also added (in case if the user wants to remove the text boxes).
The user can add an infinte amount of text boxes, there is no limitation.. but if a user reaches a specific amount of textboxes lets say 5.. then a scroll bar should be generated so that he can add more boxes and they are added in the same page and are visible once the user scrolls down.

Looking forward to your support.
Your help would be highly appreciated.

Thanks.

What I have tried:

I have already searched different sites.. i have found solutions similar but not exactly the same.
Posted
Updated 23-Sep-16 1:22am

1 solution

This should help you get started: Dynamically Adding and Deleting Rows in GridView and Saving All Rows at Once[^]

The code above is entirely server-side approach. It also uses ViewState to hold the data across postbacks. You need to be careful when using ViewState to avoid page performance issue. Also ViewState has a limit when it comes to size, so make sure that you don't store a huge amount of data in it.

Another approach that you can do is to use JavaScript/jQuery to add dynamic controls at client. That way all interactions only happens at the browser, so you can have a fast response. Just google "adding dynamic textbox using jquery" to get more examples. Here's one I found: How to add / remove textbox dynamically with jQuery[^]

Quote:
The user can add an infinte amount of text boxes

You may need to re-think your requirements. You should think about page performance and user experience. What do you think will happen when you add 100,000 TextBox on the page?

Here's a quick example based on your latest comment:

ASPX:

ASP.NET
<asp:TextBox ID="TextBox1" runat="server"/>
<asp:TextBox ID="TextBox2" runat="server"/>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server" >
</asp:GridView>


CODE BEHIND:

C#
//A method that will BIND the GridView based on the TextBox 
//values and retain its values on post backs.

private void BindGrid(int rowcount)
{
        DataTable dt = new DataTable();
        DataRow dr;
        dt.Columns.Add(new System.Data.DataColumn("Name", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("Location", typeof(String)));
 
        if (ViewState["CurrentData"] != null)
        {
            for (int i = 0; i < rowcount + 1; i++)
            {
                dt = (DataTable)ViewState["CurrentData"];
                if (dt.Rows.Count > 0)
                {
                    dr = dt.NewRow();
                    dr[0] = dt.Rows[0][0].ToString();
 
                }
            }
            dr = dt.NewRow();
            dr[0] = TextBox1.Text;
            dr[1] = TextBox2.Text;
            dt.Rows.Add(dr);
 
        }
        else
        {
            dr = dt.NewRow();
            dr[0] = TextBox1.Text;
            dr[1] = TextBox2.Text;
 
            dt.Rows.Add(dr);
 
        }
 
        // If ViewState has a data then use the value as the DataSource
        if (ViewState["CurrentData"] != null)
        {
            GridView1.DataSource = (DataTable)ViewState["CurrentData"];
            GridView1.DataBind();
        }
        else
        {
        // Bind GridView with the initial data assocaited in the DataTable
            GridView1.DataSource = dt;
            GridView1.DataBind();
 
        }
        // Store the DataTable in ViewState to retain the values
        ViewState["CurrentData"] = dt;
 
    }

 protected void Button1_Click(object sender, EventArgs e)
 {
        // Check if the ViewState has a data assoiciated within it.
        if (ViewState["CurrentData"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentData"];
            int count = dt.Rows.Count;
            BindGrid(count);
        }
        else
        {
            BindGrid(1);
        }
        TextBox1.Text = string.Empty;
 
        TextBox1.Focus();
}
 
Share this answer
 
v3
Comments
Faran Saleem 23-Sep-16 8:57am    
Hey Vincent..The gridvew approach is working fine for me.. But just another query.. i have 2 fixed columns as well 'School Name' and 'City' and user will fill and submit those fields aswell along with the dynamic gridview..
Like if a School name is ABC and City is London. So i want to store this and dynamically generated columns in gridview in the same table in sql database..can you help me with that please..?

And user will not be entering 100,000 records.. just wanted it to be dynamic to add text boxes without any limit
Vincent Maverick Durano 23-Sep-16 9:03am    
The first link should do the trick for you. You just need to tweak the code a bit based on your requirements.
Faran Saleem 25-Sep-16 13:59pm    
Thank you Vincent. your guidance here helped me a lot. Thanks a ton.. :)
Vincent Maverick Durano 23-Sep-16 9:10am    
I've updated the solution above and included a quick demo. Please take a look.
Karthik_Mahalingam 23-Sep-16 9:14am    
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900