访问Repeater <li>中的<li>类

时间:2011-04-04 22:01:58

标签: asp.net repeater itemtemplate

我有一个包含ItemTemplate且具有<li>'s的Repeater。我需要在RepeaterProdView__ItemDataBound事件中访问此<li>

<asp:Repeater ID="RepeaterProdView" runat="server" OnItemDataBound="RepeaterProdView__ItemDataBound">   
<ItemTemplate>
<li id="liProdView" class="prodviewwide" runat="server">
<a href="#" onClick="javascript:swapPicture('../images/products/<%#Eval("option1Other")%>','../images/products/<%#Eval("option1Other2")%>','masterLink')">
<img src="images/products/<%#Eval("option1Desc")%>" height="125" />
</a>
</li>
</ItemTemplate>
</asp:Repeater>

我想动态更改班级

<li id="liProdView" class="prodviewshort" runat="server">

2 个答案:

答案 0 :(得分:3)

RepeaterProdView__ItemDataBound方法中:

var liProdView = e.Item.FindControl("liProdView") as HtmlGenericControl;

if (liProdView != null)
{
    liProdView.Attributes["class"] = "prodviewshort";
}

应该这样做。

答案 1 :(得分:3)

e.Item.FindControl("liProdView) liProdView.Attributes.Add("class", "yourClass"); find the control in ItemDataBound。 我认为它是一个HtmlGenericControl,但我不确定。如果你将它转换为Web.UI.HtmlControls.HtmlControl并设置它的class-attribute就足够了:

{{1}}