One Hyperlink In Many Different Locations
I'd like to implement a hyperlink in many locations on my website, however I just want to have it defined once not several times over. What is the best way to achieve this?
I started down the road of listing it in the node of web.config but I was only able to get that to list as a literal and wasn't successful in having it end up as a hyperlink.
I don't know much about master pages, but what I do know about them seems to me that they aren't the answer for 开发者_开发百科this task because they wouldn't allow for that hyperlink to be located on some pages and not others and in different locations on some pages than others.
Help please! :)
I'm working in ASP.net VB.net
Put a HyperLink
control on each page where you want it.
e.g. <asp:HyperLink runat="server" id="LogInLink">Login</asp:HyperLink>
Then either set the NavigateUrl
property on the hyperlink in code-behind, e.g. this.LogInLink.NavigateUrl = Global.MySpecialUrl;
or use <%=Global.MySpecialUrl%>
notation to reference the value you want from your code in the NavigateUrl
in the markup.
[Sorry, that's C# code]
You can create a custom control that inherits from HyperLink. This method will not require to use code-behind on individual pages but you will need to create a new class and modify your web.config file. Alter the namespaces as needed.
SpecialLink.vb
Namespace YourWebSite.Controls
Public Class SpecialLink
Inherits HyperLink
Public Sub New()
NavigateUrl = "~/SpecialLinkUri.aspx"
Text = "Special Link Text"
End Sub
End Class
End Namespace
web.config (add this to the system.web node) This allows you to use this control on any page/master page/user control throughout your site
<pages>
<controls>
<add namespace="YourWebSite.Controls" tagPrefix="YourWebSite"/>
</controls>
</pages>
Using it on your page
<p>This is some text, here's the link: <YourWebSite:SpecialLink></YourWebSite:SpecialLink></p>
<p>This is some text, <YourWebSite:SpecialLink>here's the link</YourWebSite:SpecialLink>.</p>
精彩评论