15-May-07 (Created: 15-May-07) | More in 'CS-dotnet'

data binding samples

Simple data binding


<%# Container.DataItem("expression") %>

Any expression that will result in a string will do.

Example


<asp:Label id="label1" runat="server"
     Text='<%# Container.DataItem("CustomerName") %>'
</asp:Label>

Example2


<asp:Label id="label1" runat="server"
     Text='<%# DataBinder.Eval(Container.DataItem
	 			, "OrderDate"
				,"{0:dddd d MMMM}") %>'
</asp:Label>

Using eval


<%# Eval("Price", "{0:C}") %>
<%# Eval("Price", "Special Offer {0:C} for Today Only!") %>

databinder, eval, string expressions


Text='<%# 
      DataBinder.Eval(Container.DataItem, "titleofcourtesy") + 
      " " + 
      DataBinder.Eval(Container.DataItem, "firstname") + 
      " <b> " +
      DataBinder.Eval(Container.DataItem, "lastname") + 
      " </b>" 
      %>' >
				
DataBinder.Eval(Container.DataItem, "employeeid", "{0:g}")

String.Format("{0:g}", 
    ((DataRowView)Container.DataItem)["employeeid"]);
	
<template name="ItemTemplate">
<asp:checkbox runat=server enabled=false 
    checked='<% # HasBoss((int) DataBinder.Eval(
        Container.DataItem, "reportsto")) 
    %>'  />
</template>