ASP.NET DropDownList

设置了两个下拉列表,一个为省份,一个为市。要设置为两个列表相关联,即如果选择的省份为四川省,则市的列表里显示的只能是四川省里的市。求后台代码,急、、、谢谢... 设置了两个下拉列表,一个为省份,一个为市。要设置为两个列表相关联,即如果选择的省份为四川省,则市的 列表里显示的只能是四川省里的市。
求后台代码,急、、、谢谢
展开
 我来答
496217750
推荐于2016-03-13
知道答主
回答量:2
采纳率:0%
帮助的人:3万
展开全部
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlProvince" runat="server" AutoPostBack="True"
onselectedindexchanged="ddlProvince_SelectedIndexChanged">
</asp:DropDownList>仔皮悄
<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="True"
onselectedindexchanged="ddlCity_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="ddlDist" runat="server">
</asp:DropDownList>
</div>
</form>
</body>

cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace Ganged
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}

//数据绑定
private void Bind()
{
using (SqlConnection con = new SqlConnection("server=.;database=ganged;uid=sa;pwd=sa;"))
{
con.Open();

//先初始化省握好

SqlCommand cmdPro = new SqlCommand("select ProID,ProName from T_Province", con);
SqlDataReader drPro = cmdPro.ExecuteReader();
this.ddlProvince.DataSource = drPro;
this.ddlProvince.DataTextField ="ProName";//显示念渣值
this.ddlProvince.DataValueField = "ProID"; //显示主键
this.ddlProvince.DataBind();
drPro.Close();

//初始化市

SqlCommand cmdCity = new SqlCommand("select CityID,CityName from T_City where ProID='" + this.ddlProvince.SelectedValue + "'", con);
SqlDataReader drCity = cmdCity.ExecuteReader();
this.ddlCity.DataSource = drCity;
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "CityID";
this.ddlCity.DataBind();
drCity.Close();

//初始化县

SqlCommand cmdDist = new SqlCommand("select id,DisName from T_District where CityID='" +this.ddlCity.SelectedValue + "'",con);
SqlDataReader drDist = cmdDist.ExecuteReader();
this.ddlDist.DataSource = drDist;
this.ddlDist.DataTextField = "DisName";
this.ddlDist.DataValueField = "id";
this.ddlDist.DataBind();
drDist.Close();
}

}

protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e) //DropDownList选中(省改变)产生的事件
{
string ProID = this.ddlProvince.SelectedValue;
using (SqlConnection con = new SqlConnection("server=.;database=ganged;uid=sa;pwd=sa;"))
{
con.Open();
SqlCommand cmdCity = new SqlCommand("select CityID,CityName from T_City where ProID='" + ProID + "'", con);
SqlDataReader drCity = cmdCity.ExecuteReader();
this.ddlCity.DataSource = drCity;
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "CityID";
this.ddlCity.DataBind();
drCity.Close();
con.Close();
ddlCityBind();
}
}

protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)//DropDownList选中(市改变)产生的事件
{
ddlCityBind();
}

private void ddlCityBind()
{
string CityID = this.ddlCity.SelectedValue;
using (SqlConnection con = new SqlConnection("server=.;database=ganged;uid=sa;pwd=sa;"))
{
con.Open();
SqlCommand cmdDist = new SqlCommand("select id,DisName from T_District where CityID='" + CityID + "'", con);
SqlDataReader drDist = cmdDist.ExecuteReader();
this.ddlDist.DataSource = drDist;
this.ddlDist.DataTextField = "DisName";
this.ddlDist.DataValueField = "id";
this.ddlDist.DataBind();
drDist.Close();
}
}
}
}
hu_xiao_kai
2010-11-15 · 超过38用户采纳过TA的回答
知道答主
回答量:93
采纳率:0%
帮助的人:86.2万
展开全部
你在省份的下拉列表的onchange事件里加载市的下拉列表
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
gcf98765
2010-11-14 · TA获得超过333个赞
知道小有建树答主
回答量:741
采纳率:0%
帮助的人:421万
展开全部
这东东挺简单的,自己整整就出来了。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式