Webform 三级联动例子
2018-06-22 07:36:59来源:未知 阅读 ()
首先分别做三个下拉列表
<body> <form id="form1" runat="server"> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"></asp:DropDownList> <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"></asp:DropDownList> <asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList> </form> </body>
建类:China实体类
public China() { } private string code; public string Code { get { return code; } set { code = value; } } private string name; public string Name { get { return name; } set { name = value; } } private string prentcode; public string Prentcode { get { return prentcode; } set { prentcode = value; } }
数据访问类:
public class ChinaData { SqlConnection conn = null; SqlCommand cmd = null; public ChinaData() { conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123"); cmd = conn.CreateCommand(); } public List<China> Select(string pcode) { cmd.CommandText = "select * from ChinaStates where ParentAreaCode=@pcode"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@pcode", pcode); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); List<China> list = new List<China>(); if (dr.HasRows) { while (dr.Read()) { China data = new China(); data.Code = dr[0].ToString(); data.Name = dr[1].ToString(); data.Prentcode = dr[2].ToString(); list.Add(data);
}
}
conn.Close();
return list;
}
}
.cs后台代码
首先数据绑定:
if (!IsPostBack) { //调用方法绑定数据 Bind(DropDownList1,new ChinaStatesData().Select("0001")); Bind(DropDownList2, new ChinaStatesData().Select(DropDownList1.SelectedValue)); Bind(DropDownList3, new ChinaStatesData().Select(DropDownList2.SelectedValue)); } //委托 DropDownList1.SelectedIndexChanged += DropDownList1_SelectedIndexChanged; DropDownList2.SelectedIndexChanged += DropDownList2_SelectedIndexChanged;
//造方法: private void Bind(DropDownList ddl, List<ChinaStates> list) { ddl.DataSource = list; ddl.DataTextField = "AreaName"; ddl.DataValueField = "AreaCode"; ddl.DataBind(); }
//SelectedIndexChanged事件 void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { Bind(DropDownList2, new ChinaStatesData().Select(DropDownList1.SelectedValue));//填充市 Bind(DropDownList3, new ChinaStatesData().Select(DropDownList2.SelectedValue));//填充区 } void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) { Bind(DropDownList3, new ChinaStatesData().Select(DropDownList2.SelectedValue));//填充区 }
代码部分完成后,将下拉列表的AutoPostBack属性改为true
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Asp.Net中WebForm的生命周期 2020-03-29
- ASP.NET MVC IOC依赖注入之Autofac系列(二)- WebForm当中 2019-08-13
- 解析ASP.NET MVC下拉框联动实例 2019-08-09
- ASP.NET -- WebForm -- ScriptManager 类 2018-12-02
- ASP.NET -- WebForm -- HttpRequest类的方法和属性 2018-11-28
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash