wrong option selected in dropdownlist

  • Thread starter Thread starter sam
  • Start date Start date
S

sam

Hi,
I have a bound dropdownlist with an event handler
OnSelectedIndexChanged.

If I select a value I'm redirected to another page. I come back using
the browser's back button, and the option is still selected. So far so
good. But the very weird thing is that if I look at the source code :

<option selected="selected" value="13808">DataPage6</option>
<option value="13805">DataPage5</option>
<option value="13489">DataPage4</option>

and yet, I've got the second item (DataPage5) selected in the list.
So what's going on there ????

Thanks for helping.
Sam
 
Hi,

Would you make it bit clear .. what is actual problem....
What you want?
What is happening?

Thanks..
 
Ok, watch that :

In the load event:

If Not IsPostBack Then
'get data from database and bind dropdownlist
End If

in the selectedindexchanged event handler:

Public Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As
EventArgs)
Dim q As New Query(CType(sender,
DropDownList).SelectedItem.Text, CInt(CType(sender,
DropDownList).SelectedItem.Value))
Session("CurrentQuery") = q
Response.Redirect("Flow.aspx")
End Sub

Then when I browse back from Flow.aspx to my page, the selecteditem in
dropdownlist is still the one I had selected, which is expected, BUT
the html code if I do 'View Source' is:

<option selected="selected" value="13808">DataPage6</option>
<option value="13805">DataPage5</option>
<option value="13489">DataPage4</option>

even if the selecteditem is DataPage5 or DataPage4. It doesn't make
sense to me.
Can you help ?
 
Normally, the Back button doesn't cause the page to re-load. It gets it from
a local cache. You have to take special care to get the page to reload.

Eliyahu
 
I know that. But can you help me a bit further please. What 'special
care' should I take ?
 
I didn't do it myself. If you google on something like

"back button" reload page asp.net

you will find a lot of suggestions.

Eliyahu
 
still.... i found that really strange. Did you read my first post ??
The fact that the selected option in the html source code (generated
html from asp server control) does not match the one selected by the
user, is rather strange...

the only way i found on the web is to disable caching, but this is
quite inefficient. I'm quite amazed that there is no simple way to
achieve what I want, without making any compromises such as disabling
caching.
 
still.... i found that really strange. Did you read my first post ??
The fact that the selected option in the html source code (generated
html from asp server control) does not match the one selected by the
user, is rather strange...

That's the whole point! You are dealing with the following scenario:

1. User loads the page from the server. This selects the option specified in
the code.

2. User makes another option selected.

3. User moves out from the page. The page remains cached in the browser
cache with the option selected by the user.

4. User hits the "Back" button. The browsers gets the page from the browser
cache with the option selected by the user. It DOES NOT load the page from
the server.

Eliyahu
 
so?
I know that, all I'm saying is that when the use selected the value in
the list this triggers a postback, therefore I would expect the
selected option to change from 0 to whatever index was selected in the
list .... no ?
 
Didn't you say you redirected to another page? Your postback doesn't result
in rendering the page again, does it?

Eliyahu
 
correct. But here's my understanding:

1. user select a value
2. postback
3. selectedindexchanged event handler called
4. user redirected
5. user browses back to the page.

On 5. the page is loaded from cache, i agree. But to me it is clear
that the page in cache should be the one that was created in 2. and it
should know about the new selected index.

If not, and if the only way to achieve what i want is through forcing
reloading by not caching the page, then can you help me for that ? I've
tried the following code to force expiration of the page, but it only
works with IE, it does not work with Mozilla or Netscape :

<%
Response.CacheControl = "no-cache"
Response.AddHeader("Pragma", "no-cache")
Response.ExpiresAbsolute = DateTime.Now.Date
Response.Expires = -1

%>
 
How do you redirect?

I can't advice you on other browsers. So far I have being blessed with the
customers who are happy with IE only. Ask this question in a separate
thread.

Eliyahu
 
How do you redirect?

Just a simple Response.Redirect("anotherpage.aspx") in the
SelectedIndexChanged event handler of my dropdownlist.
I can't advice you on other browsers. So far I have being blessed with the
customers who are happy with IE only. Ask this question in a separate
thread.

lucky you...
 
On your postback you are calling Response.Redirect.

This means the browser gets sent an HTTP header with a redirect
instruction and no body rather than "the page that was created at step
2." No HTML was ever rendered and sent to the browser at all, just the
redirect header.
 
You could use an "Expires" header (Response.Expires or
Response.AppendHeader) to force page reload on browser back. You'd
also need to cache the dropdown selected value in Session so you could
check for and restore it on the reload.
 

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

Back
Top