code_junkie

Bind ASP.NET DropDownList DataTextField to method?

Nov 11th, 2011
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. <asp:DropDownList ID="dropDownList" runat="server" DataSourceID="dataSource" DataValueField="DataValueField" DataTextField="DataTextField" />
  2. <asp:ObjectDataSource ID="dataSource" runat="server" SelectMethod="SelectForDataSource" TypeName="CategoryDao" />
  3.  
  4. public IEnumerable<object> SelectForDataSource()
  5. {
  6. return _repository.Search().Select(x => new{
  7. DataValueField = x.CategoryId,
  8. DataTextField = x.ToString() // Here is the trick!
  9. }).Cast<object>();
  10. }
  11.  
  12. <asp:DropDownList ID="ddlType" runat="server" Width="250px" AppendDataBoundItems="true" DataSourceID="dsTypeList" DataTextField="Description" DataValueField="ID">
  13. <asp:ListItem Value="0">All Categories</asp:ListItem>
  14. </asp:DropDownList><br />
  15. <asp:ObjectDataSource ID="dsTypeList" runat="server" DataObjectTypeName="MyType" SelectMethod="GetList" TypeName="MyTypeManager">
  16. </asp:ObjectDataSource>
Add Comment
Please, Sign In to add comment