dropdown list selecteditem changes after page.unload event

Discussion in 'ASP .Net' started by Justin Bartels, Oct 20, 2003.

  1. 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
     
    Justin Bartels, Oct 20, 2003
    #1
    1. Advertising

  2. Justin Bartels

    David Wier Guest

    Are you trying to only select the index to 15 on the first page load ?

    Is so, do the selection of the item in an if/then/pageispostback block.


    David Wier
    http://aspnet101.com
    http://aspexpress.com


    "Justin Bartels" <> wrote in message
    news:05df01c39712$faf03e80$...
    > 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
     
    David Wier, Oct 20, 2003
    #2
    1. Advertising

  3. 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.
     
    Jacob Yang [MSFT], Oct 21, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christina
    Replies:
    4
    Views:
    501
    Patrick.O.Ige
    Aug 2, 2005
  2. Assimalyst

    Check form before unload for changes

    Assimalyst, Aug 10, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    5,459
    Assimalyst
    Aug 11, 2005
  3. Vinay
    Replies:
    2
    Views:
    1,040
    Vinay
    Aug 18, 2005
  4. Mariella Bakker
    Replies:
    1
    Views:
    5,259
    John Timney \( MVP \)
    May 8, 2006
  5. lander
    Replies:
    5
    Views:
    2,605
    Mark Rae [MVP]
    Mar 3, 2008
Loading...

Share This Page