468,738 Members | 2,324 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,738 developers. It's quick & easy.

AutoPostBack + SelectedIndexChanged event bad behavior in ASP.NET

rdb
VB.NET web program with a webform w/2 dropdownlistboxes, set to AutoPostBack
TRUE, selection in either dropdown fires the SelectedIndexChanged events
correctly UNTIL I navigate to second webform in the same project, THEN use
the Back button to return to first webform; then changing the selection of
the second dropdownlistbox fires the SelectedIndexChanged event for the FIRST
dropdownlistbox.

I realize that a) using an eventhandler for both events, and b) setting
AutoPostBack to FALSE may resolve this issue; HOWEVER, this appears to be a
big fat .NET Framework bug in my VB.NET application. Anyone experience this
bad behavior? The application that demonstrates this is available upon
request, but code is only

Private Sub Page_Load...
If Not (Me.IsPostBack) Then

DropDownList1.Items.Add("DropDownList1 Line 1 Selected")
DropDownList1.Items.Add("DropDownList1 Line 2 Selected - Go To
Webform2")
DropDownList1.Items.Add("DropDownList1 Line 3 Selected")

DropDownList2.Items.Add("DropDownList2 Line 1 Selected")
DropDownList2.Items.Add("DropDownList2 Line 2 Selected")
DropDownList2.Items.Add("DropDownList2 Line 3 Selected")
EndIf
End Sub

Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
DropDownList1.SelectedIndexChanged

If DropDownList1.SelectedItem.Text = "DropDownList1 Line 1 Selected"
Then
Label3.Text = DropDownList1.SelectedItem.Text()
End If
If DropDownList1.SelectedItem.Text = "DropDownList1 Line 2 Selected
- Go To Webform2" Then
Label3.Text = DropDownList1.SelectedItem.Text()

Response.Redirect("http://localhost/VB_WebNavigation/Webform2.aspx")
End If
If DropDownList1.SelectedItem.Text = "DropDownList1 Line 3 Selected"
Then
Label3.Text = DropDownList1.SelectedItem.Text()
End If
End Sub

Private Sub DropDownList2_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
DropDownList2.SelectedIndexChanged
REM event to process the selections

If DropDownList2.SelectedItem.Text = "DropDownList2 Line 1 Selected"
Then
Label3.Text = DropDownList2.SelectedItem.Text()
End If
If DropDownList2.SelectedItem.Text = "DropDownList2 Line 2 Selected"
Then
Label3.Text = DropDownList2.SelectedItem.Text()
End If
If DropDownList2.SelectedItem.Text = "DropDownList2 Line 3 Selected"
Then
Label3.Text = DropDownList2.SelectedItem.Text()
End If
End Sub

Thanks! --
Roger Briggs
ASNA Technical Support Analyst
Nov 19 '05 #1
2 5279
This is just wishful thinking on your part. Once the user starts
monkeying with the back button, all bets are off. It has always been
this way for CGI programming, and ASP.NET has neither improved nor
broken it.

ASP.NET, and all server side technologies run on the web server.
Browser page caching happens on the browser. There is no way around
this short of instructing the browser not to cache your pages. There
are hacks and workarounds, such as javascript history.go(1);'s at the
top of every page. But for the most part, you just have to build your
application to deal with the fact that it lives in a browser that
contains a Back button.

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com

Nov 19 '05 #2
Just to shed some lihgt on what's happening...

The 1st dropdownlist isn't firing because it's being posted back (clearly,
the 2nd one is doing the postback). Instead, it's firing because it's value
has changed from the initial value, which is the true meaning on
SelectedIndexChanged.

Think of it this way,
AutoPostBack=true isn't what makes the event handler fire, it's the fact
that the value has changed. That's why if you set AutoPostBack=false and
have a button postback, the SelectedIndexChanged handler will still fire IF
the value changed.

Notice when you hit back, the value in the 1st dropdown is still the 2nd
item, which means it's index did changed from the initial value (which was
the 1st item).

Simply putting:
<script language="javascript">
document.getElementById("DropDownList1").selectedI ndex = 0;
</script>

in your page will solve the problem...though I wouldn't consider it the
solution. The solution is that you understand what's really going on (it
isn't a bug), and why the simple solution above works...it might not work in
all cases and it might have other sideffects...I'm only telling you about it
so you can get an understanding of what's happening.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"jasonkester" <ja*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
This is just wishful thinking on your part. Once the user starts
monkeying with the back button, all bets are off. It has always been
this way for CGI programming, and ASP.NET has neither improved nor
broken it.

ASP.NET, and all server side technologies run on the web server.
Browser page caching happens on the browser. There is no way around
this short of instructing the browser not to cache your pages. There
are hacks and workarounds, such as javascript history.go(1);'s at the
top of every page. But for the most part, you just have to build your
application to deal with the fact that it lives in a browser that
contains a Back button.

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com

Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Lloyd Sheen | last post: by
4 posts views Thread by Scott M. | last post: by
reply views Thread by Scott | last post: by
3 posts views Thread by Dabbler | last post: by
2 posts views Thread by Tom Edelbrok | last post: by
1 post views Thread by CARIGAR | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.