大佬教程收集整理的这篇文章主要介绍了c# – 如何自定义asp.net DropDownList以支持自定义数据绑定字段,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
标准控制代码
<asp:DropDownList runat="server" DataTextField="TextField" DataValueField="ValueField"/>
<asp:DropDownList runat="server" DataTextField="TextField" DataValueField="ValueField" DataCustomField="CustomField"/>
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 } }
免责声明:这不是唯一的@L_607_4@,但这是我能想到的最简单的@L_607_4@,而不会覆盖LoadViewState和SaveViewState等.
以上是大佬教程为你收集整理的c# – 如何自定义asp.net DropDownList以支持自定义数据绑定字段全部内容,希望文章能够帮你解决c# – 如何自定义asp.net DropDownList以支持自定义数据绑定字段所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。