Asp.net 在中继器项内注入表行

Asp.net 在中继器项内注入表行,asp.net,repeater,Asp.net,Repeater,我试图在repeaters项目内部注入,问题是该行在错误的位置生成。 如果您尝试这个简单的示例,您将看到rown是在标签“2”下生成的,因为它应该在标签“1”下生成 为什么会这样?如何解决这个问题 protected void Page_Load(object sender, EventArgs e) { int [] array = {1,2,3,4,5}; rpt.DataSource = array; rpt.DataBind(); } protecte

我试图在repeaters项目内部注入,问题是该行在错误的位置生成。
如果您尝试这个简单的示例,您将看到rown是在标签“2”下生成的,因为它应该在标签“1”下生成

为什么会这样?如何解决这个问题

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>
受保护的无效页面加载(对象发送方,事件参数e)
{
int[]数组={1,2,3,4,5};
rpt.DataSource=数组;
rpt.DataBind();
}
受保护字符串_extrowHTML;
受保护的void rpt_ItemDataBound(对象发送方,RepeaterItemEventArgs e)
{
如果(e.Item.ItemType==ListItemType.Item||
e、 Item.ItemType==ListItemType.AlternatingItem)
{           
int tmp=((int)e.Item.DataItem);
_extrowHTML=tmp==1?“主题”:string.Empty;
}
}

这样做很容易出错,因为以这种方式使用变量并不是完全可以控制的

我要做的是在模板中添加如下内容

<asp:literal id="litExtraRow" runat="server" mode="Passthrough" />
像这样的东西应该更可靠一些


出现问题的原因是,模板使用值进行评估,因为调用了“ItemDataBound”,第1行的值为string.Empty,第二行的值是在项目1被数据绑定后更新的。

我个人认为更好的方法是替换:

<%# _extraRowHtml %>


然后在代码隐藏中实现:

protected string GetExtraRow(object Data)
{
    int tmp = (int) Data:
    return _tmp == 1 ? 
        "<tr><td class=\"topicsRow\" colspan=\"100%\"> Topics </td></tr>" : 
        string.Empty;
}
受保护的字符串GetExtraRow(对象数据)
{
int tmp=(int)数据:
返回值_tmp==1?
“主题”:
字符串。空;
}

然后删除
rpt_ItemDataBound
函数(以及
onItemDataBound

你指的是什么标签?我不想有一个特殊的服务控件(asp:literal),因为rown应该生成一次。。。所以我不希望控件出现在页面的控件树中
<%# GetExtraRow(Container.DataItem) %>
protected string GetExtraRow(object Data)
{
    int tmp = (int) Data:
    return _tmp == 1 ? 
        "<tr><td class=\"topicsRow\" colspan=\"100%\"> Topics </td></tr>" : 
        string.Empty;
}