Click here to Skip to main content
15,880,796 members
Articles / Web Development / XHTML

ASP.NET PartialHyperLink Control

Rate me:
Please Sign up or sign in to vote.
4.84/5 (11 votes)
16 Jun 2009CPOL2 min read 40.1K   211   33   8
Introducing a small user control to simplify localization of literal text with contained hyperlinks
Image 1

Introduction

Recently I has the need to localize some .ASPX files. I did that the standard way by using the "Generate local resources" tool bar button in the design view of an .ASPX file inside Visual Studio .NET 2008. 

Among others, I used the ASP.NET HyperLink control for hyperlinks and the ASP.NET Localize control for literal text.

What bothered me was that I often had a sentence with a hyperlink inside. E.g. "To get further information, please visit the support pages on our web site".

In the above example, I would have to split the sentence into two Localize controls and one HyperLink control. Too much effort for a lazy developer. In addition, the "split position" could vary among the different languages and the hyperlink also could be different in each language.

Introducing the PartialHyperLink Control

To make the described scenario more easy to handle, I wrote myself a light-weight control named "PartialHyperLink".

The control consists of one single .CS file that you can drop into your (web) project and use immediately.

It contains the following features:

  • Mimics the most common features of a standard HyperLink control
  • Has a NavigateUrl property that is localizable
  • Has a Text property that can include a start and an end placeholder to indicate where you want the clickable hyperlink

Using the Code

To use the control, simply put the "PartialHyperLink.cs" file into the "App_Code" folder of your web project.

Next, register the control on each .ASPX page you want to use it, by writing the following line at the top of the page:

ASP.NET
<%@ Register Namespace="App_Code" TagPrefix="zeta" %>

Last, create an instance of the control by writing code similar to the following one:

ASP.NET
<zeta:PartialHyperLink 
    runat="server" 
    ID="MyHyperLink" 
    Text="This text that {0}includes a hyperlink{1} is inside a single control"
    NavigateUrl="http://www.zeta-test.com" 
    meta:resourcekey="MyHyperLinkResource1" />

Use the placeholders "{0}" and "{1}" inside the Text property to mark the start and end of the clickable hyperlink.

That's all!

I've included a small example project to demonstrate how to use the control. If you open and run the project's solution, try switching the languages of your browser to see the different texts and hyperlinks that are loaded from the resources. I've included the languages English and German to give you an idea.

Conclusion

In this article, I have shown you a small user control that tries to simplify the task of translating literal texts with hyperlinks.

Maybe I am on a totally wrong path and there is already a built-in way of doing things that I implemented in my control. If yes, please tell me, I'd love to know!

In addition, if you have any questions, comments, bug reports or suggestions, please write them below in the comments section of this article. Thank you!

History

  • 2009-06-12
    • First published on The Code Project

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Zeta Software GmbH
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions

 
GeneralMy vote of 4 Pin
Doug Domeny22-Jun-11 11:17
professionalDoug Domeny22-Jun-11 11:17 
GeneralAwesome Pin
jasonp1229-Dec-09 7:37
jasonp1229-Dec-09 7:37 
GeneralRe: Awesome Pin
Uwe Keim29-Dec-09 8:20
sitebuilderUwe Keim29-Dec-09 8:20 
GeneralNice idea, two small problems Pin
Richard Deeming16-Jun-09 3:26
mveRichard Deeming16-Jun-09 3:26 
GeneralRe: Nice idea, two small problems Pin
Uwe Keim16-Jun-09 3:35
sitebuilderUwe Keim16-Jun-09 3:35 
GeneralRe: Nice idea, two small problems Pin
Uwe Keim16-Jun-09 4:20
sitebuilderUwe Keim16-Jun-09 4:20 
GeneralGreat Idea Pin
Peter Lange13-Jun-09 9:52
Peter Lange13-Jun-09 9:52 
GeneralRe: Great Idea Pin
Uwe Keim13-Jun-09 9:54
sitebuilderUwe Keim13-Jun-09 9:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.