省市之二 ------------------后台实现DropDownList联动
2009-12-21 15:54
435 查看
在网上看了很多,自己为了锻炼一下自己写了一个小的省市的二级联动。运行是可以的,请高手们多指教。
前台页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDownhoutai.aspx.cs" Inherits="DropDownhoutai" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DropdownList联动</title>
</head>
<body>
<form id="form1" runat="server">
<div>
省份:<asp:DropDownList ID="ddlProvince" runat="server" OnSelectedIndexChanged="ddlProvince_SelectedIndexChanged" AutoPostBack ="true" ></asp:DropDownList>
市:<asp:DropDownList ID="ddlCity" runat="server"></asp:DropDownList>
</div>
</form>
</body>
</html>
后台页面:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class DropDownhoutai : System.Web.UI.Page
{
static string connString = "server=localhost;database=NationalAll;uid=sa;pwd=123456;";
SqlConnection con = new SqlConnection(connString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataAdapter sda = new SqlDataAdapter("select * from Province", con);
DataSet dt = new DataSet();
sda.Fill(dt);
ddlProvince.DataSource = dt.Tables[0].DefaultView;
ddlProvince.DataTextField = "ProName";
ddlProvince.DataValueField = "ProId";
ddlProvince.DataBind();
ddlProvince.Items.Insert(0, new ListItem("--请选择--", "0"));
ddlCity.Items.Insert(0, new ListItem("--请选择--", "0"));
}
}
protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
{
BindCity(int.Parse(this.ddlProvince.SelectedItem.Value));
}
protected void BindCity(int ProID)
{
SqlCommand command = new SqlCommand("select * from City where ProID=@ProID",con);
command.Parameters.Add(new SqlParameter("@ProID", ProID));
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(ds);
ddlCity.DataSource = ds.Tables[0].DefaultView;
ddlCity.DataTextField = "CityName";
ddlCity.DataValueField = "CityID";
ddlCity.DataBind();
}
}
如果大家有更好的可以回帖,希望大家多给点意见。谢谢!!!!
前台页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDownhoutai.aspx.cs" Inherits="DropDownhoutai" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DropdownList联动</title>
</head>
<body>
<form id="form1" runat="server">
<div>
省份:<asp:DropDownList ID="ddlProvince" runat="server" OnSelectedIndexChanged="ddlProvince_SelectedIndexChanged" AutoPostBack ="true" ></asp:DropDownList>
市:<asp:DropDownList ID="ddlCity" runat="server"></asp:DropDownList>
</div>
</form>
</body>
</html>
后台页面:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class DropDownhoutai : System.Web.UI.Page
{
static string connString = "server=localhost;database=NationalAll;uid=sa;pwd=123456;";
SqlConnection con = new SqlConnection(connString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataAdapter sda = new SqlDataAdapter("select * from Province", con);
DataSet dt = new DataSet();
sda.Fill(dt);
ddlProvince.DataSource = dt.Tables[0].DefaultView;
ddlProvince.DataTextField = "ProName";
ddlProvince.DataValueField = "ProId";
ddlProvince.DataBind();
ddlProvince.Items.Insert(0, new ListItem("--请选择--", "0"));
ddlCity.Items.Insert(0, new ListItem("--请选择--", "0"));
}
}
protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
{
BindCity(int.Parse(this.ddlProvince.SelectedItem.Value));
}
protected void BindCity(int ProID)
{
SqlCommand command = new SqlCommand("select * from City where ProID=@ProID",con);
command.Parameters.Add(new SqlParameter("@ProID", ProID));
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(ds);
ddlCity.DataSource = ds.Tables[0].DefaultView;
ddlCity.DataTextField = "CityName";
ddlCity.DataValueField = "CityID";
ddlCity.DataBind();
}
}
如果大家有更好的可以回帖,希望大家多给点意见。谢谢!!!!
相关文章推荐
- 利用ajax实现省市县三级联动之二,三
- 用.net实现注册用户使用RadioButtonList,DropDownList与数据库绑定,使用DropDownList实现省市联动
- DropDownList控件实现省市联动
- DropDownList控件实现省市联动(AJAX实现)
- 用DropDownList实现Ajax无刷新省市三级联动中出现的错误
- SSH DWR省市县三级联动-后台控制,DWR配置,页面实现,源码下载
- DropDownList控件实现省市联动(AJAX实现)
- DropDownList:用Anthem写的实现省市县3级联动
- 移动端纯原生JS不依赖AJAX后台服务器实现省市县三级联动
- 移动端纯原生JS不依赖ajax后台服务器实现省市县三级联动
- ASP.NET用DropDownList控件来实现省市联动--C#,附SQL2000语句
- DropDownList用JQuery实现Ajax三级联动
- 使用Ajax实现DropDownList和ListBox的联动以及两个ListBox之间数据的移动
- 如何实现无刷新的DropdownList联动效果 (转贴)
- vue 实现省市区三级联动
- 如何实现无刷新的DropdownList联动效果[转]
- [导入]实现无刷新DropDownList联动效果
- Ajax实现省市二级联动(解析XML内容)
- 写了一个用AJAX(ASP.NET2.0)实现的DropDownList三级联动
- JQuery EasyUI Combobox 实现省市二级联动菜单