Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropdownlist OnSelectedIndexChanged not firing

Status
Not open for further replies.

meckeard

Programmer
Aug 17, 2001
619
US
Hi,

I can't get the OnSelectedIndexChanged event to fire on a dropdownlist control.

Here is how my page is designed:

When my page loads, I have a dropdownlist with a list of states. When the user changes the dropdownlist, the AutoPostBack fires and runs a sub that loads & shows a second dropdownlist that shows cities for the state selected.

The problem occurs when I make a selection from the city dropdownlist. Nothing happens.

Here is my dropdownlist:

<form runat=&quot;server&quot;>
<asp:DropDownList ID=&quot;drpCityList&quot; onfiltered=&quot;GetATMList&quot; runat=&quot;server&quot;>
</asp:DropDownList>
</form>

Here is the code in a codebehind page:

Public Sub GetATMList(sender As Object, e As System.EventArgs)
'This sub loads the ATM's for the city selected.

Dim objConn As SqlConnection
Dim objSQLCommand As SqlCommand
Dim objDataReader as SqlDataReader
Dim strCity as String = drpCityList.SelectedItem.Text

if UCase(strCity) <> &quot;NOW SELECT A CITY&quot; then

'Set up our connection object and SQL command object.
objConn = New SqlConnection(application(&quot;WSFSQLConnString&quot;))
objSQLCommand = New SqlCommand(&quot;AD_s_GetATMByCity&quot;, objConn)
'Set the stored procedure.
objSQLCommand.CommandType = CommandType.StoredProcedure
'Add parameters.
objSQLCommand.Parameters.Add(&quot;@City&quot;, strCity)
'Open the db connection & Execute Command
objConn.Open()

'Execute the procedure.
objDataReader = objSQLCommand.ExecuteReader()
'Now bind the datareader to the repeater.
grdATMs.DataSource = objDataReader
grdATMs.DataBind()
'Close the DB connection.
objConn.Close()

end if

end Sub 'GetATMList

I tried to change the sub to set the value to a label, thinking maybe there was something wrong with the value of the dropdownlist. But it didn't work either.

What am I doing wrong?

Thanks,
Mark
 
meck -

Cut and pasted you code into a simple page and when I changed:

onfiltered=&quot;GetATMList&quot;

to

OnSelectedIndexChanged=&quot;GetATMList&quot;

it worked. I also included a AutoPostBack=&quot;true&quot;

Seems the onfiltered wasn't cutting it - although I like that approach. I found it interesting too that by using the OnSelectedIndexChanged handle for directly referencing a function instead of its accompanying handle, it worked fine.

This is the entirety of my page:

<%@ Page Language=&quot;vb&quot; Debug=&quot;true&quot; %>
<%@ Import Namespace = &quot;Microsoft.VisualBasic&quot; %>
<%@Import Namespace = &quot;System&quot;%>
<script runat=&quot;server&quot;>
Public Sub GetATMList(sender As Object, e As System.EventArgs)
lblTest.Text = &quot;Fire away&quot;
End Sub
</script>
<HTML>
<HEAD>
<title>Test</title>
</HEAD>
<body>
<form runat=&quot;server&quot;>
<asp:DropDownList ID=&quot;drpCityList&quot; OnSelectedIndexChanged=&quot;GetATMList&quot; AutoPostBack=&quot;true&quot; runat=&quot;server&quot;>
<asp:ListItem Value=&quot;01&quot;>A</asp:ListItem>
<asp:ListItem Value=&quot;02&quot;>B</asp:ListItem>
<asp:ListItem Value=&quot;03&quot;>C</asp:ListItem>
</asp:DropDownList>
<asp:Label id=&quot;lblTest&quot; runat=&quot;server&quot;/>
</form>
</body>
</HTML>
 
Isadore,

Opps, sorry. I had it that way from the start. I did a View -> Source and that is what is sent to the browser. Sorry!

Let me take a step back and describe exactly what I am trying to do.

When the page is first loaded, I bind a dropdownlist with all states. When the user makes a state selection, I bind & show the second dropdownlist for all cities for the selected state (control in question). When the user selects a city, I want to run a sub that binds & shows a datagrid with some results based on the city selection.

With that said, I think that my problem is in the script part of the page. How do I code to decide to show the city dropdownlist, then show the datagrid only after the city dropdownlist has been changed?

Thanks,
Mark





 
meck --

I've only been at .net for a year now, doing other things a well, so not a guru like some of these guys -- although I admit I learned much from them (jfrost, custom, Paul, et al.)

In your case what I would do is simply post back after a City is selected, and from the OnSelectedIndexChanged handle I would add code to bind the datagrid and then make the datagrid visible. For example,

1. User selects a city, post back.

2. In the OnSelectedIndexChanged handle I would have something like
...
GetCtyDetail(ddCty.SelectedItem.Index)
dgMyGrid.Visible = &quot;True&quot;
...
Sub GetSites (intID As Integer)
'bind grid
End Sub

In the SQL of the DataGrid you can use of course the value from the Cty dropdown - something along these lines.

Anyway, a simple approach (and I have not scrutinized your code above). Someone may come by and add some further insight, what initially caught my eye was your use of the &quot;filert&quot; statement.
 
meck -

better, just ref the dropdown in the dgrid bind SQL and you don't need to pass the variable --
 
All,

I got it to work. I needed to play around with the postback a little to detect the seletion of the city.

Thanks all for the help & insight.

I am still stuck on another problem, posted here:

thread855-690359 anyone gets bored, take a look and let me know if you can help.

Thanks,
Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top