分享一个刚刚写的联动DropDownList控件 欢迎拍砖
前几天泡论坛的时候发现有很多人问关于联动DropDownList的问题 在下不才写了个支持联动的DropDownList服务器控件 方便大家 代码有点儿不好懂我会写上注释 2楼上调用代码 欢迎拍砖
源代码:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;using System.Text;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;[assembly: TagPrefix("WebApplication", "MyControl")]namespace WebApplication1{ [ToolboxData("<{0}:LinkedDropDownList runat=server></{0}:LinkedDropDownList>")] public class LinkedDropDownList : DropDownList//继承自基本的DropDownList { private DropDownList _linkedDropDownList; public LinkedDropDownList() { this.SelectedIndexChanged += new EventHandler(LinkedDropDownList_SelectedIndexChanged);//添加自己的SelectedIndexChanged事件 this.AutoPostBack = true;//强制设置自己的AutoPostBack为true } //下面两个事件都作为获取数据源的事件 public event EventHandler BindData;//这个事件为绑定父DropDownList public event Action<object, DropDownListIndexChangeEventArg> BindLinkedData;//这个事件为绑定子DropDownList protected void LinkedDropDownList_SelectedIndexChanged(object sender, EventArgs e) { if (this.BindLinkedData != null) { this._linkedDropDownList = this.NamingContainer.FindControl(this.NextDropDownListID) as DropDownList;//从控件容器中找出子下拉菜单 var newE = new DropDownListIndexChangeEventArg(this.SelectedValue, this.SelectedItem.Text, this.SelectedIndex,this._linkedDropDownList); BindLinkedData(this, newE); } //如果子DropDownList本身也为联动DropDownList 执行子DropDownList的绑定 var next = this.NamingContainer.FindControl(this.NextDropDownListID) as LinkedDropDownList; if (next != null) { next.OnSelectedIndexChanged(EventArgs.Empty); } } //重写OnLoad使得可以发生数据绑定 protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (this.BindData != null && !Page.IsPostBack) { BindData(this, EventArgs.Empty); this.SelectedIndex = 0; } if (this.BindLinkedData != null &&!Page.IsPostBack) { this._linkedDropDownList = this.NamingContainer.FindControl(this.NextDropDownListID) as DropDownList; var newE = new DropDownListIndexChangeEventArg(this.SelectedValue, this.SelectedItem.Text, this.SelectedIndex, this._linkedDropDownList); BindLinkedData(this, newE); } } //子DropDownList的ID public string NextDropDownListID { get { return ViewState[this.ID + "LinkedDropDownList"] as string;//加上自身的ID做唯一标示 } set { ViewState[this.ID + "LinkedDropDownList"] = value; } } } //事件参数 方便绑定子菜单时候得到数据源绑定参数 public class DropDownListIndexChangeEventArg : EventArgs { private readonly string _selectedValue = null; private readonly string _selectedText = null; private readonly int _selectedIndex; private readonly DropDownList _nextDropDownList = null; public DropDownListIndexChangeEventArg(string selectedValue, string selectedText, int selectedIndex,DropDownList nextDropDownList) { this._selectedValue = selectedValue; this._selectedText = selectedText; this._selectedIndex = selectedIndex; this._nextDropDownList = nextDropDownList; } public string SelectedValue { get { return this._selectedValue; } } public string SelectedText { get { return this._selectedText; } } public int SelectedIndex { get { return this._selectedIndex; } } public DropDownList NextDropDownList { get { return this._nextDropDownList; } } }}