Page_PreRender 通过 id 查找 div (asp.net VB)

发布于 2024-12-02 05:50:49 字数 2248 浏览 1 评论 0原文

我试图在 Page_PreRender 上运行一些代码,但只希望它在某个 DIV 内的超链接上运行。

该代码的作用是,如果 NavigateUrl = 用户所在页面的 URL,则更改超链接的颜色。

我有一些有效的代码,但当我只希望它发生在某个 div 内时,它会更改页面上匹配的每个链接的颜色。

我想要更改超链接的 DIV ID 是“subNav”

当前代码

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

        Dim filePath As String = "/~" & System.Web.HttpContext.Current.Request.Path

        Dim strControlType As String
        For Each ctrl As Control In Me.Controls
            For Each subctrl As Control In ctrl.Controls
                strControlType = Convert.ToString(subctrl.[GetType]())
                If strControlType = "System.Web.UI.WebControls.HyperLink" Then
                    If filePath = "/" & DirectCast(subctrl, HyperLink).NavigateUrl Then
                        'DirectCast(subctrl, HyperLink).CssClass = "active"
                        DirectCast(subctrl, HyperLink).Attributes.Add("style", "color:#993366")
                        'Label2.Text = "/" & DirectCast(subctrl, HyperLink).NavigateUrl
                    End If

                End If
            Next
        Next

    End Sub

代码我正在尝试

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

    Dim filePath As String = "/~" & System.Web.HttpContext.Current.Request.Path

    Dim strControlType As String

    Dim subNavDiv As HtmlControl = CType(FindControl("subNav"), HtmlControl)
    For Each ctrl As Control In subNavDiv.Controls
        For Each subctrl As Control In ctrl.Controls
            strControlType = Convert.ToString(subctrl.[GetType]())
            If strControlType = "System.Web.UI.WebControls.HyperLink" Then
                If filePath = "/" & DirectCast(subctrl, HyperLink).NavigateUrl Then
                    'DirectCast(subctrl, HyperLink).CssClass = "active"
                    DirectCast(subctrl, HyperLink).Attributes.Add("style", "color:#993366")
                    'Label2.Text = "/" & DirectCast(subctrl, HyperLink).NavigateUrl
                End If

            End If
        Next
    Next

End Sub

不确定这是否是解决问题的方法,但它似乎不起作用。

感谢您的任何帮助。

J。

I am trying to run some code on Page_PreRender but only want it to run on hyperlinks within a certain DIV.

What the code does is change the colour of a hyperlink if the NavigateUrl = the URL of the page the user is on.

I have some code that works but it changes the colour of every link on the page that matches when I only want it to happen within a certain div.

The DIV ID i want the hyperlinks changed in is 'subNav'

CURRENT CODE

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

        Dim filePath As String = "/~" & System.Web.HttpContext.Current.Request.Path

        Dim strControlType As String
        For Each ctrl As Control In Me.Controls
            For Each subctrl As Control In ctrl.Controls
                strControlType = Convert.ToString(subctrl.[GetType]())
                If strControlType = "System.Web.UI.WebControls.HyperLink" Then
                    If filePath = "/" & DirectCast(subctrl, HyperLink).NavigateUrl Then
                        'DirectCast(subctrl, HyperLink).CssClass = "active"
                        DirectCast(subctrl, HyperLink).Attributes.Add("style", "color:#993366")
                        'Label2.Text = "/" & DirectCast(subctrl, HyperLink).NavigateUrl
                    End If

                End If
            Next
        Next

    End Sub

CODE IM TRYING

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

    Dim filePath As String = "/~" & System.Web.HttpContext.Current.Request.Path

    Dim strControlType As String

    Dim subNavDiv As HtmlControl = CType(FindControl("subNav"), HtmlControl)
    For Each ctrl As Control In subNavDiv.Controls
        For Each subctrl As Control In ctrl.Controls
            strControlType = Convert.ToString(subctrl.[GetType]())
            If strControlType = "System.Web.UI.WebControls.HyperLink" Then
                If filePath = "/" & DirectCast(subctrl, HyperLink).NavigateUrl Then
                    'DirectCast(subctrl, HyperLink).CssClass = "active"
                    DirectCast(subctrl, HyperLink).Attributes.Add("style", "color:#993366")
                    'Label2.Text = "/" & DirectCast(subctrl, HyperLink).NavigateUrl
                End If

            End If
        Next
    Next

End Sub

Not sure if this is the way to go about it or not, but it doesn't seem to be working.

Thanks for any help.

J.

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

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

发布评论

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

评论(1

油焖大侠 2024-12-09 05:50:49

您需要向 div 添加 runat="server" 标记并为其指定 ID。完成此操作后,您可以找到如下所示的 DIV:

编辑:使用面板而不是 DIV,并将超链接控件添加到面板,如下所示:

<asp:Panel ID="pnlLinks" runat="server">
    <asp:HyperLink ID="lnk1" runat="server" Text="Link 1" />
    <asp:HyperLink ID="lnk2" runat="server" Text="Link 2" />
</asp:Panel>

然后在后面的代码中执行以下操作:

For Each lnk As HyperLink In pnlLinks.Controls.OfType(Of HyperLink)()
    lnk.NavigateUrl = "/somefolder/somepage.aspx"
Next

更新
我在迭代链接时添加了一些代码:

Response.Write(DirectCast(subctrl, HyperLink).NavigateUrl & "<br />")

但是当我将 runat="server" 添加到 div 时,div 中的超链接不再写出。

更新2
在您的帮助下到达那里,面板确实有效,谢谢。
最终代码:

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

    Dim filePath As String = "/~" & System.Web.HttpContext.Current.Request.Path

    For Each lnk As HyperLink In subNav.Controls.OfType(Of HyperLink)()
        If filePath = "/" & lnk.NavigateUrl Then
            DirectCast(lnk, HyperLink).CssClass = "active"
        End If
    Next

End Sub

You will need to add a runat="server" tag to the div and give it an ID. Once you do that, you can find the DIV like this:

EDIT: Use Panel instead of DIV, and add HyperLink controls to the Panel, like this:

<asp:Panel ID="pnlLinks" runat="server">
    <asp:HyperLink ID="lnk1" runat="server" Text="Link 1" />
    <asp:HyperLink ID="lnk2" runat="server" Text="Link 2" />
</asp:Panel>

Then in your code behind, do this:

For Each lnk As HyperLink In pnlLinks.Controls.OfType(Of HyperLink)()
    lnk.NavigateUrl = "/somefolder/somepage.aspx"
Next

UPDATE
I added in some code when iterating through the links:

Response.Write(DirectCast(subctrl, HyperLink).NavigateUrl & "<br />")

But when I added runat="server" to the div the hyperlinks I within the div were no longer writen out.

UPDATE2
Got there with your help, the panel bit definitely worked, thanks.
Final Code:

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

    Dim filePath As String = "/~" & System.Web.HttpContext.Current.Request.Path

    For Each lnk As HyperLink In subNav.Controls.OfType(Of HyperLink)()
        If filePath = "/" & lnk.NavigateUrl Then
            DirectCast(lnk, HyperLink).CssClass = "active"
        End If
    Next

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文