在asp.net2.0中,有三个dropdownlist控件分别对应数据库里面的系部表(department),专业表(speciality)和班级表(class),现在在page_load事件中已经联结上了系部名称字段;代码如下:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim sql As String
Dim conn As New SqlConnection
conn.ConnectionString = "data source=192.168.31.199;initial catalog=kaoqin manager;user id=james5;password=james5;"
sql = "select * from department"
Dim cmd As New SqlCommand(sql, conn)
conn.Open()
Dim dr As SqlDataReader
dr = cmd.ExecuteReader()
DropDownList1.AppendDataBoundItems = True
DropDownList1.Items.Add(New ListItem("--请选择--", "-1"))
DropDownList1.DataSource = dr
DropDownList1.DataValueField = "departmentNo"
DropDownList1.DataTextField = "departmentname"
DropDownList1.DataBind()
End Sub
但是在dropdownload的Index_change事件中关联专业表是出了总是,老是自动刷新,而且每刷新一次,dropdownlist控件的系部和专业名称又会自动增加,请高手帮忙看一下错到哪里;不胜感激.关联字段如下:
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Dim sql As String
Dim conn As New SqlConnection
conn.ConnectionString = "data source=192.168.31.199;initial catalog=kaoqin manager;user id=james5;password=james5;"
sql = "select * from speciality where departmentNo='" & DropDownList1.SelectedValue & "'"
Dim cmd As New SqlCommand(sql, conn)
conn.Open()
Dim dr As SqlDataReader
dr = cmd.ExecuteReader()
DropDownList5.AppendDataBoundItems = True
DropDownList5.Items.Add(New ListItem("--请选择--", "-1"))
DropDownList5.DataSource = dr
DropDownList5.DataValueField = "specialityNO"
DropDownList5.DataTextField = "specialityname"
DropDownList5.DataBind()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim sql As String
Dim conn As New SqlConnection
conn.ConnectionString = "data source=192.168.31.199;initial catalog=kaoqin manager;user id=james5;password=james5;"
sql = "select * from department"
Dim cmd As New SqlCommand(sql, conn)
conn.Open()
Dim dr As SqlDataReader
dr = cmd.ExecuteReader()
DropDownList1.AppendDataBoundItems = True
DropDownList1.Items.Add(New ListItem("--请选择--", "-1"))
DropDownList1.DataSource = dr
DropDownList1.DataValueField = "departmentNo"
DropDownList1.DataTextField = "departmentname"
DropDownList1.DataBind()
End Sub
但是在dropdownload的Index_change事件中关联专业表是出了总是,老是自动刷新,而且每刷新一次,dropdownlist控件的系部和专业名称又会自动增加,请高手帮忙看一下错到哪里;不胜感激.关联字段如下:
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Dim sql As String
Dim conn As New SqlConnection
conn.ConnectionString = "data source=192.168.31.199;initial catalog=kaoqin manager;user id=james5;password=james5;"
sql = "select * from speciality where departmentNo='" & DropDownList1.SelectedValue & "'"
Dim cmd As New SqlCommand(sql, conn)
conn.Open()
Dim dr As SqlDataReader
dr = cmd.ExecuteReader()
DropDownList5.AppendDataBoundItems = True
DropDownList5.Items.Add(New ListItem("--请选择--", "-1"))
DropDownList5.DataSource = dr
DropDownList5.DataValueField = "specialityNO"
DropDownList5.DataTextField = "specialityname"
DropDownList5.DataBind()
End Sub
Page_Load()
if(!IsPostBack)
{
//绑定数据
}
IF NOT IsPostBack Then '不知道VB语法是不是这样写的!嘿嘿
Dim sql As String
Dim conn As New SqlConnection
conn.ConnectionString = "data source=192.168.31.199;initial catalog=kaoqin manager;user id=james5;password=james5;"
sql = "select * from department"
Dim cmd As New SqlCommand(sql, conn)
conn.Open()
Dim dr As SqlDataReader
dr = cmd.ExecuteReader()
DropDownList1.AppendDataBoundItems = True
DropDownList1.Items.Add(New ListItem("--请选择--", "-1"))
DropDownList1.DataSource = dr
DropDownList1.DataValueField = "departmentNo"
DropDownList1.DataTextField = "departmentname"
DropDownList1.DataBind()
END IF
End Sub
……
end if