Entering a table row inside a repeater element

I am trying to enter within repeaters elements, the problem is that the line is being created in the wrong place.
If you try this simple example, you will see that the outlet is created under the label "2", since it should be generated under the label "1".

Why is this happening? And how to fix it?

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>
+3
source share
2 answers

Personally, I think the best way is to replace:

<%# _extraRowHtml %>

with

<%# GetExtraRow(Container.DataItem) %>

then in your code behind the implementation:

protected string GetExtraRow(object Data)
{
    int tmp = (int) Data:
    return _tmp == 1 ? 
        "<tr><td class=\"topicsRow\" colspan=\"100%\"> Topics </td></tr>" : 
        string.Empty;
}

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

+3
source

, -, .

, - .

<asp:literal id="litExtraRow" runat="server" mode="Passthrough" />

if (((int)e.Item.DataItem) == 1)
{
    ((Literal)e.Item.FindControl("litExtraRow")).Text = "Your HTML Here";
}

- .

, , , , "ItemDataBound", 1 - string.Empty, , 1 .

+1

Source: https://habr.com/ru/post/1740324/


All Articles