Click here to Skip to main content
Click here to Skip to main content

ASP.NET PartialHyperLink Control

, 16 Jun 2009
Rate this:
Please Sign up or sign in to vote.
Introducing a small user control to simplify localization of literal text with contained hyperlinks

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:

<%@ Register Namespace="App_Code" TagPrefix="zeta" %>

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

<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)

About the Author

Uwe Keim
Chief Technology Officer Zeta Producer Desktop CMS
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.
 
In his free time, he does climbing, running and mountain biking. Recently he became a father of a cute boy.
 
Some cool, free software from us:
 
Free Test Management Software - Intuitive, competitive, Test Plans. Download now!  
Homepage erstellen - Intuitive, very easy to use. Download now!  
Send large Files online for free by Email
Offline-Homepage-Baukasten

Comments and Discussions

 
GeneralMy vote of 4 PinmemberDoug D in NH22-Jun-11 11:17 
GeneralAwesome Pinmemberjasonp1229-Dec-09 7:37 
GeneralRe: Awesome PinsitebuilderUwe Keim29-Dec-09 8:20 
GeneralNice idea, two small problems PinmemberRichard Deeming16-Jun-09 3:26 
GeneralRe: Nice idea, two small problems PinsitebuilderUwe Keim16-Jun-09 3:35 
GeneralRe: Nice idea, two small problems PinsitebuilderUwe Keim16-Jun-09 4:20 
GeneralGreat Idea PinmemberPeter Lange13-Jun-09 9:52 
GeneralRe: Great Idea PinsitebuilderUwe Keim13-Jun-09 9:54 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 16 Jun 2009
Article Copyright 2009 by Uwe Keim
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid