有没有办法定制asp.net下拉列表来支持自定义数据绑定字段.它不应该是控件的自定义属性.它应该能够通过DataBind();具有相同数据源的方法绑定数据.在这个自定义服务器控件中,我正在尝试访问新的自定义字段,并且基于该值,我将对该数据源的特定行进行一些计算.
标准控制代码
<asp:DropDownList runat="server" DataTextField="TextField" DataValueField="ValueField"/>
Run Code Online (Sandbox Code Playgroud)
新的自定义控件应该是这样的,
<asp:DropDownList runat="server" DataTextField="TextField" DataValueField="ValueField" DataCustomField="CustomField"/>
Run Code Online (Sandbox Code Playgroud)
你可以将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++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
并在您的标记上使用它如下:
<cc1:MyDropDownList ID="MyDropDownList1" DataTextField="Name" DataValueField="Department" CustomProperty="ImageUrl" runat="server">
</cc1:MyDropDownList>
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我绑定到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>
Run Code Online (Sandbox Code Playgroud)
更新:(对于@emrahozguner,他通过Twitter向我发送了一个关于如何检索CustomPropertyon 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++;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
你这样访问CustomPropertyon SelectedIndexChanged:
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
}
}
Run Code Online (Sandbox Code Playgroud)
免责声明:这是不是这样做的唯一方式,但它是我能想到的没有覆盖最简单LoadViewState和SaveViewState等.
| 归档时间: |
|
| 查看次数: |
7208 次 |
| 最近记录: |