Page_PreRender 通过 id 查找 div (asp.net VB)
我试图在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要向 div 添加 runat="server" 标记并为其指定 ID。完成此操作后,您可以找到如下所示的 DIV:
编辑:使用面板而不是 DIV,并将超链接控件添加到面板,如下所示:
然后在后面的代码中执行以下操作:
更新
我在迭代链接时添加了一些代码:
但是当我将 runat="server" 添加到 div 时,div 中的超链接不再写出。
更新2
在您的帮助下到达那里,面板确实有效,谢谢。
最终代码:
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:
Then in your code behind, do this:
UPDATE
I added in some code when iterating through the links:
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: