How to use dynamically added dropdownlist controls?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Ok, let's say I [must do] recreate the dynamically-added Dropdownlist controls of my web form even at postbacks.
That means they are repopulated with values as well.
(If I don't I won't be getting anything back, right?)

So, how do I maintain the selectedItem between postbacks?

dimitris
 
Dimitris said:
Ok, let's say I [must do] recreate the dynamically-added Dropdownlist
controls of my web form even at postbacks. That means they are
repopulated with values as well. (If I don't I won't be getting
anything back, right?)

Actually, that's not true. If you recreate your list, but you don't fill
it, it will be filled from viewstate.
So, how do I maintain the selectedItem between postbacks?

Just recreate your (empty) list at postback. In fact, you need to
recreate all your controls exactly as before the postback.
Only then will they all keep state.
After handling any events, you may rebuild your page again,
this time destroying the controls that you don't want anymore.
 
Actually, that's not true. If you recreate your list, but you don't fill
it, it will be filled from viewstate.

No, it will not. Here is the code:

private void Page_Load(object sender, System.EventArgs e)
{
GenDDLs();
}

private void GenDDLs()
{
DropDownList ddl;
for (int i = 0; i < 5; i++)
{
ddl = new DropDownList();
if (!IsPostBack) ddl.Items.AddRange(Options());
}
PlaceHolder1.Controls.Add(ddl);
}

all 5 controls become empty after postback.

so ?

dimitris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top