Error with SelectedIndexChanged using CascadingDropDown in ASP.NET, VB and VS 2012


Joined
Aug 26, 2015
Messages
1
Reaction score
0
Hi all,

I receive an error "ddlCountry_SelectedIndexChanged is not a member of ASP.dll_vb_asp" and cannot load the page in the browser, when attempting to create Cascading Drop Downs in ASP.NET using Visual Studio 2012 and Visual Basic. Here is my code, if any one can help to figure out what is wrong in my code causing me this error. If I can get this sample working, I can get working on a project I need done.

My DDL_VB.aspx page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Ajax bind cascading country-state-city dropdownlist without
page refresh example in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<h4>
Ajax bind cascading country-state-city dropdownlist without page refresh</h4>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table>
<tr>
<td>
Select Country:
<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" DataSourceID="SqlDataSourceCountry" DataTextField="CountryName" DataValueField="CountryName">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceCountry" runat="server" ConnectionString="<%$ ConnectionStrings:CustomToolsDBConnectionString %>" SelectCommand="SELECT [CountryName] FROM [Country] ORDER BY [CountryName]"></asp:SqlDataSource>
</td>
</tr>
<tr>
<td>
Select State:
<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ddlState_SelectedIndexChanged" DataSourceID="SqlDataSourceState" DataTextField="StateName" DataValueField="StateName">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceState" runat="server" ConnectionString="<%$ ConnectionStrings:CustomToolsDBConnectionString %>" SelectCommand="SELECT [StateName] FROM [State]"></asp:SqlDataSource>
</td>
</tr>
<tr>
<td>
Select City:
<asp:DropDownList ID="ddlCity" runat="server" DataSourceID="SqlDataSourceCity" DataTextField="CityName" DataValueField="CityName">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceCity" runat="server" ConnectionString="<%$ ConnectionStrings:CustomToolsDBConnectionString %>" SelectCommand="SELECT [CityName] FROM [City]"></asp:SqlDataSource>
</td>
</tr>
</table>
<asp:UpdateProgress ID="UpdateProgress1" AssociatedUpdatePanelID="UpdatePanel1"
runat="server">
<ProgressTemplate>
<div id="ajaxloader">
Loading..</div>
</ProgressTemplate>
</asp:UpdateProgress>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlCountry" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="ddlState" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>




My DDL_VB.aspx.vb page:

Imports System.Data.SqlClient
Imports System.Data

Partial Class DDL_VB
Inherits System.Web.UI.Page
'specify your connection string here..
Public Shared strConn As String =
"Data Source=c3digpssql2;Integrated Security=true;Initial Catalog=CustomToolsDB"

Private Property ddlState As Object

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindCountryList()
End If
End Sub
'bind countries to country dropdownlist
Private Sub BindCountryList()
Try
Using sqlConn As New SqlConnection(strConn)
Using sqlCmd As New SqlCommand()
sqlCmd.CommandText = "SELECT CountryId,CountryName FROM Country"
sqlCmd.Connection = sqlConn
sqlConn.Open()
Dim da As New SqlDataAdapter(sqlCmd)
Dim dt As New DataTable()
da.Fill(dt)
ddlCountry.DataSource = dt
ddlCountry.DataValueField = "CountryId"
ddlCountry.DataTextField = "CountryName"
ddlCountry.DataBind()
sqlConn.Close()

'Adding "Please select country" option in dropdownlist
ddlCountry.Items.Insert(0, New ListItem("Please select country", "0"))

'Adding initially value to "state" and "city" dropdownlist
ddlState.Items.Insert(0, New ListItem("Please select state", "0"))
ddlCity.Items.Insert(0, New ListItem("Please select city", "0"))
End Using
End Using
Catch
End Try
End Sub

'bind states to state dropdownlist on country change event
Protected Sub ddlCountry_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Try
Using sqlConn As New SqlConnection(strConn)
Using sqlCmd As New SqlCommand()
sqlCmd.CommandText = "SELECT StateId,StateName FROM State " &
"WHERE [email protected]"
sqlCmd.Parameters.AddWithValue("@CountryId", ddlCountry.SelectedValue)
sqlCmd.Connection = sqlConn
sqlConn.Open()
Dim da As New SqlDataAdapter(sqlCmd)
Dim dt As New DataTable()
da.Fill(dt)
ddlState.DataSource = dt
ddlState.DataValueField = "StateId"
ddlState.DataTextField = "StateName"
ddlState.DataBind()
sqlConn.Close()

'Adding "Please select state" option in dropdownlist
ddlState.Items.Insert(0, New ListItem("Please select state", "0"))

'also clear city dropdownlist because we are changing country
ddlCity.Items.Clear()
ddlCity.Items.Insert(0, New ListItem("Please select city", "0"))
End Using
End Using
Catch
End Try
End Sub

'bind cities to city dropdownlist on state change event
Protected Sub ddlState_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Try
Using sqlConn As New SqlConnection(strConn)
Using sqlCmd As New SqlCommand()
sqlCmd.CommandText = "SELECT CityId,CityName FROM City " &
"WHERE [email protected]"
sqlCmd.Parameters.AddWithValue("@StateId", ddlState.SelectedValue)
sqlCmd.Connection = sqlConn
sqlConn.Open()
Dim da As New SqlDataAdapter(sqlCmd)
Dim dt As New DataTable()
da.Fill(dt)
ddlCity.DataSource = dt
ddlCity.DataValueField = "CityId"
ddlCity.DataTextField = "CityName"
ddlCity.DataBind()
sqlConn.Close()

'Adding "Please select city" option in dropdownlist
ddlCity.Items.Insert(0, New ListItem("Please select city", "0"))
End Using
End Using
Catch
End Try
End Sub

Private Function ddlCountry() As Object
Throw New NotImplementedException
End Function

Private Function ddlCity() As Object
Throw New NotImplementedException
End Function

End Class

Any help is appreciated!
 
Ad

Advertisements


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

Top