Reference databound value in webform


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. //The simplest way is:
  2. <%# Eval("DivID") %>
  3.  
  4. //This calls DataBinder.Eval behind the scenes.
  5.  
  6. //For ultimate performance, Use explicit casting instead of DataBinder.Eval to avoid reflection:
  7. Replace:
  8. <td><%# DataBinder.Eval(Container.DataItem, "field1") %></td>
  9. with
  10. <td><%# ((DataRowView)Container.DataItem)["field1"] %></td>
  11.  
  12. //or, if using EF/LINQ, cast to the underlying datatype:
  13. <%# ((ftj.com.App_Data.ProductFAQ)Container.DataItem).Question %> //you even get intellisense support.
  14.  
  15. NOTE: For above to work, must add <%@ Import Namespace="System.Data" %> to web form. Alternatively, call via fully qualified System.Data.DataRowView.
  16.  
  17. //however, this assumes the scope of the current databind. With longer syntaxes below you can hit items in other scopes or drill deeper into current scope.
  18.  
  19. <p><strong>Member Conditions</strong></p>
  20. <asp:Repeater runat="server" ID="rptMemberCondition">
  21. <ItemTemplate><asp:HyperLink runat="server" Text='<%# DataBinder.GetPropertyValue(Container.DataItem, "ConditionDescription") %>' CausesValidation="false" NavigateUrl='<%# "~/ApplyNow/Step5.aspx?ID=" + DataBinder.GetPropertyValue(Container.DataItem, "ID") %>' /><br /></ItemTemplate>
  22. </asp:Repeater>
  23.  
  24. //Alternatively, you can use eval instead of GetPropertyValue. This is useful when the object has a property that contains a nested object. e.g.
  25.  
  26. <%# DataBinder.Eval(Container.DataItem, "ProductType.Name") %>

URL: http://weblogs.asp.net/jgalloway/archive/2005/09/20/425687.aspx

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.