DropDownList实现二级联动
导入命名空间
Imports System.Data
Imports System.Data.SqlClient
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
'Dim mydb As DB
If Not IsPostBack Then
'绑定省
'Dim connstr As String = "server=.;database=mag_Web;User ID=sa;password=;"
'Dim myconn As SqlConnection
'myconn = New SqlConnection(connstr)
'myconn = mydb.GetSqlconn()
Dim myconn As SqlConnection = myconnection.GetSqlconn
myconn.Open()
Dim mycommand As New SqlCommand("select * from province", myconn)
Dim myreader As SqlDataReader
myreader = mycommand.ExecuteReader()
DropDownList1.DataSource = myreader
DropDownList1.DataTextField = "proName"
DropDownList1.DataValueField = "proID"
DropDownList1.DataBind()
myreader.Close()
'绑定市
Dim mycity As New SqlCommand("select * from city where proID=" + DropDownList1.SelectedValue, myconn)
Dim myreader1 As SqlDataReader
myreader1 = mycity.ExecuteReader()
DropDownList2.DataSource = myreader1
DropDownList2.DataTextField = "cityName"
DropDownList2.DataValueField = "cityID"
DropDownList2.DataBind()
myreader1.Close()
myconn.Close()
End If
'Dim myconn As SqlConnection
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
txtUserName.Text = ""
txtPassword1.Text = ""
txtPassword2.Text = ""
RequiredFieldValidator1.Visible = False And RequiredFieldValidator2.Visible = False
'CompareValidator1.Visible = False
End Sub
Private Sub btnSumbit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSumbit.Click
'Dim connstr As String = "server=.;database=mag_Web;User ID=sa;password=;"
Dim myconn As SqlConnection = myconnection.GetSqlconn
'myconn = New SqlConnection(connstr)
myconn.Open()
Dim mycommand As New SqlCommand("insert into mag_Users(UserName,UserPassword,UserProvince,UserCity) values('" + txtUserName.Text + "','" + txtPassword1.Text + "','" + DropDownList1.SelectedItem.Text + "','" + DropDownList2.SelectedItem.Text + "')")
mycommand.Connection = myconn
mycommand.ExecuteNonQuery()
myconn.Close()
Response.Redirect("Login2.aspx")
End Sub
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Dim proID As String
proID = DropDownList1.SelectedValue
'Dim connstr As String = "server=.;database=mag_Web;User ID=sa;password=;"
Dim myconn As SqlConnection = myconnection.GetSqlconn
'myconn = New SqlConnection(connstr)
'myconn = mydb.GetSqlconn()
myconn.Open()
Dim mycommand As New SqlCommand("select * from city where proID=" + proID, myconn)
Dim myreader As SqlDataReader
myreader = mycommand.ExecuteReader()
DropDownList2.DataSource = myreader
DropDownList2.DataTextField = "cityName"
DropDownList2.DataValueField = "cityID"
DropDownList2.DataBind()
myreader.Close()
myconn.Close()
End Sub
Imports System.Data
Imports System.Data.SqlClient
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
'Dim mydb As DB
If Not IsPostBack Then
'绑定省
'Dim connstr As String = "server=.;database=mag_Web;User ID=sa;password=;"
'Dim myconn As SqlConnection
'myconn = New SqlConnection(connstr)
'myconn = mydb.GetSqlconn()
Dim myconn As SqlConnection = myconnection.GetSqlconn
myconn.Open()
Dim mycommand As New SqlCommand("select * from province", myconn)
Dim myreader As SqlDataReader
myreader = mycommand.ExecuteReader()
DropDownList1.DataSource = myreader
DropDownList1.DataTextField = "proName"
DropDownList1.DataValueField = "proID"
DropDownList1.DataBind()
myreader.Close()
'绑定市
Dim mycity As New SqlCommand("select * from city where proID=" + DropDownList1.SelectedValue, myconn)
Dim myreader1 As SqlDataReader
myreader1 = mycity.ExecuteReader()
DropDownList2.DataSource = myreader1
DropDownList2.DataTextField = "cityName"
DropDownList2.DataValueField = "cityID"
DropDownList2.DataBind()
myreader1.Close()
myconn.Close()
End If
'Dim myconn As SqlConnection
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
txtUserName.Text = ""
txtPassword1.Text = ""
txtPassword2.Text = ""
RequiredFieldValidator1.Visible = False And RequiredFieldValidator2.Visible = False
'CompareValidator1.Visible = False
End Sub
Private Sub btnSumbit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSumbit.Click
'Dim connstr As String = "server=.;database=mag_Web;User ID=sa;password=;"
Dim myconn As SqlConnection = myconnection.GetSqlconn
'myconn = New SqlConnection(connstr)
myconn.Open()
Dim mycommand As New SqlCommand("insert into mag_Users(UserName,UserPassword,UserProvince,UserCity) values('" + txtUserName.Text + "','" + txtPassword1.Text + "','" + DropDownList1.SelectedItem.Text + "','" + DropDownList2.SelectedItem.Text + "')")
mycommand.Connection = myconn
mycommand.ExecuteNonQuery()
myconn.Close()
Response.Redirect("Login2.aspx")
End Sub
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Dim proID As String
proID = DropDownList1.SelectedValue
'Dim connstr As String = "server=.;database=mag_Web;User ID=sa;password=;"
Dim myconn As SqlConnection = myconnection.GetSqlconn
'myconn = New SqlConnection(connstr)
'myconn = mydb.GetSqlconn()
myconn.Open()
Dim mycommand As New SqlCommand("select * from city where proID=" + proID, myconn)
Dim myreader As SqlDataReader
myreader = mycommand.ExecuteReader()
DropDownList2.DataSource = myreader
DropDownList2.DataTextField = "cityName"
DropDownList2.DataValueField = "cityID"
DropDownList2.DataBind()
myreader.Close()
myconn.Close()
End Sub
posted on 2007-12-24 08:42 Peter wang的学习.NET之路 阅读(445) 评论(0) 编辑 收藏 举报