asp.net Repeater等数据控件模版内部2个DropDownList控件级联

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();
         }
     }
}


上一篇:一张图说明访问网站的流程


下一篇:IIs 网站应用程序与虚拟目录的区别及高级应用说明(文件分布式存储方案)