ASPのDropDownListを使用して、国、州、および都市をバインドしようとしています。VB.NETを使用したNET。問題は、国を選択すると、その国に関連する州のリストが表示されないことです。私はデータベースにMSAccess2003を使用しています。これが私のテーブルとコードです
Country Table
テーブルの国を作成します(CountryID Int Primary Key、CountryName Varchar(30))
CountryStateテーブル
テーブルcountryStateを作成します(StateID Int主キー、CountryID Int外部キー参照Country(CountryID)、State Varchar(30))
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
cnnOLEDB.ConnectionString = strConnectionString
If Not IsPostBack Then
Bind_ddlCountry()
End If
End Sub
Public Sub Bind_ddlCountry()
Dim cnnOLEDB As New OleDbConnection(strConnectionString)
cnnOLEDB.Open()
Dim cmd As New OleDbCommand("SELECT CountryID,CountryName FROM Country", cnnOLEDB)
Dim dr As OleDbDataReader = cmd.ExecuteReader()
ddlcountry.DataSource = dr
ddlcountry.Items.Clear()
ddlcountry.Items.Add("--Please Select country--")
ddlcountry.DataTextField = "CountryName"
ddlcountry.DataValueField = "CountryID"
ddlcountry.DataBind()
cnnOLEDB.Close()
End Sub
Public Sub Bind_ddlState()
Dim cnnOLEDB As New OleDbConnection(strConnectionString)
cnnOLEDB.Open()
'Dim cmd As New OleDbCommand("SELECT StateID, State FROM CountryState", cnnOLEDB)
Dim cmd As New OleDbCommand("SELECT StateID, State FROM CountryState WHERE CountryID='" & ddlcountry.SelectedValue & "'", cnnOLEDB)
Dim dr As OleDbDataReader = cmd.ExecuteReader()
ddlstate.DataSource = dr
ddlstate.Items.Clear()
ddlstate.Items.Add("--Please Select state--")
ddlstate.DataTextField = "State"
ddlstate.DataValueField = "StateID"
ddlstate.DataBind()
cnnOLEDB.Close()
End Sub
Protected Sub ddlcountry_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlcountry.SelectedIndexChanged
Bind_ddlState()
End Sub