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

时间:2011-09-01 12:08:31

标签: asp.net vb.net asp.net-4.0 htmlcontrols

我正在尝试在Page_PreRender上运行一些代码,但只希望它在特定DIV中的超链接上运行。

如果NavigateUrl =用户所在页面的网址,代码所做的就是更改超链接的颜色。

我有一些可行的代码,但它改变了页面上每个链接的颜色,当我只希望它在某个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

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

不确定这是不是这样,但它似乎没有用。

感谢您的帮助。

学家

1 个答案:

答案 0 :(得分:1)

您需要向div添加runat =“server”标记并为其指定ID。一旦你这样做,你可以找到这样的DIV:

编辑:使用Panel而不是DIV,并将HyperLink控件添加到Panel,如下所示:

<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 />")

但是当我向div添加runat =“server”时,div中的超链接不再被写出来。

<强> UPDATE2 在你的帮助下到达那里,面板位确实有效,谢谢。 最终守则:

    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