gpt4 book ai didi

c# - 如何自定义asp.net DropDownList 以支持自定义数据绑定(bind)字段

转载 作者:行者123 更新时间:2023-11-30 14:16:08 25 4
gpt4 key购买 nike

有什么方法可以自定义 asp.net 下拉列表以支持自定义数据绑定(bind)字段。它不应该是控件的自定义属性。它应该能够通过 DataBind(); 方法与相同的数据源绑定(bind)数据。在此自定义服务器控件中,我尝试访问新的自定义字段,并根据该值对数据源的特定行进行一些计算。

标准控制代码

<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>

就我而言,我绑定(bind)到 List<Employee>具有一些属性,例如 Name , DepartmentImageUrl .呈现下拉列表后如下所示:

<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 向我发送了一个关于如何在 CustomProperty 事件上检索 SelectedIndexChanged 的问题)

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++;
}
}
}
}

然后您访问 CustomPropertySelectedIndexChanged这样:

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
}
}

免责声明:这不是唯一的方法,但它是我能想到的最简单的方法,无需覆盖 LoadViewStateSaveViewState

关于c# - 如何自定义asp.net DropDownList 以支持自定义数据绑定(bind)字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8196899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com