Using Eval to bind a dropdownlist
I have a dropdownlist that gets data through entity objects, but with a navigation. But I get an error trying to do this, can anyone help me please.
<asp:DropDownList ID="ddlVacancy" DataValueField="ID" DataTextField='<%# Eva开发者_如何学Gol("Position.Name") %>'
runat="server" Width="200px"/>
You can create a property in your entity object like this:
public string PositionName
{
get
{
return Position.Name;
}
}
and then replace Eval("Position.Name") with Eval("PositionName")
hope this helps
I had the same issue and I managed to create a simple control that derives from DropDownList. I also implemented an ItemDataBound Event that can help as well.
public class RTIDropDownList : DropDownList
{
public delegate void ItemDataBoundDelegate( ListItem item, object dataRow );
[Description( "ItemDataBound Event" )]
public event ItemDataBoundDelegate ItemDataBound;
protected override void PerformDataBinding( IEnumerable dataSource )
{
if ( dataSource != null )
{
if ( !AppendDataBoundItems )
this.Items.Clear();
IEnumerator e = dataSource.GetEnumerator();
while ( e.MoveNext() )
{
object row = e.Current;
var item = new ListItem( DataBinder.Eval( row, DataTextField, DataTextFormatString ).ToString(), DataBinder.Eval( row, DataValueField ).ToString() );
this.Items.Add( item );
if ( ItemDataBound != null ) //
ItemDataBound( item, row );
}
}
}
}
精彩评论