创建数据库表 province 和 city ,使用 dropdownlist 的绑定,实现省市列表级联。
2012-12-10 09:14
309 查看
aspx:
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
Xonselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</div>
</form>
后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using daorudaochuLianXi;
using System.Data.SqlClient;
namespace 简单绑定
{
public partial class 省市级联数据绑定 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataReader reader = sqlhelper.datareader("select * from province");
if (reader.HasRows)
{
//读取表中的省份
DropDownList1.DataSource = reader;
//将表中的省份名称绑定到控件的字段名上
DropDownList1.DataTextField = "province";
DropDownList1.DataValueField = "id";
//将表中的id绑定到控件的value上
DropDownList1.DataBind();
DropDownList1.SelectedIndex = 0;
//以下是初始化城市列表用的
string value = DropDownList1.SelectedValue;
SqlDataReader reader2 = sqlhelper.datareader("select * from city where
pid=@pid", new SqlParameter("@pid", value));
if (reader2.HasRows)
{
DropDownList2.DataSource = reader2;
DropDownList2.DataTextField = "city";
DropDownList2.DataValueField = "id";
DropDownList2.DataBind();
}
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string value=DropDownList1.SelectedValue;
SqlDataReader reader = sqlhelper.datareader("select * from city where
pid=@pid", new SqlParameter("@pid", value));
if (reader.HasRows)
{
DropDownList2.DataSource = reader;
DropDownList2.DataTextField = "city";
DropDownList2.DataValueField = "id";
DropDownList2.DataBind();
}
}
}
}
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
Xonselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</div>
</form>
后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using daorudaochuLianXi;
using System.Data.SqlClient;
namespace 简单绑定
{
public partial class 省市级联数据绑定 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataReader reader = sqlhelper.datareader("select * from province");
if (reader.HasRows)
{
//读取表中的省份
DropDownList1.DataSource = reader;
//将表中的省份名称绑定到控件的字段名上
DropDownList1.DataTextField = "province";
DropDownList1.DataValueField = "id";
//将表中的id绑定到控件的value上
DropDownList1.DataBind();
DropDownList1.SelectedIndex = 0;
//以下是初始化城市列表用的
string value = DropDownList1.SelectedValue;
SqlDataReader reader2 = sqlhelper.datareader("select * from city where
pid=@pid", new SqlParameter("@pid", value));
if (reader2.HasRows)
{
DropDownList2.DataSource = reader2;
DropDownList2.DataTextField = "city";
DropDownList2.DataValueField = "id";
DropDownList2.DataBind();
}
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string value=DropDownList1.SelectedValue;
SqlDataReader reader = sqlhelper.datareader("select * from city where
pid=@pid", new SqlParameter("@pid", value));
if (reader.HasRows)
{
DropDownList2.DataSource = reader;
DropDownList2.DataTextField = "city";
DropDownList2.DataValueField = "id";
DropDownList2.DataBind();
}
}
}
}
相关文章推荐
- 创建数据库表 province 和 city ,使用 dropdownlist 的绑定,实现省市列表级联。
- 使用dropdownlist的绑定_实现省市列表级联
- 使用dropdownlist,实现省市列表级联
- 牛腩购物15:中国省市县数据库,DropDownList 绑定数据库 下拉选择省市,购物车的实现,使用sqlserver 2008 导入access数据库,不能在 DropDownList 中选择多个项
- 用.net实现注册用户使用RadioButtonList,DropDownList与数据库绑定,使用DropDownList实现省市联动
- ASP.NET中DropDownList控件的简单使用--DDL绑定数据库表中值,选择DDL中值直接检索数据
- 使用Ext中的Grid,Form,Dialog来实现列表,创建,更新,删除,查找和分页功能
- 使用navicat8实现创建数据库和导入数据[图文]
- 如何解决FormView中实现DropDownList连动选择时出现 "Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用" 的错误
- 使用postgre数据库实现树形结构表的子-父级迭代查询,通过级联菜单简单举例
- javascript实现dom动态创建省市纵向列表菜单的方法
- Jquery实现简单的级联下拉列表 (省市)
- Android采用ListView实现数据列表显示2-使用SimpleAdapter进行数据绑定
- 使用postgre数据库实现树形结构表的子-父级迭代查询,通过级联菜单简单举例
- KendoUi中KendoDropDownList控件的使用——三级级联模块的实现
- ASP.NET 中 DropDownList+DataList(数据列表)的使用前台绑定
- 使用spinner实现省市二级级联
- 使用Ext的Grid,Form,Dialog来实现分页列表,创建,修改,删除功能
- 使用 MySQL Query Browse //实现创建数据库+导入数据+MapleStory全体怪物的掉宝率调整等(图文)
- 使用VS2013创建数据库项目实现MSSQL正则方法