分享一个刚刚写的联动DropDownList控件 欢迎拍砖

xboxeer 2012-05-04 12:12:26
加精
前几天泡论坛的时候发现有很多人问关于联动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;
}
}
}
}

...全文
1948 78 打赏 收藏 转发到动态 举报
写回复
用AI写文章
78 条回复
切换为时间正序
请发表友善的回复…
发表回复
代码小天王 2012-06-28
  • 打赏
  • 举报
回复
楼主你想多了。。。。别人在使用你的服务器控件的时候,一样的设置数据源来绑定,只是你在客户端表现形式,以及回发方法不一样而已,跟客户端数据格式没关系。。。。我以前做过一个服务器控件就是这样的,一种ajax,一种postback
[Quote=引用 77 楼 的回复:]

ajax化考虑到客户端的数据格式无法预知 没法定义一个比较通用的接口~
引用 76 楼 的回复:

楼主,建议你设置个属性,IsAjax,如果是true的话,那么你服务端输出一些资源文件(js)之类的,然后使用ajax来做级联效果,如果是false的话,就默认是postback提交
[/Quote]
xboxeer 2012-06-27
  • 打赏
  • 举报
回复
ajax化考虑到客户端的数据格式无法预知 没法定义一个比较通用的接口~
[Quote=引用 76 楼 的回复:]

楼主,建议你设置个属性,IsAjax,如果是true的话,那么你服务端输出一些资源文件(js)之类的,然后使用ajax来做级联效果,如果是false的话,就默认是postback提交
[/Quote]
代码小天王 2012-06-27
  • 打赏
  • 举报
回复
楼主,建议你设置个属性,IsAjax,如果是true的话,那么你服务端输出一些资源文件(js)之类的,然后使用ajax来做级联效果,如果是false的话,就默认是postback提交
Lugyedo 2012-05-09
  • 打赏
  • 举报
回复
独乐乐不如众乐乐
yespie 2012-05-09
  • 打赏
  • 举报
回复
为什么不用Jquery
moqijunnu 2012-05-09
  • 打赏
  • 举报
回复
太麻烦了吧。。。。。不过定下
samyou 2012-05-08
  • 打赏
  • 举报
回复
分享不错。
lqkankan 2012-05-08
  • 打赏
  • 举报
回复
虽然都是用js写联动 但是也顶一下

这里涉及到用户控件 事件等
wsjrzjp 2012-05-08
  • 打赏
  • 举报
回复
没必要这么复杂的
yp19910928 2012-05-08
  • 打赏
  • 举报
回复
先回复,回头再仔细看下
liaoyanstorm 2012-05-08
  • 打赏
  • 举报
回复
分享不错。
fhgg666 2012-05-08
  • 打赏
  • 举报
回复
LZ用心了 支持分享!!!
苹果哥 2012-05-08
  • 打赏
  • 举报
回复
支持分享是必须的,LZ用心了
tinranqi 2012-05-07
  • 打赏
  • 举报
回复
多谢分享
dugutianshun 2012-05-07
  • 打赏
  • 举报
回复
支持下!
花开是心碎了 2012-05-07
  • 打赏
  • 举报
回复
学习,收藏,备用。
ShenWong 2012-05-07
  • 打赏
  • 举报
回复
[Quote=引用 44 楼 的回复:]

支持分享!!!
[/Quote]
+1
EnForGrass 2012-05-06
  • 打赏
  • 举报
回复
支持一个,
xiaocongzhi 2012-05-06
  • 打赏
  • 举报
回复
收藏,备用
yyl8781697 2012-05-05
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 的回复:]

不用这么麻烦吧?
代码太多了
ajax其实挺简单的。
[/Quote]
我也觉得,LZ可以用jquery+ajax+ashx+(xml或者sqlserver)来写,很方便的联动
加载更多回复(36)

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧