Home > DeveloperSection > Forums > Injecting table row inside repeater item
marcel ethan
marcel ethan

Total Post:105

Points:735
Posted on    August-27-2014 1:50 AM

 ASP.Net
Ratings:


 1 Reply(s)
 666  View(s)
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>



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    August-27-2014 3:02 AM

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).


Don't want to miss updates? Please click the below button!

Follow MindStick