• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

problem with dynamically created controls on post back

I know somebody has asked a similar question on experts-exchange before and I tried to follow the instructions at http://www.codeproject.com/aspnet/RetainingState.asp, but haven't been able to figure out the problem I have having.

I have a drop down list that someone can use to create a certain number of textboxes. So if somebody chooses 2, the page will dynamically create two text boxes.  The code that I have for the 'SelectedIndexChanged' event of this drop down is as follows:

private void cboNumOfEnvelopes_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  int numOfEnvelopes = Int32.Parse(cboNumOfEnvelopes.SelectedValue);
                  if (numOfEnvelopes>0)
                  {
                        Literal li = new Literal();
                        li.Text = "List Inserts:<br>";
                        panelNumOfEnvelopes.Controls.Add(li);
                        for (int i=1; i<=numOfEnvelopes; i++)
                        {
                              Literal li1 = new Literal();
                              li1.Text = i.ToString() + ". ";
                              TextBox tx = new TextBox();
                              tx.ID = "txtEnvelopes" + i.ToString();
                              tx.Width = 200;
                              Literal li2 = new Literal();
                              li2.Text = "<br>";
                              panelNumOfEnvelopes.Controls.Add(li1);
                              panelNumOfEnvelopes.Controls.Add(tx);
                              panelNumOfEnvelopes.Controls.Add(li2);
                              this.NumberOfControls++;
                        }
                  }
                  panelNumOfEnvelopes.Visible = true;
            }

In addition, I have the following property for the class, which I call MailRoomRequest.
protected int NumberOfControls
            {
                  get{return (int)ViewState["NumControls"];}
                  set{ViewState["NumControls"] = value;}
            }

and the Page_Load is as follows:
private void Page_Load(object sender, System.EventArgs e)
            {
                  if(!Page.IsPostBack)
                        //Initiate the counter of dynamically added controls
                        this.NumberOfControls = 0;
                  else
                        //Controls must be repeatedly be created on postback
                        this.createControls();
            }

and createControls is as follows:
private void createControls()
            {
                  int count = this.NumberOfControls;
                  Literal li = new Literal();
                  li.Text = "List Inserts:<br>";
                  panelNumOfEnvelopes.Controls.Add(li);
                  for (int i=1; i<=count; i++)
                  {
                        Literal li1 = new Literal();
                        li1.Text = i.ToString() + ". ";
                        TextBox tx = new TextBox();
                        tx.ID = "txtEnvelopes" + i.ToString();
                        tx.Width = 200;
                        Literal li2 = new Literal();
                        li2.Text = "<br>";
                        panelNumOfEnvelopes.Controls.Add(li1);
                        panelNumOfEnvelopes.Controls.Add(tx);
                        panelNumOfEnvelopes.Controls.Add(li2);
                  }
                  panelNumOfEnvelopes.Visible = true;
            }

no matter what I try, I can't get this code to work! When I view the source code forthe page, I can't see anything for the dynamic controls. When I select a number from the drop down menu, it works fine the first time (i.e. it creates the text boxes). However, when I change the number, I get the error message 'Multiple controls with the same ID 'txtEnvelopes1' were found. FindControl requires that controls have unique IDs.]'

can someone please help me debug this code?
0
IUAATech
Asked:
IUAATech
  • 8
  • 5
1 Solution
 
Hamed ZaghaghiCommented:
u must use
panelNumOfEnvelopes.Controls.Clear();

before you add your controls;

have a good programming day;
--hamed
0
 
Hamed ZaghaghiCommented:
becuase you dont clear your controls from  panelNumOfEnvelopes,
then in the second time, there are several textbox with a same name;
0
 
Hamed ZaghaghiCommented:
change  to :
private void createControls()
          {
               panelNumOfEnvelopes.Controls.Clear();// this code is inserted
               int count = this.NumberOfControls;
               Literal li = new Literal();
               li.Text = "List Inserts:<br>";
               panelNumOfEnvelopes.Controls.Add(li);
               for (int i=1; i<=count; i++)
               {
                    Literal li1 = new Literal();
                    li1.Text = i.ToString() + ". ";
                    TextBox tx = new TextBox();
                    tx.ID = "txtEnvelopes" + i.ToString();
                    tx.Width = 200;
                    Literal li2 = new Literal();
                    li2.Text = "<br>";
                    panelNumOfEnvelopes.Controls.Add(li1);
                    panelNumOfEnvelopes.Controls.Add(tx);
                    panelNumOfEnvelopes.Controls.Add(li2);
               }
               panelNumOfEnvelopes.Visible = true;
          }
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
NauticalNonsenseCommented:
On your PostBack, are you creating the controls IN ADDITION to what's in viewstate for those controls?

For example the first time it runs, you have txtEnvelopes1, and then when you postback, you have txtEnvelopes1 in ViewState (already) and are recreating it again because CreateControls() is running every time the page is reposted?
0
 
IUAATechAuthor Commented:
Here is what I get when I select "2" from the drop down list (notice the extra 'List Inserts").

List Inserts:
List Inserts:
1. <textbox1>
2. <textbox2>

I then selected a different number and I still get 'Multiple controls with the same ID 'txtEnvelopes1' were found' despite adding  panelNumOfEnvelopes.Controls.Clear(); to the method createControls().

NauticalNonsense, I am not quite sure I understand your question (I am quite new to ASP.NET). The 'AutoPostBack' is set to True for cboNumOfEnvelopes. From your example, it sounds like I am indeed recreating the controls in addition to what's in viewstate. Maybe you can figure it out from my code.....

0
 
Hamed ZaghaghiCommented:
did you add
panelNumOfEnvelopes.Controls.Clear();
to SelectedIndexChanged ?

0
 
IUAATechAuthor Commented:
ok. adding panelNumOfEnvelopes.Controls.Clear(); to both the methods seemed to work until I saw more problems. I have a checkbox control whose 'AutoPostBack' is set to True and selecting/deselecting it shows/hides another panel (which is NOT dynamically created). So now, lets say I select '3' from my cboNumOfEnvelopes dropdown and then I change my mind and select '4' instead. After doing this, if I select the checkbox (which is supposed to show/hide a certain panel), the 4 dynamic textboxes that I had earlier increases to 7 (3+4). Something to do with this.NumberOfControls++; in the dropNumOfEnvelopes_SelectedIndexChanged method?

Since this is a separate question, I am gonna increase the points.
0
 
Hamed ZaghaghiCommented:
it's best that you ask your new question in a new session ;)
--hamed
0
 
IUAATechAuthor Commented:
since this new issue is pretty much tied to what I had asked earlier, I thought I would ask it in the same session.
I will be sure to split the points fairly :)
0
 
Hamed ZaghaghiCommented:
you want to have 2 panel, one that create dunamicaly and one that there is in your form, and you want to show/hide it,
so when you select 4 from the cboNumOfEnvelopes, and then select the checkbox, the static panel show ? text box?
0
 
IUAATechAuthor Commented:
actually both the panels are NOT created dynamically. In one panel I add textboxes dynamically (when I select a number from the dropdown menu) and in the other panel, the controls are already added beforehand. I just use a checkbox to show/hide this second panel.
However, when I select twice from the drop down menu (say I select 3 and then 4), the code will add 4 textboxes to the first panel. AND THEN I select the checkbox to show the second panel. But doing this changes the number of textboxes I have in my first panel to 7 (3+4). I hope this explanation makes sense.
0
 
Hamed ZaghaghiCommented:
u can use a invisible panel that keep the textboxes in the prev selected, and when the check box selected, use this:

panelNumOfEnvelopes.Controls.Add(tmpPanle.Controls);

hope help you
0
 
IUAATechAuthor Commented:
I see what you mean. But I cannot have an invisible panel. I want it to be visible all the time since I need for someone to enter text in the textboxes in the first panel, then when they select the checkbox, I want them to enter information in the textboxes in the second panel. Maybe if you can show more code, it would make sense.
0
 
Hamed ZaghaghiCommented:
you must set the checkbox autopostback to true, and then handle the CheckedChanged event and in this function swap the visiblaty of the second panel.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now