Click here to Skip to main content
6,595,444 members and growing! (17,939 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » Grid Controls     Beginner License: The Code Project Open License (CPOL)

About GridView, HyperLinkField, and UrlEncode

By percyboy

Tired of converting HyperLinkFields into TemplateFields in order to solve the UrlEncode issue? This solution is right for you.
C# (C# 2.0, C# 3.0), .NET (.NET 2.0, .NET 3.0, .NET 3.5), ASP.NET, Dev
Version:2 (See All)
Posted:22 Apr 2008
Views:19,765
Bookmarked:23 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
8 votes for this article.
Popularity: 3.55 Rating: 3.93 out of 5

1

2
3 votes, 37.5%
3
2 votes, 25.0%
4
3 votes, 37.5%
5

Introduction

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.

Background

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)
    {
        return;
    }
    gridView.RowDataBound += delegate(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.DataRow)
        {
            return;
        }
        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,
                                hyperLinkField.DataNavigateUrlFields[j]);
                            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.

License

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

About the Author

percyboy


Member
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:
http://blog.joycode.com/percyboy/
Thanks!
Occupation: Software Developer
Location: China China

Other popular ASP.NET Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
QuestionWhere do you put the call to your routine? PinmemberRod Falanga11:50 19 May '09  
GeneralIt is much more easy to override PerformDataBinding of GridView PinmemberTomas Kubes7:15 19 Dec '08  
GeneralVery nice PinmemberFilipB4:18 23 May '08  
GeneralVery Clever PinmemberMichael Chick11:18 29 Apr '08  
GeneralAdd some source code Pinmember leppie 1:17 22 Apr '08  
GeneralRe: Add some source code Pinmemberpercyboy2:29 22 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 Apr 2008
Editor: Smitha Vijayan
Copyright 2008 by percyboy
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project