Dynamically alter markup when loading an ASP.NET page
ASP.NET v2
I have MasterPage which includes the navigation bar for the site along the lines of:
<ul>
<li id="current"><a href="overview.aspx">Home</a></li>
<li><a href="users.aspx">Users</a></li>
<li><a href="courses.aspx">Courses</a></li>
</ul>
The css styles the list and id="current" is require开发者_运维问答d to highlight the current page. What is the best way to manipulate the markup so the relevant list item has is assigned id="current" within each page.
Try sitmap. Implemenation will be very easy.
look at: https://web.archive.org/web/20210510020922/http://aspnet.4guysfromrolla.com/articles/111605-1.aspx
Either use a sitemap or create the control in your code behind in the Page.prerender method.
Your html should look like this:
<ul>
<li runat="server" id="navul_home">home</li>
<li runat="server" id="navul_users">users</li>
<li runat="server" id="navul_courses">courses</li>
</ul>
And the codebehind should be:
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
Dim Currentpage As String = "users"
navul_home.Attributes.Clear()
navul_home.Attributes.Clear()
navul_home.Attributes.Clear()
Select Case Currentpage
Case "home"
navul_home.Attributes.Add("id", "current")
Case "users"
navul_users.Attributes.Add("id", "current")
Case "courses"
navul_courses.Attributes.Add("id", "current")
End Select
End Sub
精彩评论