访问Access数据库实现DropDownList二级异步联动(ASP.NET WebForm开发方式)
来源:互联网 发布:淘宝卖qq号怎么卖 编辑:程序博客网 时间:2024/06/06 09:54
前台代码:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> 市<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True" ></asp:DropDownList> 县<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"></asp:DropDownList><br /> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" /> </Triggers> </asp:UpdatePanel>
关键点:UpdatePanelUpdateMode=”Conditional”;
AsyncPostBackTrigger 的 EventName=”SelectedIndexChanged”;
后台代码:
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { //第一次加载页面,将DropDownList1和DropDownList2都设置为“--请选择--”,并将DropDownList2设置为不可用 DropDownList1.Items.Add("--请选择--"); DropDownList2.Items.Add("--请选择--"); DropDownList2.Enabled = false; //数据库地址 string dbPath = Server.MapPath(@"/App_Data/change.mdb"); //sql查询语句 string sqlStr = "SELECT DISTINCT [city] FROM [Region]"; //绑定数据 BindDropDownList(dbPath, sqlStr, DropDownList1); } if (DropDownList1.SelectedIndex == 0) { DropDownList2.Items.Add("--请选择--"); } } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { DropDownList2.Enabled = true; DropDownList2.Items.Clear(); if (DropDownList1.SelectedIndex == 0) { DropDownList2.Items.Add("--请选择--"); DropDownList2.Enabled = false; } string dbPath = Server.MapPath(@"/App_Data/change.mdb"); string sqlStr = "SELECT DISTINCT [country] FROM [Region] WHERE [city]='" + DropDownList1.SelectedItem.Value + "'"; BindDropDownList(dbPath, sqlStr, DropDownList2); } // 为DropDownList绑定数据 private void BindDropDownList(string dbPath,string sqlStr,DropDownList ddl) { string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath; OleDbConnection connnection = new OleDbConnection(connstr); connnection.Open(); OleDbCommand command = new OleDbCommand(sqlStr, connnection); OleDbDataReader dataReader = command.ExecuteReader(); while (dataReader.Read()) { //ddl.Items.Add(new ListItem(dr["bjmc"].ToString(), dr["id"].ToString()));//增加Item //或者这样也能绑定, ddl.Items.Add(new ListItem(dataReader[0].ToString(), dataReader[0].ToString()));//增加Item //都是要在知道Sql语句或者数据表结构的前提下才能这样绑定 } dataReader.Close(); connnection.Close(); }
关键点:OleDb访问数据库;DropDownList数据绑定;
0 0
- 访问Access数据库实现DropDownList二级异步联动(ASP.NET WebForm开发方式)
- asp.net js实现dropdownlist二级联动(动态)
- asp.net js实现dropdownlist二级联动(动态)
- asp.net DropDownList无刷新ajax二级联动实现详细过程
- ASP.NET实现无刷新DropDownList联动
- asp.net实现无刷新DropDownList联动
- Asp.net WebForm DropDownList 无限极联动(不需要写C#代码,不需要写JS,只是配置属性)
- asp.net二级联动
- .net asp.net实现无刷新DropDownList联动控件
- Jquery、DropDownList、数据库 联动实现(一)
- Jquery、DropDownList、数据库 联动实现(二)
- 实现DropDownList无刷新二级联动
- AJAX实现DropDownList无刷新二级联动
- ASP.NET实现无刷新的DropdownList联动效果(20041204)
- asp.net实现无刷新DropDownList联动控件
- asp.net实现无刷新DropDownList联动控件
- asp.net实现无刷新DropDownList联动控件
- asp.net实现DropDownList、ListBox无刷新三级联动(javascript)
- 判断输入一个数是否为素数的一般算法
- 开博第一天
- 2015年创业中遇到的技术问题:31-40
- [leetcode]C语言:171 Excel Sheet Column Number
- 第3周项目5-数组作数据成员(1)
- 访问Access数据库实现DropDownList二级异步联动(ASP.NET WebForm开发方式)
- 不使用第三变量,交换a,b两个值
- c++中qsort函数实际应用
- Android的5个进程等级
- Single Number 2
- SQL小知识点(一)
- Oracle表空间用户数据导出
- HDU 1096 A+B for Input-Output Practice (VIII)
- [leetcode]C语言:169 Majority Element