Click here to Skip to main content
14,773,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I have generated textboxes dynamically in a panel in my web form. I was also able to empty them by panel1.controls.clear()
bt this did not resolved my problem.

Explanation:
when page loads, on clicking first button 2 textbox appear in the panel and then again on click of that button 2 more appear. now we have 4 textbox. if we enter data into it and click on 2nd button the data is inserted into database and controls inside panel gets clear with "panel1.controls.clear();".
now if we click on first button again.. it gives 6 textboxes whereas i should get 2 textbox.

aspx.cs code

List<TextBox> textboxes = new List<TextBox>();
    protected void Page_Load(object sender, EventArgs e)
    {
            for (int i = 0; i < TotalNumberAdded; ++i)
            {
                AddControls(i + 1);
            }
            Button1.Click += new EventHandler(Button1_Click);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TotalNumberAdded++;
        AddControls(TotalNumberAdded);
    }
    private void AddControls(int controlNumber)
    {
        var newTextbox = new TextBox();
        newTextbox.ID = "TextBox_" + controlNumber;
        textboxes.Add(newTextBox);
        Panel1.Controls.Add(newTextbox);
    }
    protected int TotalNumberAdded
    {
        get { return (int)(ViewState["TotalNumberAdded"] ?? 0); }
        set { ViewState["TotalNumberAdded"] = value; }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string connStr = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = connStr;
        conn.Open();
        foreach (TextBox tb in textboxes)
        {
            if (tb.Text != "")
            {
                SqlCommand cmd = new SqlCommand("insert into place(city,area) values('"+TextBox1.Text+"','" + tb.Text + "')", conn);
                cmd.ExecuteNonQuery();
            }
        }
        conn.Close();
        TextBox1.Text = "";
        Panel1.Controls.Clear();
    }


I am new to asp.net so all the code for generating textbox dynamically was searched from internet. Let me know if i can improve my existing code with a better coding. :)

Please Help!! Thank you.
Posted
Updated 6-Aug-14 22:40pm
v3
Comments
Dilan Shaminda 7-Aug-14 3:24am
   
can you post your coding part of adding textboxes to the page?
Ashi0891 7-Aug-14 4:02am
   
I have Updated my question please have a look
Dilan Shaminda 7-Aug-14 4:04am
   
check my answer : Solution 3

Reset TotalNumberAdded to 0 after clearing controls inside the panel.
And remember to remove your text boxes from memory as well

"Calling the Clear method does not remove control handles from memory. You must explicitly call the Dispose method to avoid memory leaks."

Source : Control.ControlCollection.Clear Method
   
v4
Comments
Ashi0891 7-Aug-14 4:08am
   
adding TotalNumberAdded=0; in button2_click event code resolved the problem. thank you so much!
Dilan Shaminda 7-Aug-14 4:49am
   
Nice..you are welcome :-)
After updating the value in Database, you are just clearing the textbox and not deleting the textbox. So that's why it is coming as 6 textbox's (existing 4 and new 2) when you click the button again. So it's better you delete the textbox after updating the values in database. So that it will not be 6 when you click the button again.

For deleting the textbox please refer this link[^]

Please let me know is this what you are searching for !!!
Happy Coding :)
   
v3
Comments
Ashi0891 7-Aug-14 3:33am
   
I have already gone through this form and many other that i searched from google. i have added my code to the question. please take a look.
SRK90 7-Aug-14 3:35am
   
Can you please post your full codings of that method ?
Paste your codes here. Then it'll be easy to solve. I think you are using count values to create textboxes.
   
v2
Comments
Ashi0891 7-Aug-14 3:40am
   
i have pasted all the code.. please take a look.

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