dropdown list selecteditem changes after page.unload event

J

Justin Bartels

Hi,

I have been struggling with an unusual problem with a
dropdownlist web control for quite some time now.
Cutting straight to the problem, I am setting the
selected item in the dropdownlist using the IndexOf
method of the list's items collection (in my debugging
case this successfully sets the selectedindex to 15).
After postback, the selecteditem is always the first item
in the list, losing the selection I set previously (And
yes, enableviewstate is turned OFF for the control, and
the whole page as well). I set a breakpoint in the
page.unload event to check once more what the
selectedindex of the dropdown was, just to make sure I
was not accidentally resetting it elsewhere in code, but
the selecteditem was still set correctly (to 15) at this
point. Frustrated, I set a breakpoint in global.asax's
Global_PostRequestHandlerExecute, where the selectedindex
had mysteriously reset to 0. My question is: What could
possibly happen after page.unload and before
Global_PostRequestHandlerExecute to change the
selectedindex of this list? I have seen viewstate do
similar things but i have made certain that it is not on.

Thanks,

Justin Bartels
 
J

Jacob Yang [MSFT]

Hi Justin,

Firstly I want to thank David for his great help in this issue.

Based on my research and experience, I would like to share the following
information with you.

ASP.NET uses the Control.ViewState property provides a dictionary object
for retaining values between multiple requests for the same page, which
uses a HTML-standard hidden fields to store any page-specific information
in the page.

However, to make use of this new future, we should enable this
functionality by setting the EnableViewState property to True on the
control whose states want to be maintained.

In this case, the current state in the dropdownlist control will not be
overridden in the unload phrase until the page object tries to reload the
viewstate on the web page. Since the EnableViewState properties on all
controls on this page were disabled, what stored in viewstate is the
original information when the web page was loaded first time. That is why
each time the web page posts back, the dropdownlist control lost the
selected item before submit occurs.

To resolve the problem in this case, there are two ways,

1. Just enable the EnableViewState properties on controls whose states want
to be maintained. Nothing else we should do.

2. Get the selectedItem in each post back and re-assign it to the
dropdownlist control in the later phrase of the page lifecycle. With this
way, we may have to add a couple of code, as what we do in the traditional
ASP world.

I hope it helps.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top