当需要一堆下拉框进行互联时,详见下文
例:当选择完医院时,自动对应该医院科室(本文最后附主要代码)
-
首先写出一个下拉框的代码(记为
dropdownlist1
)
详情请见上一篇博客dropdownlis怎么使用 -
将dropdownlist1的属性AutoPostBack改为True
(千万别忘了,呜呜呜我就是忘了,然后研究了好久)
-
在前端双击
dropdownlist1
,然后在DropDownList1_SelectedIndexChange
中写dropdownlist2的控制部分,以此类推在DropDownList2_SelectedIndexChange
中写dropdownlist3的控制部分 -
遇到的问题
(1)查询到的数据有重复怎么办
其实这应该算是数据库知识了(但我当时忘记了),举个例子,我想在DropDownList2
中展示以下第一列内容,但是you重复怎么办,只需要在select语句中加入DISTINCT
就好啦。
代码如下
string queryRecord1;
queryRecord1 = "SELECT DISTINCT 列名 FROM 表名";
(2)dropdownlist1
在选择完之后会立即变回默认值
这时就需要将你的dropdownlist1
写在以下函数中,就好啦,注意要引用哦。
if(!IsPostBack)
{
你的dropdownlist1;
}
- 最后附上开篇程序代码
前端(工具箱拖进来就好,别忘了改属性AutoPostBack
)
<div class="col-md-12" >
<asp:Label ID="Label1" runat="server" Text="请选择医院" style="font-size: large"></asp:Label>  
<asp:DropDownList ID="DropDownList1" runat="server" Height="35px" Width="240px" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
<br/><br/>
</div>
<div class="col-md-12"
<asp:Label ID="Label2" runat="server" Text="请选择科室" style="font-size: large"></asp:Label>  
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"></asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList>
</div>
后端(我用的VS自带SQL数据库,不会的可见VS连接自带SQL数据库)
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace 软件开发课程设计
{
public partial class 挂号 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) //防止返回默认值
{
cleckhospital();
}
}
protected void cleckhospital()
{
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(constr);
conn.Open();
string queryRecord;
queryRecord = "SELECT hosname FROM hospital_log ";
SqlCommand com;
com = new SqlCommand(queryRecord, conn);
SqlDataReader sqlreader;
sqlreader = com.ExecuteReader();
this.DropDownList1.DataSource = sqlreader;
this.DropDownList1.DataTextField = "hosname";
this.DropDownList1.DataValueField = "hosname";
this.DropDownList1.DataBind();
this.DropDownList1.Items.Insert(0, new ListItem("<--请选择医院-->", "0"));//设置默认值
this.DropDownList2.Items.Insert(0, new ListItem("<--请选择分类-->", ""));
this.DropDownList3.Items.Insert(0, new ListItem("<--请选择科室-->", ""));
sqlreader.Close();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(constr);
conn.Open();
string queryRecord1;
queryRecord1 = "SELECT DISTINCT lall FROM wuxi_people";
SqlCommand com1;
com1 = new SqlCommand(queryRecord1, conn);
SqlDataReader sqlreader1;
sqlreader1 = com1.ExecuteReader();
this.DropDownList2.DataSource = sqlreader1;
this.DropDownList2.DataTextField = "lall";
this.DropDownList2.DataValueField = "lall";
this.DropDownList2.DataBind();
sqlreader1.Close();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(constr);
conn.Open();
string queryRecord1;
queryRecord1 = "SELECT branch FROM wuxi_people where lall=N'" + DropDownList2.SelectedItem.Text + "' ;";
SqlCommand com1;
com1 = new SqlCommand(queryRecord1, conn);
SqlDataReader sqlreader1;
sqlreader1 = com1.ExecuteReader();
this.DropDownList3.DataSource = sqlreader1;
this.DropDownList3.DataTextField = "branch";
this.DropDownList3.DataValueField = "branch";
this.DropDownList3.DataBind();
sqlreader1.Close();
}
}
}