前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >DropDownList1_SelectedIndexChanged-的使用

DropDownList1_SelectedIndexChanged-的使用

作者头像
全栈程序员站长
发布2022-09-09 10:10:50
2200
发布2022-09-09 10:10:50
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

代码语言:javascript
复制
怎样使当DropDownList1改变时也改变相应的DropDownList2的值?
代码语言:javascript
复制
实现二级联动。如省市联动啊。
以下有代码。
将DropDownList1的AutoPostBack属性设为true

导入命名空间
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
 
其他回答  
  
  
  
   
   
    
     
     将DropDownList1的AutoPostBack属性设为true

在DropDownList1_SelectedIndexChanged 中
将DropDownList1.SelectedValue 赋给DropDownList2

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/161259.html原文链接:https://javaforall.cn
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档