DropDownList SelectedIndexChanged event not firing
am 13.11.2007 19:37:04 von MartinHortonI recently had a small problem that took me a day to solve, and I'm not sure
if it represents a bug, or a lack of understanding on my part.
I am in the process if redoing a website to make extensive use of
MasterPages and to use the aspnet Membership/Roles/Profiles functions.
I had one fairly simple page in the old site that, for the sake of
simplicity, contained two DropDownLists. The listitems in the first were
retrieved from a database in the !IsPostBack path of Page_Load. The second
DropDownList was populated in the OnSelectedIndexChanged event of the first
DropDownList. The only change I wanted to make to this page was to use the
new MasterPage (It already used a MasterPage, but I wanted to use a different
one). The DropDownLists were contained within an UpdatePanel.
So I copied the page from the old web site to the new one and changed the
MasterPageFile attribute.
Imagine my surprise when this page would not, under any circumstances, fire
the "SelectedIndexChanged" event for either DropDownList.
Eventually, I came to the conclusion that the problem had to lie either in
the web.config file or in the new Masterpage. The problem lay in the
MasterPage file.
The MasterPage had a menu across the top. If the person logged on was not in
a particular Role I wanted to remove one of the listitems from the menu.
Given that, in general, I prefer to remove extraneous stuff from the
resultant HTML rather than just hide the control I had done the following:
In the MasterPage I trapped the LoadComplete event of the page and had code
similar to...
ctlId.Parent.Controls.Remove(ctlId);
This worked nicely, but it appears that this totally messes up the
ViewState, at least for the DropDownLists. When I replaced the line above
with....
ctlId.Visible = false;
everything functioned perfectly.
I thought I would share this in case it helps anyone. Also I am wondering if
this is a bug and that it should have worked or whether deleting controls in
the "PageComplete" should be documented as not admissible.