Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

DropDownList & Dynamic UserControls

3 views
Skip to first unread message

aye...@gmail.com

unread,
Nov 20, 2005, 1:46:57 PM11/20/05
to
Okay, I'm pretty sure that I'm doing everything right here, but
something is very wrong in the result.

I've a page that has a drop down list (with auto-post-back = true) and
a place holder.

During page load, I'm loading a control dynamically (using
LoadControl()) and adding it to the place holder.

In the control OnInit(), I load some values into it.

The issue is when the page is doing a post back (when the page's drop
down list changes).

Then I load another control (totally different), which has another drop
down list (with a different Id then the one on the first control.)
In the 2nd control OnInit(), I load different values into the control's
drop down list.
I then add it to the page.

On the page I get the _first control's drop down list values_!

I debugged it, and it seems that the old drop down list values are
being added to the new drop down list when I add it to the place holder
on the page.

I've no idea why, since they have a different id and are loaded in a
totally different user control.

If I move the adding of values to the load event, the old values are
still there, and they appear first, and then the new values.

If I disable the view state of either control, everything works as it
should. But I need the view state for those controls.

I've a demo of the bug at:
http://www.ayende.com/DropDownListBug.zip

aye...@gmail.com

unread,
Nov 21, 2005, 5:11:10 AM11/21/05
to
In case someone else has the same problem, the issue is that ASP.Net
loads the controls by index, and not by ID.
You can see the docs here:
http://msdn2.microsoft.com/en-us/library/system.web.ui.viewstatemodebyidattribute.aspx

It can be solved by simply annotating the control with a
ViewStateModeById attribute.

0 new messages