Click here to Skip to main content
Email Password   helpLost your password?

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:

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralAwesome
jasonp12
8:37 29 Dec '09  
This has proved be a very useful code for me. Thanks a lot Big Grin


GeneralRe: Awesome
Uwe Keim
9:20 29 Dec '09  
Thank you! Very glad you like it and that it helped you!

My personal 24/7 webcamZeta Test - Intuitive, competitive Test Management environment for Test Plans and Test Cases. Download now!Zeta Producer Desktop CMS - Intuitive, very easy to use. Download now!

GeneralNice idea, two small problems
Richard Deeming
4:26 16 Jun '09  
You're using the WriteEncodedText method to write the text before and after the link, and a HyperLink control to render the text within the link. As a result, the text before and after the link will be HTML encoded, and the text within the link will not.

Eg:
Text="> Test <b>&</b> {0}test <b>&</b> test{1} again<b>!</b> <"
becomes:
&gt; Test &lt;b&gt;&amp;&lt;/b&gt; <a ...>test <b>&</b> test</a> again&lt;b&gt;!&lt;/b&gt; &lt;


Also, since you never add the HyperLink control to the control tree, the skin will never be applied, and neither will any control adapters configured for the HyperLink control.

To fix these issues, you should HTML encode the hyperlink text before assigning it, add the HyperLink control to the control tree, and call the ApplyStyleSheetSkin method:

if (!string.IsNullOrEmpty(hyperlinkText))
{
HyperLink hyperLink = new HyperLink();

hyperLink.Text = HttpUtility.HtmlEncode(hyperlinkText);
hyperLink.NavigateUrl = NavigateUrl;
hyperLink.Target = Target;
hyperLink.ToolTip = ToolTip;
hyperLink.CssClass = CssClass;
hyperLink.ImageUrl = ImageUrl;
hyperLink.SkinID = SkinID;

hyperLink.TemplateControl = this.TemplateControl;
hyperLink.ApplyStyleSheetSkin(Page);
Controls.Add(hyperLink);

hyperLink.RenderControl( writer );
}

Adding the control to the tree will apply the skin defined in a theme (Theme="..."), but will not apply the skin defined in a stylesheet theme (StyleSheetTheme="..."), which is why the call to ApplyStyleSheetSkin is necessary.


"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

GeneralRe: Nice idea, two small problems
Uwe Keim
4:35 16 Jun '09  
Thank you very much.

IIRC I created much of the code by looking at the Reflector output on the original ASP.NET HyperLink control.

I will add your fixes immediately and update this article.

Thanks again!
Uwe

My personal 24/7 webcamZeta Test - Intuitive, competitive Test Management environment for Test Plans and Test Cases. Download now!Zeta Producer Desktop CMS - Intuitive, very easy to use. Download now!

GeneralRe: Nice idea, two small problems
Uwe Keim
5:20 16 Jun '09  
I have just updated the article with your suggested corrections.

Thank you very much!

My personal 24/7 webcamZeta Test - Intuitive, competitive Test Management environment for Test Plans and Test Cases. Download now!Zeta Producer Desktop CMS - Intuitive, very easy to use. Download now!

GeneralGreat Idea
Peter Lange
10:52 13 Jun '09  
Hey, that is a very simple control, yet at the same time a Great Idea. Thanks for sharing it. Not only does it have application with the scenario you mentioned, it really has a lot of possibility outside of locallization.
GeneralRe: Great Idea
Uwe Keim
10:54 13 Jun '09  
Glad that you like it, Peter! Thanks for your feedback!

My personal 24/7 webcamZeta Test - Intuitive, competitive Test Management environment for Test Plans and Test Cases. Download now!Zeta Producer Desktop CMS - Intuitive, very easy to use. Download now!


Last Updated 16 Jun 2009 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010