Data Binding Syntax: <%#  %>


With ASP.NET data binding, you can bind any server control to simple properties, collections, expressions and/or methods. The data binding enables page developers to hierarchically bind control properties to data container values. Code located within a <%# %> code block is only executed when the DataBind method of its parent control container is invoked. The following example demonstrates how to use the data binding syntax within an <asp:datalist runat="server"> control:

reference5_cs.aspx

Web
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Collections" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script language="C#" runat="server">
 protected void Page_Load( object sender, EventArgs e ) {
  ArrayList Items = new ArrayList( );
  Items.Add( "One" );
  Items.Add( "Two" );
  Items.Add( "Three" );
  MyList.DataSource = Items;
  MyList.DataBind( );
 }
</script>

<html xmlns="http://www.w3.org/1999/xhtml"> 
 <body> 
  <asp:datalist id="MyList" runat="server">
   <ItemTemplate>
    Here is a value: <%# Container.DataItem %>
   </ItemTemplate>
  </asp:datalist>
 </body>
</html>