Dropdown list and browser back button issue

Posted on 2006-04-06
Last Modified: 2008-08-04
I am working on an ASP.NET VB Web project.  I have a page with a dropdownlist with items (let's say A, B and C).  B is assigned as default selected item.  when a user selects a different item (let's say C) on the dropdownlist it triggers the selectedindexonchange event and do a postback. It all works very well but then when a user clicks on the BACK button of the browser the item the user previsouly selected is highlighted (which is C) but if I look at the code behind using View source from the browser "B" is still marked as SELECTED.  The problem is, if I select B now the selectedindexonchange event won't fire.... how can I solve this problem?  I tried not to cache the page but it still not solve the "SELECTED" issue....
Please help.. very urgent...
Question by:waiman
    LVL 12

    Accepted Solution

    The issue is when you click "back" button, it will display the cache version of the page. That's why B is shown "selected" in the source code and that causes the selectedindexonchange event won't fire when you select "B" in the drop list (even the dropdownlist highlighted "C").

    try add this to your page_load,
            Response.CacheControl = "no-cache"
    and now when user clicks "C" and then click "back" button, it will show "B" as highlighted instead of "C".
    LVL 7

    Assisted Solution

    Seems to me, its browser cache problem
    Add the following two lines to your .aspx page and see the difference.

                <meta http-equiv="Expires" content="-1">
                <meta http-equiv="Pragma" content="no-cache">


    Author Comment

    Thanks for the advices... but things still not quite right....
    I put the meta tags and the response.cachecontrol in the code......and that's what happening...
    If the default selected item is B (out of A, B, C) in the dropdown list, then when I click on 'C', it kicks off a postback.  Then when I click on the BACK button of the browser, 'B' is highlighted again.  I could still click on A or C and it still works.... occassionally I get the "Warning: Page has expired" from IE in some pages which I am NOT happy with....

    However, the ideal situation will be: default item is 'B', when I click on 'C', it kicks off a postback, when I click on the BACK button on the browser, 'C' should be highlighted, and I can click on 'B' again and do another postback.  

    The problem I described in my previous posting was: 'B' is always marked as 'SELECTED' behind the scene even 'C' is appeared to be highlighted in the browser, and the postback will not happen (or did weired things) when I click on 'B' again.
    LVL 1

    Assisted Solution

    The back button is really difficult to handle.  Have you tried turning smartnavigation on?  It sometimes interferes with javascript, but you can give it a try.
    LVL 12

    Assisted Solution

    Use ONLY Response.CacheControl = "no-cache" and page won't get expired.

    The flow is:
    1st - B is selected by default > if user selects "C", page will post back
    2nd - Now "C" is highlighted > click "button" and naturally it will go back to previous page which is (1st)

    If user clicks "back" button and select "B" and you want page to post back, they can directly select "B" in 2nd stage.

    Author Comment

    I ended up have to rewrite the onchange event using attributes.add.  The viewstate was absolutely screwed but I did not have time to investigate what really went wrong.  It fired the onselectedindexchange on the first time in debug mode and it did not happen the second time.... no idea... anyway the problem was bypassed ......

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now