How to use dynamically added dropdownlist controls?

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
 
J

Jos

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.
 
G

Guest

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top