ASP.NET DropDownList
设置了两个下拉列表,一个为省份,一个为市。要设置为两个列表相关联,即如果选择的省份为四川省,则市的列表里显示的只能是四川省里的市。求后台代码,急、、、谢谢...
设置了两个下拉列表,一个为省份,一个为市。要设置为两个列表相关联,即如果选择的省份为四川省,则市的 列表里显示的只能是四川省里的市。
求后台代码,急、、、谢谢 展开
求后台代码,急、、、谢谢 展开
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();
}
}
}
}
<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();
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询