从转发器的ItemDataBound事件中添加<tr>

nem*_*iss 1 asp.net

我的转发器模板生成一个表,其中每个项都是一个表行.
当满足非常特定的条件(itemdata)时,我想从此事件向表中添加一行.

我怎样才能做到这一点?

protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   if (e.Item.ItemType == ListItemType.Item ||
         e.Item.ItemType == ListItemType.AlternatingItem)
   {

        bool tmp = bool.Parse(DataBinder.Eval(e.Item.DataItem, "somedata").ToString());
        if (!tmp && e.Item.ItemIndex != 0)
        {
            //Add row after this item
        }

   }
}
Run Code Online (Sandbox Code Playgroud)

我可以使用e.Item.Controls.Add()并添加TableRow但为此我需要找到一个正确的表?
我怎么解决这个问题?

Sun*_*nny 6

我会在转发器项目模板中放入控件,将其可见性设置为隐藏&仅在这种情况下,我将显示它...

喜欢:

<asp:Repeater ...>

..

<ItemTemplate>

 <tr>
   <td>...</td>
 </tr>
 <tr runat="server" id="tr_condition" Visible="false">
  <td> Show only in specific condition </td>
 </tr>
Run Code Online (Sandbox Code Playgroud)

然后在ItemDataBound事件中:

var tr_condition = e.Item.FindControl("tr_condition") as HtmlTableRow;
tr_condition.Visible = myCondition;
Run Code Online (Sandbox Code Playgroud)

其中myCondition是一个在特定条件下获取集合的标志

HTH