asp.net Repeater等資料控制元件模版內部2個DropDownList控制元件級聯

暖楓無敵發表於2012-05-04

1、新建一個從DropDownList繼承的擴充套件類CommandableDropDownList.cs,程式碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Web.UI.WebControls;

namespace GrowthMonitor.BaseClass
{
    public class CommandableDropDownList : DropDownList
    {
        #region 屬性
        [
        Browsable(true),
        DefaultValue("")
        ]
        public string CommandName
        {
            get
            {
                return ViewState["CommandName"] == null ? String.Empty : ViewState["CommandName"].ToString();
            }
            set
            {
                ViewState["CommandName"] = value;
            }
        }


        [
        Browsable(true),
        DefaultValue("")
        ]
        public string CommandArgument
        {
            get
            {
                return ViewState["CommandArgument"] == null ? String.Empty : ViewState["CommandArgument"].ToString();
            }
            set
            {
                ViewState["CommandArgument"] = value;
            }
        }

        #endregion

        #region 構造器
        public CommandableDropDownList()
        {
            //
            // TODO: 在此處新增建構函式邏輯
            //
        }
        #endregion


        #region 事件

        private static object _commandEvent = new object();

        public event CommandEventHandler SelectedChangedCommand
        {
            add
            {
                Events.AddHandler(_commandEvent, value);
            }
            remove
            {
                Events.RemoveHandler(_commandEvent, value);
            }
        }

        protected virtual void OnCommand(CommandEventArgs e)
        {
            CommandEventHandler commandHander = (CommandEventHandler)Events[_commandEvent];
            if (commandHander != null)
            {
                commandHander(this, e);
            }
            else
            {
                base.RaiseBubbleEvent(this, e);
            }
        }

        #endregion

        #region 覆寫的方法

        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            base.OnSelectedIndexChanged(e);
            if (this.AutoPostBack)
            {
                CommandEventArgs args = new CommandEventArgs(this.CommandName, this.CommandArgument);
                OnCommand(args);
            }
        }
        #endregion
    }
}

2、在需要使用的頁面頭部加上如下標記:

<%@ Register TagPrefix="ddlTag" Namespace="GrowthMonitor.BaseClass" Assembly="GrowthMonitor" %>


3、在Repeater中的DropDownList

<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_OnItemDataBound" OnItemCommand="rpt_OnItemCommand">
      <ItemTemplate>
              <ddlTag:CommandableDropDownList ID="ddlVarietyType" runat="server" CommandName="VarietyTypeChange"   AutoPostBack="true">
              </ddlTag:CommandableDropDownList>

             <ddlTag:CommandableDropDownList ID="ddlVarietyName" runat="server">
             </ddlTag:CommandableDropDownList>
      </ ItemTemplate>
</asp:Repeater>

4、後臺事件程式碼

public void rpt_OnItemCommand(object sender, RepeaterCommandEventArgs e)
{
     if (e.CommandName == "VarietyTypeChange")
     {
          string currentVarietyTypeID = String.Empty;
          int index = e.Item.ItemIndex;
          CommandableDropDownList ddl = this.rpt.Items[index].FindControl("ddlVarietyType") as CommandableDropDownList;
         if (ddl != null)
         {
              currentVarietyTypeID = ddl.SelectedItem.Value.Trim();
         }
         ddl = this.rpt.Items[index].FindControl("ddlVarietyName") as CommandableDropDownList;
         if (ddl != null)
         {
             ddl.DataSource = new VTB_VarietiesBLL().GetData(" IsDelete=0 and VarietyTypeID=" + currentVarietyTypeID, " CreateTime asc");
             ddl.DataTextField = "VarietiesName";
             ddl.DataValueField = "ID";
             ddl.DataBind();
          }
     }
}

public void rpt_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
          //品種型別
          CommandableDropDownList ddl_VarietyType = e.Item.FindControl("ddlVarietyType") as CommandableDropDownList;
          if (ddl_VarietyType != null)
          {
              ddl_VarietyType.DataSource = new VTB_VarietyTypeBLL().GetData(" IsDelete=0 ", " CreateTime asc");
              ddl_VarietyType.DataTextField = "VarietyTypeName";
              ddl_VarietyType.DataValueField = "ID";
              ddl_VarietyType.DataBind();
          }

          //品種
          CommandableDropDownList ddl_Variety = e.Item.FindControl("ddlVarietyName") as CommandableDropDownList;
         if (ddl_Variety != null)
         {
             ddl_Variety.DataSource = new VTB_VarietiesBLL().GetData(" IsDelete=0 and VarietyTypeID=" + ddl_VarietyType.SelectedItem.Value, " CreateTime asc");
             ddl_Variety.DataTextField = "VarietiesName";
             ddl_Variety.DataValueField = "ID";
             ddl_Variety.DataBind();
         }
     }
}


相關文章