0

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.

Jammer
  • 2,330
  • 11
  • 48
  • 77
  • In general the method seems correct. However, you don't give enough information about the result when you state "it doesn't seem to be working". Do you mean that it doesn't change the color of any element in the subNav? Or that it changes also elements outside subNav? Or that it gives a syntax error? Also a sample of the aspx page might be useful. – Durden81 Sep 01 '11 at 12:53
  • Well the first lot of code works but on every hyperlink, the problem with the second lot of code seems to be that the hyperlinks within the 'SubNav' div don't get iterated through once i change it to runat="server"...confused! – Jammer Sep 01 '11 at 14:09

1 Answers1

1

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
Jammer
  • 2,330
  • 11
  • 48
  • 77
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • No, didn't do the trick. I was writing out each NavigateUrl on Pre_Render and once I had changed the div to runat="server" it didn't include the links within that div. Thanks for the input though. – Jammer Sep 01 '11 at 14:14
  • What do you mean it didn't include the links in that div? There's something wrong with the code somewhere if that's happening. Try using a Panel instead, because it outputs a div anyway. – James Johnson Sep 01 '11 at 14:17
  • Got there! thanks for your help. Final code is in edited answer. – Jammer Sep 01 '11 at 14:55