2

I am using ASP.NET/C#.

In one of my page I am using Update Panel.Inside my Update Panel I have a LinkButton which adds Textbox inside the Update Panel.

The Textboxes are created dynamically and it is working fine.However when I click the same Linkbutton to add next Textbox , the value of my previous Textbox is lost.

I think this is because of PostBack.

Here is my code for creating Textboxes.

protected void linkAddAmount_Click(object sender, EventArgs e)
        {
            int count = 0;

            if (ViewState["ButtonCount"] != null)
            {
                count = (int)ViewState["ButtonCount"];
            }

            count++;
            ViewState["ButtonCount"] = count;

            for (int i = 0; i < count; i++)
            {
                AmountUpdatePanel.ContentTemplateContainer.Controls.Add(new LiteralControl("<span>From:&nbsp;</span>"));
                TextBox textbox1 = new TextBox();
                textbox1.ID = "txtAmountFrom" + i;
                textbox1.Attributes.Add("class", "ShortTextbox");
                AmountUpdatePanel.ContentTemplateContainer.Controls.Add(textbox1);
            }
        }

Can anyone help me to solve this issue?

Any suggestion is welcome.

Priyank Patel
  • 6,898
  • 11
  • 58
  • 88

3 Answers3

3

Maybe this MSDN article explains why this happens: Dynamic Web Server Controls and View State

You will find a good explanation in this older article: Dynamic Web Controls, Postbacks, and View State

If the dynamic controls are created on each page request, they will pick up the ViewState values from the previous request, so maybe create the controls on load, hide them and show them when the user does something.

Also, if you have controls created at design time that have this behavior of losing the values after postback, check that you don't set their EnableViewState property to false somewhere.

And you should set the Name of the control along it's Id. The name is used in the form submission. This is best seen when you have a list of radio buttons with different ids and the same name. When submitted, a pair with the name of the radio buttons as key and selected radio button's id as value will be sent.

victorvartan
  • 1,002
  • 2
  • 11
  • 31
  • I am now creating these textboxes on postback so that they can retain values , but it doesnt seem to be a good solution , as my Add button will just do a postback and on postback I will create textboxes.What do you think? – Priyank Patel Aug 07 '12 at 08:57
  • Well, the best solution would be not to alter the interface, but to alter the data source on submit, and create the interface to reflect that data source on load. For example, you can have a list of amounts and when a user clicks on the Add button, just add a new amount entry. Read the list of amounts on load, and generate a textbox for each amount. When the users clicks on the Save button iterate through all the controls in the form and if the control name starts with "txtAmountFrom" get the value of txtAmountFromN into the N-th item in the list and so on. – victorvartan Aug 07 '12 at 09:17
  • I am trying to create it on page load but I don't know how many times the user clicks the Add button , so do you think I can use Session to store the count?Thanks for the effort. – Priyank Patel Aug 07 '12 at 10:39
  • If you add a new amount entry to a list of amounts (kept in a session or in the database) on each Add button click, then you can use the Count property of the list to find out the number of textboxes required. – victorvartan Aug 07 '12 at 11:41
  • @victovartan Exactly I am trying it out.Thanks for the help.Much appreciated. – Priyank Patel Aug 07 '12 at 11:54
1

On Postback one can call these type of code to regain the value of Textbox.

txtPartName.Text = Request.Form[txtPartName.UniqueID];

For the first-time load we can below code before set values as said above line. These lines may helpfull if the values are binding from DB or outside for the first-time.

if (!string.IsNullOrEmpty(Request.Form[txtPartName.UniqueID]))
Raaghav
  • 3,000
  • 1
  • 23
  • 21
0

Info on using dynamic controls in an update panel can be found here on Stack:

Dynamic created controls inside UpdatePanel?

Did you implement the answer from flem in following way?

protected void linkAddAmount_Click(object sender, EventArgs e)
        {
            int count = 0;

            if (ViewState["ButtonCount"] != null)
            {
                count = (int)ViewState["ButtonCount"];
            }

            count++;
            ViewState["ButtonCount"] = count;

            for (int i = 0; i < count; i++)
            {
                AmountUpdatePanel.ContentTemplateContainer.Controls.Add(new LiteralControl("<span>From:&nbsp;</span>"));
                TextBox textbox1 = new TextBox();
                textbox1.ID = "txtAmountFrom" + i;
                textbox1.Attributes.Add("class", "ShortTextbox");

            if (!string.IsNullOrEmpty(Request.Form["txtAmountFrom" + i.ToString()]))
                {
              textbox1.Text = Request.Form["txtAmountFrom" + i.ToString()];
                }

                AmountUpdatePanel.ContentTemplateContainer.Controls.Add(textbox1);
            }
        }
Community
  • 1
  • 1
Luke Baughan
  • 4,658
  • 3
  • 31
  • 54