ASP.NET 三级联动控件

前台:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="DropDownListThree.ascx.cs" Inherits="UserControls_DropDownListThree" %> <%@ Register Assembly="yysd.Common" Namespace="yysd.Common"     TagPrefix="cc1" %> <script language="javascript" type="text/javascript"> //得到二级类别 function GetTwoType(oneType,twoType,threeType,hidValue) {     var oneType = document.getElementById(oneType);     var twoType = document.getElementById(twoType);         var strOne = UserControls_DropDownListThree.GetStrType(oneType.value);

    BindDropDownList(strOne.value,twoType)         GetThreeType(twoType.id,threeType,hidValue);    } //得到三级类别 function GetThreeType(twoType,threeType,hidValue) {     var twoType = document.getElementById(twoType);     var threeType = document.getElementById(threeType);     var strTwo = UserControls_DropDownListThree.GetStrType(twoType.value);     BindDropDownList(strTwo.value,threeType);  

    if(threeType.length == 0||threeType.value == "%")     {        SetValueOne(twoType,hidValue);     }     else     {        SetValueOne(threeType,hidValue);     }         } //得到值

function GetValue(threeType,hidValue) {     var threeType = document.getElementById(threeType);         var hidValue = document.getElementById(hidValue);     SetValueOne(threeType,hidValue.id);      } //设置值

function SetValueOne(ddlDistrict,hidValue) {       var hidValue = document.getElementById(hidValue);     if(ddlDistrict.length>0)     {      for(var i=0;i<ddlDistrict.length;i++)      {                if(ddlDistrict[i].selected == true)         {                      hidValue.value = ddlDistrict[i].value;         }      }      }   }  //绑定下拉列表ddl,数据源是arrResult     function BindDropDownList(arrResult,ddl)     {         if(arrResult != "")       {            //先清空原有的值

          ddl.length=0;         //拆分值成数组         var piArray = arrResult.substring(arrResult.lastIndexOf(">")+1,arrResult.length).split(",");         //循环数组         if(piArray!=null&&piArray!="")         {          for(var i=0;i<piArray.length-1;i++)          {       //再拆分成ID及名称

       var ary1 = piArray[i].toString().split("|");           //alert(ary1[1].toString());        //alert(ary1[0].toString());        //逐一添加项

       ddl.options.add(new Option(ary1[1].toString(),ary1[0].toString()));          }           }         else         {           ddl.length = 0;           ddl.options.add(new Option("-选项为空-","%"));         }       }       else       {          //如果没有取到值,则清空要填充的下拉框的值

        ddl.length = 0;         ddl.options.add(new Option("-选项为空-","%"));       }     } </script>

<cc1:dropdownlistbase id="ddlOneType" runat="server" ></cc1:dropdownlistbase> <cc1:dropdownlistbase id="ddlTwoType" runat="server"></cc1:dropdownlistbase> <cc1:dropdownlistbase id="ddlThreeType" runat="server"></cc1:dropdownlistbase>

<asp:Label ID="lblText" runat="server"></asp:Label> <input type="hidden" runat="server" id="hidValue" /> 后台:

using System; using System.Collections; using System.Web.UI; using System.Web.UI.WebControls; using Resources; using yysd.BLL; using yysd.Model; using yysd.Common; using Ajax;

/* 功能说明:该控件主要用于显示Category表中类别的三级联动下拉列表 TypeValue为类别的值,即ID ParentID 为分类根目录父ID值,即取该分类父ID下所有分类记录集 ReadOnly为是否将该控件设置为只读,即没有下拉列表 说明:liyh  */

public partial class UserControls_DropDownListThree : UserControl {     /// <summary>     /// 类别类型     /// </summary>     public enum CategoryT     {         Two = 2,         Three = 3     }     /// <summary>     /// 首选项     /// </summary>     public string strChoosePlease = DisplayText.PleaseChoose.ToString();       protected void Page_Load(object sender, EventArgs e)     {         Ajax.Utility.RegisterTypeForAjax(typeof(UserControls_DropDownListThree));         ddlOneType.Attributes.Add("onchange", "GetTwoType('" + this.ddlOneType.ClientID + "','" + this.ddlTwoType.ClientID + "','" + this.ddlThreeType.ClientID + "','" + this.hidValue.ClientID + "')");         ddlTwoType.Attributes.Add("onchange", "GetThreeType('" + this.ddlTwoType.ClientID + "','" + this.ddlThreeType.ClientID + "','" + this.hidValue.ClientID + "')");         ddlThreeType.Attributes.Add("onchange", "GetValue('" + this.ddlThreeType.ClientID + "','" + this.hidValue.ClientID + "')");

        //如果不是编辑状态则加载,否则不能加载         if (!FlagEdit)         {             if (!IsPostBack)             {                 BindOneType(GetRootType(ParentID));                 BindTwoType(GetTypeByParent(ddlOneType.Items[0].Value));                 BindThreeType(GetTypeByParent(ddlTwoType.Items[0].Value));                 if (ddlThreeType.Items.Count > 0)                 {                     hidValue.Value = ddlThreeType.Items[0].Value;                 }                 else                 {                     hidValue.Value = ddlTwoType.Items[0].Value;                 }             }         }

     

        if (yysd.Common.Utility.IsSamePage())         {             if (Request.QueryString["Action"] != null)             {

                BindOneType(GetRootType(ParentID));                 BindTwoType(GetTypeByParent(ddlOneType.Items[0].Value));                 BindThreeType(GetTypeByParent(ddlTwoType.Items[0].Value));                 hidValue.Value = TypeValue;             }         }

    }

    private String mParentID;     private bool mFlagEdit;     private String mTypeValue;     private bool mReadOnly;

     /// <summary>     /// 父类ID值     /// </summary>    public String ParentID    {        get { return this.mParentID; }        set { this.mParentID = value; }    }

   /// <summary>    /// 只读    /// </summary>    public bool FlagEdit    {        get { return this.mFlagEdit; }        set { this.mFlagEdit = value; }    }

    /// <summary>     /// 类别值

    /// </summary>     public String TypeValue     {         get         {             if (hidValue.Value != "")             {                 this.mTypeValue = hidValue.Value;             }             else             {                 this.mTypeValue = "";             }             return this.mTypeValue;         }         set         {             if (value.Trim() != "" && value.Trim() != "%")             {                 this.mTypeValue = value;                 BindDownList(mTypeValue);                 hidValue.Value = value;

            }             if (ReadOnly)             {                 ddlOneType.Visible = ddlTwoType.Visible = ddlThreeType.Visible = false;                 string strSeparator = WebsiteInfo.SpitString.ToString();                 string strText = CheckNull(ddlOneType) + strSeparator + CheckNull(ddlTwoType) + strSeparator + CheckNull(ddlThreeType);                 if (strText == "||")                 {                     lblText.Text = "";                 }                 else                 {                     lblText.Text = strText;                 }

            }

        }     }     private String CheckNull(DropDownList ddl)     {         if (ddl != null && ddl.Items.Count > 0)         {             if (ddl.SelectedItem.Text != strChoosePlease)             {                 return ddl.SelectedItem.Text.ToString();             }             else             {                 return "";             }         }         else         {             return "";         }     }     /// <summary>     /// 只读     /// </summary>     public bool ReadOnly     {         get { return this.mReadOnly; }         set { this.mReadOnly = value; }     }     /// <summary>     /// 设置下拉列表选项     /// </summary>     /// <param name="ddl"></param>     /// <param name="strValue"></param>     private void setValue(ref DropDownListBase ddl, string strValue)     {         ddl.SelectedIndex = -1;         for (int i = 0; i < ddl.Items.Count; i++)         {             if (ddl.Items[i].Value == strValue)             {                 ddl.Items[i].Selected = true;             }         }     }     /// <summary>     /// 绑定一级类别

    /// </summary>     /// <param name="ilist">一级类别列表</param>     private void BindOneType(IList ilist)     {         ddlOneType.Items.Clear();         Category uGategory = new Category();         ddlOneType.Items.Add(new ListItem(strChoosePlease, ""));         foreach (object o in ilist)         {             uGategory = (Category)o;             ddlOneType.Items.Add(new ListItem(uGategory.CategoryName, uGategory.CategoryID));         }     }     /// <summary>     /// 绑定二级类别     /// </summary>     /// <param name="ilist">二级类别列表</param>     private void BindTwoType(IList ilist)     {         ddlTwoType.Items.Clear();         Category uGategory = new Category();         ddlTwoType.Items.Add(new ListItem(strChoosePlease, ""));         foreach (object o in ilist)         {             uGategory = (Category)o;             ddlTwoType.Items.Add(new ListItem(uGategory.CategoryName, uGategory.CategoryID));         }     }     /// <summary>     /// 绑定三级类别     /// </summary>     /// <param name="ilist">三级类别列表</param>     private void BindThreeType(IList ilist)     {         ddlThreeType.Items.Clear();         Category uGategory = new Category();         ddlThreeType.Items.Add(new ListItem(strChoosePlease, ""));         foreach (object o in ilist)         {             uGategory = (Category)o;             ddlThreeType.Items.Add(new ListItem(uGategory.CategoryName, uGategory.CategoryID));         }     }

    /// <summary>     /// 得到一级类别     /// </summary>     /// <returns></returns>     private IList GetRootType(string ParentID)     {         CategoryBLL uCategoryObject = new CategoryBLL();         IList ProductCategoryList;         ProductCategoryList = uCategoryObject.GetCategoryList(ParentID);         return ProductCategoryList;     }     /// <summary>     /// 根据父ID得到下级目录,可选出二级类别及三级类别

    /// </summary>     /// <param name="CategoryId">父ID</param>     /// <returns></returns>     private IList GetTypeByParent(string CategoryId)     {         IList ProductCategoryList = new ArrayList();         if (CategoryId != null && CategoryId != "")         {             CategoryBLL uCategoryObject = new CategoryBLL();

            ProductCategoryList = uCategoryObject.GetCategoryList(CategoryId);

        }         return ProductCategoryList;     }     /// <summary>     /// 根据CategoryID设置三级下拉列表     /// </summary>     /// <param name="CategoryId"></param>     private void BindDownList(string CategoryId)     {         Category oneCategory = new Category();         Category twoCategory = new Category();         Category threeCategory = new Category();         CategoryBLL uCategoryBLL = new CategoryBLL();

        //获取从子级到所有父级列表,直到顶级.目前为三级         IList CategoryList = uCategoryBLL.GetParentList(CategoryId);         if (CategoryList.Count > 0)         {             //如果是二级,则说明CategoryId就是二级分类ID             if (CategoryList.Count == (int)CategoryT.Two)             {                 foreach (Category uCategory in CategoryList)                 {                     //如果uCategory实体的CategoryID值等于参数二级分类ID,则uCategory实体属于二级分类的实体                     if (uCategory.CategoryID == CategoryId)                     {                         twoCategory = uCategory;                     }                     else                     {   //否则是一级分类实体                         oneCategory = uCategory;                     }                 }             }             //如果是三级则说明CategoryId就是三级分类ID             else if (CategoryList.Count == (int)CategoryT.Three)             {                 //根据分类ID值获得三级分类实体                 threeCategory = uCategoryBLL.GetDetail(CategoryId);

                foreach (Category uCategory in CategoryList)                 {                     if (uCategory != threeCategory)                     {                         //如果三级父类ID等于子类ID,则说明就是二级类                         if (uCategory.CategoryID == threeCategory.ParentID)                         {                             twoCategory = uCategory;                         }                         else if (uCategory.ParentID != threeCategory.ParentID)                         {                             oneCategory = uCategory;                         }                     }                 }             }

            //显示一级             BindOneType(GetRootType(ParentID));             setValue(ref ddlOneType, oneCategory.CategoryID);

            //显示二级             BindTwoType(GetTypeByParent(oneCategory.CategoryID));             setValue(ref ddlTwoType, twoCategory.CategoryID);

            //显示三级             if (threeCategory != null && threeCategory.CategoryID != "")             {                 BindThreeType(GetTypeByParent(twoCategory.CategoryID));                 setValue(ref ddlThreeType, threeCategory.CategoryID);             }         }

    }     /// <summary>     /// 得到下拉列表字符串让WEB服务调用     /// </summary>     /// <param name="strTypeId"></param>     /// <returns></returns>     [AjaxMethod(HttpSessionStateRequirement.Read)]     public String GetStrType(string strTypeId)     {         Category uCategory = new Category();         IList CategoryList = GetTypeByParent(strTypeId);         String strResult = "";         foreach (object o in CategoryList)         {             uCategory = (Category)o;             strResult = strResult + uCategory.CategoryID + "|" + uCategory.CategoryName + ",";         }         return strResult;     } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值