Click here to Skip to main content
11,790,021 members (49,331 online)
Click here to Skip to main content

About GridView, HyperLinkField, and UrlEncode

, 21 Apr 2008 CPOL 44.5K 26
Rate this:
Please Sign up or sign in to vote.
Tired of converting HyperLinkFields into TemplateFields in order to solve the UrlEncode issue? This solution is right for you.


Unlike BoundField, HyperLinkField doesn't have methods like HtmlEncode or UrlEncode etc. But, it's quite normal when you have to do a "URL encode" processing for a hyperlink's parameter. Such as a person name may contain special characters like '&', etc. For eastern developers in China, Japan, or Korea, this will be more familiar.


Some of us hope Microsoft will add such a commonly-used feature in the next version of the .NET Framework. But it's still a problem in .NET 3.x till now. We learned from a feedback report from Microsoft that Microsoft will do no modification on this due to its policy on backwards compatibility between versions of the .NET Framework. Till now, the popular way to solve this problem is to convert the HyperLinkField into a TemplateField and to use the HttpUtility.UrlEncode method to solve it by hand. Because this conversion is not reversible, it may introduce some inconveniences when you have to do some minor modifications.

Using the Code

The following code gives you another choice, avoiding conversion into a TemplateField and hacking for the UrlEncode feature of the HyperLinkField:

public static void HyperLinkFieldUrlEncodeHack(GridView gridView)
    if (gridView == null)
    gridView.RowDataBound += delegate(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType != DataControlRowType.DataRow)
        for (int i = 0; i < gridView.Columns.Count; i++)
            DataControlField field = gridView.Columns[i];
            if (field is HyperLinkField)
                TableCell td = e.Row.Cells[i];
                if (td.Controls.Count > 0 && td.Controls[0] is HyperLink)
                    HyperLink hyperLink = (HyperLink)td.Controls[0];
                    HyperLinkField hyperLinkField = (HyperLinkField)field;
                    if (!String.IsNullOrEmpty(hyperLinkField.DataNavigateUrlFormatString))
                        string[] dataUrlFields = 
                          new string[hyperLinkField.DataNavigateUrlFields.Length];
                        for (int j = 0; j < dataUrlFields.Length; j++)
                            object obj = DataBinder.Eval(e.Row.DataItem,
                            dataUrlFields[j] = HttpUtility.UrlEncode(
                                (obj == null ? "" : obj.ToString()));
                        hyperLink.NavigateUrl = String.Format(
                            hyperLinkField.DataNavigateUrlFormatString, dataUrlFields);

You may simply call this method and pass the GridView, which includes the HyperLinkField which needs the URL Encode hacking, as the parameter. It'll be OK! Quite simple, isn't it?

Points of Interest

Just like the guys who left comments after the post, I also opposite Microsoft's strict backwards compatibility policy. While different versions of .NET applications can run side-by-side, why give so much attention to it? That is just my opinion.


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


About the Author

Software Developer
China China
I am a Chinese software developer, while I am now working in Japan.
If you can read in Chinese, you may visit my blog in Chinese:

You may also be interested in...

Comments and Discussions

QuestionPlease elaborate your code Pin
Irshad Mohammed kHan15-Jan-14 20:13
memberIrshad Mohammed kHan15-Jan-14 20:13 
QuestionWhere do you put the call to your routine? Pin
Rod Falanga19-May-09 10:50
memberRod Falanga19-May-09 10:50 
GeneralIt is much more easy to override PerformDataBinding of GridView Pin
Tomas Kubes19-Dec-08 6:15
memberTomas Kubes19-Dec-08 6:15 
GeneralVery nice Pin
FilipB23-May-08 3:18
memberFilipB23-May-08 3:18 
GeneralVery Clever Pin
Michael Chick29-Apr-08 10:18
memberMichael Chick29-Apr-08 10:18 
GeneralAdd some source code Pin
leppie 22-Apr-08 0:17
member leppie 22-Apr-08 0:17 
And the article will be approved.

xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 3 out now

GeneralRe: Add some source code Pin
percyboy22-Apr-08 1:29
memberpercyboy22-Apr-08 1:29 

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 | Terms of Use | Mobile
Web01 | 2.8.1509028.1 | Last Updated 22 Apr 2008
Article Copyright 2008 by percyboy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid