Monday, January 12, 2009

Container and DataBinder

There are a couple of ways for a data-bound control to access each of the data item. DataBinder.Eval() method can retrieve the value of specified property. By specifying a string format, you can generate required string with that value.

Container.DataItem refers to a single bound data item. You can cast the data item into a strong-typed object, and then you get full access to that object.

List 1 shows a Repeater control bound with an ArrayList of ReportInfo objects.

// List 1: access bound dataitem from a Repeater control
<asp:repeater id="rptReports6" Runat="server">
<HeaderTemplate>
<H2 class="tabletitle">Maintenance:</H2>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<!--
<a href="Report.aspx?ReportID=<%# ((ReportInfo)Container.DataItem).ReportID %>">
<%# ((ReportInfo)Container.DataItem).ReportName %>
</a>
-->

<asp:HyperLink ID="lnkMaintanceItem" runat="server" NavigateUrl= '<%# DataBinder.Eval(Container.DataItem,"ReportID","Report.aspx?ReportID={0}" ) %>'>
<%# DataBinder.Eval(Container.DataItem,"ReportName","{0}" ) %>
</asp:HyperLink>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:repeater>

No comments: