Few random development Tips-N-Tricks-7

Some scenarios with Container.DataItem.
Container.DataItem represents each row of the underlying Datasource of any Databound Control. Below are few examples, as how this helps.

1) Using Container.DataItem to set css of any row in any Databound control.
Below here it sets a different class for the last row in a Repeater.

<asp:Repeater ID="rpt1" runat="server">
<ItemTemplate>
  ccc <%# ((DataRowView)(Container.DataItem)).DataView.Count %>
  <br />
  dddd <%# Container.ItemIndex%>
  <br />
  <li class='<%#(Container.ItemIndex == (((DataRowView)(Container.DataItem)).DataView.Count - 1)) ? "last" : "" %>' > 
             <%# ((DataRowView)Container.DataItem)["product_name"]%> 
  </li>
</ItemTemplate>
</asp:Repeater>

2) Using Container.DataItem to set alternate row css.

<ItemTemplate>
<DIV id="r11" runat="server" class='<%#(Container.DataItemIndex % 2 == 0) ? "class1" : "class2" %>'>
</ItemTemplate>

3) Using Container.DataItem to bind any Databound control to an array, say ArrayList.

<asp:Repeater ID="rpt1" runat="server">
<ItemTemplate>
    <%# Container.DataItem %>
    <br />
</ItemTemplate>
</asp:Repeater>

protected void Page_Load(object sender, EventArgs e)
{
    ArrayList al = new ArrayList();
    al.Add("Data1");
    al.Add("Data2");
    al.Add("Data3");
    rpt1.DataSource = al;
    rpt1.DataBind();
}

4) Using Container.DataItem to bind any nested control — say Repeater inside a ListView

<asp:ListView runat="server" ID="lv1" >
<LayoutTemplate>
<table>
    <tr id="itemPlaceholder" runat="server"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<table>
<tr class='<%# Convert.ToBoolean(Container.DataItemIndex % 2) ? "RowOdd" : "RowEven" %>' >
    <td ><%#Eval("CategoryID", "{0:d}")%></td>
    <td ><%# Eval("CategoryName")%></td>
    <td ><%# Eval("Description")%></td>
</tr>
</table>
<asp:Repeater ID="rptProducts" runat="server" DataSource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>'>
    <ItemTemplate>
    <tr >
    <td><%# DataBinder.Eval(Container.DataItem, "[\"ProductID\"]")%></td>
    <td><%# DataBinder.Eval(Container.DataItem, "[\"ProductName\"]")%></td>
    <td><%# DataBinder.Eval(Container.DataItem, "[\"QuantityPerUnit\"]")%></td>
    <td><%# DataBinder.Eval(Container.DataItem, "[\"UnitPrice\"]")%></td>
    </tr>
    </ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:ListView>

Thanks for reading.

Advertisements
This entry was posted in Tips-n-Tricks. Bookmark the permalink.

One Response to Few random development Tips-N-Tricks-7

  1. sarvesh says:

    Awesome… its help me alot :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s