我正在生成一个带有Repeater 控件的菜单,该控件绑定到XmlDataSource。
<asp:Repeater ID="myRepeater" runat="server"
DataSourceID="myDataSource"
onitemdatabound="myRepeater_ItemDataBound"
onitemcreated="myRepeater_ItemCreated">
<HeaderTemplate>
<ul class="menu_list">
</HeaderTemplate>
<ItemTemplate>
<li id="liMenu" runat="server"><asp:HyperLink ID="hrefMenuItem" runat="server" Text='<%# XPath("@text")%>' NavigateUrl='<%# XPath("@href")%>'></asp:HyperLink></li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
<asp:XmlDataSource runat="server" ID ="myDataSource" XPath="Menu/Items/*" EnableCaching="False" />
我希望能够根据鼠标悬停事件和当前选择的菜单项设置包含 LI 的样式。我通过 HtmlGenericControl 尝试过,但我收到一个错误,它是只读的。
protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HyperLink hrefCurrentMenuLink = e.Item.FindControl("hrefMenuItem") as HyperLink;
HtmlGenericControl l_genericControl = e.Item.FindControl("liMenu") as HtmlGenericControl;
if ((hrefCurrentMenuLink != null) && (l_genericControl != null))
{
string l_currentPage = GetCurrentWebPage();
if (String.Compare(Path.GetFileNameWithoutExtension(hrefCurrentMenuLink.NavigateUrl), l_currentPage, StringComparison.OrdinalIgnoreCase) == 0)
l_genericControl.Style = "on-nav";
else
l_genericControl.Style = "off-nav";
l_genericControl.Attributes.Add("onmouseover", "navOn(this)");
l_genericControl.Attributes.Add("onmouseout", "navOff(this)");
}
}
}
有没有办法做到这一点?