c# – 如何自定义asp.net DropDownList以支持自定义数据绑定字段
发布时间:2020-12-16 01:49:13 所属栏目:百科 来源:网络整理
导读:有没有办法定制asp.net下拉列表来支持自定义数据绑定字段.它不应该是控件的自定义属性.它应该能够通过DataBind()绑定数据;具有相同数据源的方法.在这个自定义服务器控件中,我正在尝试访问新的自定义字段,并且基于该值,我将对该数据源的特定行进行一些计算.
有没有办法定制asp.net下拉列表来支持自定义数据绑定字段.它不应该是控件的自定义属性.它应该能够通过DataBind()绑定数据;具有相同数据源的方法.在这个自定义服务器控件中,我正在尝试访问新的自定义字段,并且基于该值,我将对该数据源的特定行进行一些计算.
标准控制代码 <asp:DropDownList runat="server" DataTextField="TextField" DataValueField="ValueField"/> 新的自定义控件应该是这样的, <asp:DropDownList runat="server" DataTextField="TextField" DataValueField="ValueField" DataCustomField="CustomField"/> 解决方法
你可以将DropDownList扩展为这样的东西:
public class MyDropDownList : DropDownList { public MyDropDownList() { } public string CustomProperty { get; set; } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); int i = 0; foreach (var item in this.DataSource as IEnumerable) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(item); PropertyDescriptor pd = properties.Find(CustomProperty,true); this.Items[i].Attributes.Add(CustomProperty,pd.GetValue(item).ToString()); i++; } } } 并在您的标记上使用它如下: <cc1:MyDropDownList ID="MyDropDownList1" DataTextField="Name" DataValueField="Department" CustomProperty="ImageUrl" runat="server"> </cc1:MyDropDownList> 在我的情况下,我绑定到List< Employee>具有一些属性,如Name,Department和ImageUrl.呈现下拉列表后如下所示: <select name="ctl00$MainContent$MyDropDownList1" id="MainContent_MyDropDownList1"> <option selected="selected" value="Human resources" ImageUrl="http://www.freedigitalphotos.net/images/gal_images/av-_146.jpg">Employee 0</option> <option value="Information Technology" ImageUrl="http://www.freedigitalphotos.net/images/gal_images/av-_314.jpg">Employee 1</option> <option value="Human resources" ImageUrl="http://www.freedigitalphotos.net/images/gal_images/av-_146.jpg">Employee 2</option> <option value="Information Technology" ImageUrl="http://www.freedigitalphotos.net/images/gal_images/av-_314.jpg">Employee 3</option> <option value="Human resources" ImageUrl="http://www.freedigitalphotos.net/images/gal_images/av-_146.jpg">Employee 4</option> </select> 更新:(对于@emrahozguner,他通过Twitter向我发送了一个关于如何在SelectedIndexChanged事件中检索CustomProperty的问题) public class MyDropDownList : DropDownList { public MyDropDownList() { } public string CustomProperty { get; set; } public string SelectedCustomProperty { get { //Use the SelectedIndex to retrieve the right element from ViewState return ViewState["CustomProperty" + this.SelectedIndex] as string; } } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); int i = 0; if (this.DataSource != null) { foreach (var item in this.DataSource as IEnumerable) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(item); PropertyDescriptor pd = properties.Find(CustomProperty,true); this.Items[i].Attributes.Add(CustomProperty,pd.GetValue(item).ToString()); //We need to save the CustomProperty value on ViewState if we want to be able to retrieve it... ViewState["CustomProperty" + i] = pd.GetValue(item).ToString(); i++; } } } } 您可以访问SelectedIndexChanged上的CustomProperty,如下所示: protected void MyDropDownList1_SelectedIndexChanged(object sender,EventArgs e) { MyDropDownList l = (sender as MyDropDownList); if (l != null) { string selectedCustomProperty = l.SelectedCustomProperty; //Do something cool with this selectedCustomProperty } } 免责声明:这不是唯一的方法,但这是我能想到的最简单的方法,而不会覆盖LoadViewState和SaveViewState等. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 重写ajax方法实现异步请求session过期时跳转登录页面
- Flex Viewer 开发教程(2)Widget配置文件
- *未指定*在C typedef语句中是什么意思?
- 聚合+传统媒体电子版本=门户?
- c# – 如何使键盘可滚动只读WPF TextBox?
- 依赖倒转原则(The Dependency Inversion Principle)
- oracle – 当用户取消程序ORA-01013时出现异常
- react-native – 如何在NativeBase Button中显示小写文本
- Flash调试模式,输出LOG
- actionscript-3 – AS3 NetStream AppendBytes寻求问题