1

我需要属性class(如果它不存在)位于中继器中的表行。代码里面有个问题。

aspx

<asp:Repeater runat="server">........
  <ItemTemplate> .....
      <tr runat="server" id="row" >
      .....................

代码隐藏

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

                var row= e.Item.FindControl("row");
                //how do I use it? How do I add "class" (if not exists) to "row"?
            }
        }
4

1 回答 1

1

该对象是一个 HtmlDataRow:

HtmlTableRow row = e.Item.FindControl("row") as HtmlTableRow;
if (row != null )
{
    if (string.isNullOrEmpty(row.Attributes["class"])) 
    {
         row.Attributes["class"] = "myClass"; 
    }
    else
    {
         row.Attributes["class"] = string.Format( "{0} {1}", row.Attributes["class"], "myClass");
    }
 }
于 2012-07-13T12:50:24.960 回答