INJECTING TABLE ROW INSIDE REPEATER ITEM

marcel ethan

Total Post:105

Points:735
Posted by  marcel ethan
 953  View(s)
Ratings:
Rate this:

I am trying to inject inside repeaters items, the problem is that the row is generated in a wrong location.

If you try this simple example, you will see that the rown is generated under label '2' as it should be generated under label '1'.

Why does it happen? And how to fix that?

protected void Page_Load(object sender, EventArgs e)

{

    int [] array = {1,2,3,4,5};

    rpt.DataSource = array;

    rpt.DataBind();    

}

protected string _extraRowHtml;

protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)

{

   if (e.Item.ItemType == ListItemType.Item ||

          e.Item.ItemType == ListItemType.AlternatingItem)

  {          

       int tmp = ((int)e.Item.DataItem);

       _extraRowHtml = tmp == 1 ? "<tr><td class=\"topicsRow\" colspan=\"100%\"> Topics </td></tr>" : string.Empty; ;               

  }

}

<asp:Repeater ID="rpt" runat="server" onitemdatabound="rpt_ItemDataBound">

    <HeaderTemplate>

       <table  cellpadding="0px" cellspacing="0">            

    </HeaderTemplate>           

    <ItemTemplate>    

      <tr style="height:50px">           

        <td> 

          <asp:HyperLink  ID="lnkTitle" runat="server" Text='<%# Container.DataItem%>'/>             

         </td>             

       </tr>

       <%# _extraRowHtml %>  

    </ItemTemplate>       

  <FooterTempl

    </table>

  </FooterTemplate>

</asp:Repeater>

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: Injecting table row inside repeater item

    Hi Marcel,

    replace:

    <%# _extraRowHtml %>

    with

    <%# GetExtraRow(Container.DataItem) %>

    then in your code behind implement:

    protected string GetExtraRow(object Data)

    {

        int tmp = (int) Data:

        return _tmp == 1 ?

            "<tr><td class=\"topicsRow\" colspan=\"100%\"> Topics </td></tr>" :

            string.Empty;

    }

    Then and get rid of the rpt_ItemDataBound function (and the onItemDataBound).

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!