因为最近做的项目需要3级联动显示分类,同时分类又有几种不同类型,如产品类型、公司类型等,出于提高代码的重用性考虑,参考网上代码特将3级联动做成用户控件!
实现效果说明:
1、用户控件的特性——使用和重用简单,通过简单的设置一个属性,就可实现不同类型种类的联动;
2、废话下,无刷新获取子类;
使用技术的选择:
1、AjaxPro的ajax框架
不是我不想用微软的asp.net ajax框架来实现,只是据说udatePanel是一种局部显示、但仍然是整页回传到局部更新,另外微软的ajax每个页面,都必须要一个scriptmanager,麻烦了点,最主要的是本人项目不是soa的,所以客户端调用服务的方法都是页面方法,而不是Service method,于是先天上早已决定了,不能使用AjaxControlToolkit现成的联动控件CascadingDropDown,如果你的项目是soa的你可以选择CascadingDropDown;由于以上原因,选择了相对“原始”的Ajax框架AjaxPro.2(2.0版本),不过使用很简单
2、给用户控件增加属性
给用户控件添加属性
1//标记不同类型的类别
2public string Type
3 {
4 set {ViewState["type"]=value;}
5 get { return ViewState["type"].ToString(); }
6
7 }
8
9//提供对下拉框1的选择值到访问
10 public string selValue1
11 {
12 get { return hdf1.Value; }
13
14 }
15
16//提供对下拉框2的选择值到访问
17 public string selValue2
18 {
19
20 get { return hdf2.Value; }
21
22 }
23
24//提供对下拉框3的选择值到访问
25 public string selValue3
26 {
27 get { return hdf3.Value; }
28
29 }
废话不多说,具体实现见代码:
用户控件的cs
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12
13public partial class controls_ThreeLevelDropdownList : System.Web.UI.UserControl
14{
15
16 public string Type
17 {
18 set {ViewState["type"]=value;}
19 get { return ViewState["type"].ToString(); }
20
21 }
22 public string selValue1
23 {
24 get { return hdf1.Value; }
25
26 }
27 public string selValue2
28 {
29
30 get { return hdf2.Value; }
31
32 }
33 public string selValue3
34 {
35 get { return hdf3.Value; }
36
37 }
38
39
40 protected void Page_Load(object sender, EventArgs e)
41 {
42
43 if (!IsPostBack)
44 {
45 AjaxPro.Utility.RegisterTypeForAjax(typeof(controls_ThreeLevelDropdownList)); //必要的
46
47 ddl1.Attributes.Add("onchange", "showNext(this.options[selectedIndex].value,'" + ddl2.ClientID + "','" + Type + "');selectValue(this.options[selectedIndex].value,'" + hdf1.ClientID + "');");
48 ddl2.Attributes.Add("onchange", "showNext(this.options[selectedIndex].value,'" + ddl3.ClientID + "','" + Type + "');selectValue(this.options[selectedIndex].value,'" + hdf2.ClientID + "');");
49 ddl3.Attributes.Add("onchange", "selectValue(this.options[selectedIndex].value,'"+hdf3.ClientID+"');");
50
51 ddl1.Items.Add(new ListItem("请选择","0"));
52 DataSet ds = GetLevelList("0", Type);
53
54 if (ds != null && ds.Tables[0].Rows.Count > 0)
55 {
56 foreach(DataRow dr in ds.Tables[0].Rows)
57 {
58 ddl1.Items.Add(new ListItem(dr["txt"].ToString(), dr["val"].ToString()));
59 }
60
61 }
62 ddl1.SelectedValue = "0";
63 }
64 }
65
66 [AjaxPro.AjaxMethod]
67 public DataSet GetLevelList(string parentId,string type)
68 {
69 return new Dao.CategoryDao().getListDs(type, parentId);
70
71 }
72}
73
用户控件的aspx
1<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ThreeLevelDropdownList.ascx.cs" Inherits="controls_ThreeLevelDropdownList" %>
2<asp:DropDownList ID="ddl1" runat="server">
3</asp:DropDownList>
4<asp:DropDownList ID="ddl2" runat="server">
5<asp:ListItem Text="请选择" Value="0"></asp:ListItem>
6</asp:DropDownList>
7<asp:DropDownList ID="ddl3" runat="server">
8<asp:ListItem Text="请选择" Value="0"></asp:ListItem>
9</asp:DropDownList> <br />
10
11<asp:HiddenField ID="hdf1" runat="server" /><asp:HiddenField ID="hdf2" runat="server" /><asp:HiddenField ID="hdf3" runat="server" />
12<script type="text/javascript" language="javascript">
13<!--
14 //常用JS函数
15 function getBid(s){
16 return document.getElementById(s);
17 }
18 function getBmc(s){
19 return document.getElementByName(s);
20 }
21
22 //显示分类列表
23 function showNext(sid,obj,type)
24 {
25 if(sid==null || sid=="" || sid.length<1)return;
26 var slt =getBid(obj);
27 var v = controls_ThreeLevelDropdownList.GetLevelList(sid,type).value; // 类的名称
28
29