AutoPostBack + SelectedIndexChanged event bad behavior in ASP.NET

G

Guest

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
 
J

jasonkester

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
 
K

Karl Seguin

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").selectedIndex = 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!)
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top