ASP.NET:以编程方式设置 HTML 元素的样式

发布于 2024-07-09 04:17:37 字数 2050 浏览 8 评论 0原文

我正在生成一个菜单,其中的 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)");
                }
            }
        }

有什么办法可以做到这一点吗?

I'm generating a menu with a Repeater control bound to an 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" /> 

I'd like to be able to set the style of the containing LI based on mouseover events and currently selected menu item. I tried via the HtmlGenericControl, but I receive an error that it's readonly.

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)");
                }
            }
        }

Is there any way to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

李白 2024-07-16 04:17:37

style 属性是一个集合。 这样做:

l_genericControl.Style.Add("css-name", "css-value")

或者,如果您使用 CSS 类,则更改 CssClass 属性:

l_genericControl.CssClass = "on-nav";

如果您尝试使用 javascript 切换 CSS 类,请尝试如下操作(未经测试):

l_genericControl.Attributes.Add("onmouseover", "this.className='on-nav';");
l_genericControl.Attributes.Add("onmouseout", "this.className='off-nav';");

如果您想使用 javascript 更改样式,这可能有效:

l_genericControl.Attributes.Add("onmouseover", "this.style.color='red'; this.style.backgroundColor='yellow';");
l_genericControl.Attributes.Add("onmouseout", "this.style.color='black'; this.style.backgroundColor='none';");

The style property is a collection. Do this:

l_genericControl.Style.Add("css-name", "css-value")

Or if you are using CSS classes, then change the CssClass property:

l_genericControl.CssClass = "on-nav";

If you're trying to toggle the CSS class with your javascript, try something like this (untested):

l_genericControl.Attributes.Add("onmouseover", "this.className='on-nav';");
l_genericControl.Attributes.Add("onmouseout", "this.className='off-nav';");

If you want to change the style with your javascript, this might work:

l_genericControl.Attributes.Add("onmouseover", "this.style.color='red'; this.style.backgroundColor='yellow';");
l_genericControl.Attributes.Add("onmouseout", "this.style.color='black'; this.style.backgroundColor='none';");
一抹苦笑 2024-07-16 04:17:37

在基本层面上,你可以这样做:

l_genericControl.Attributes["class"] = "on-nav";

at the basic level, you could do:

l_genericControl.Attributes["class"] = "on-nav";
傲影 2024-07-16 04:17:37

问题出在这部分:

l_genericControl.Style = "off-nav";

您可以通过应用 CssClass 来修复

// += to prevent overwriting a class you would set in the markup
l_genericControl.CssClass += "off-nav";

The problem is with this part:

l_genericControl.Style = "off-nav";

which you can fix by applying a CssClass instead

// += to prevent overwriting a class you would set in the markup
l_genericControl.CssClass += "off-nav";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文