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">
  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"]%> 

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

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

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

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

protected void Page_Load(object sender, EventArgs e)
    ArrayList al = new ArrayList();
    rpt1.DataSource = al;

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

<asp:ListView runat="server" ID="lv1" >
    <tr id="itemPlaceholder" runat="server"></tr>
<tr class='<%# Convert.ToBoolean(Container.DataItemIndex % 2) ? "RowOdd" : "RowEven" %>' >
    <td ><%#Eval("CategoryID", "{0:d}")%></td>
    <td ><%# Eval("CategoryName")%></td>
    <td ><%# Eval("Description")%></td>
<asp:Repeater ID="rptProducts" runat="server" DataSource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>'>
    <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>

Thanks for reading.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s