5,133,229 members and growing! (13,852 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » Grid Controls     Beginner License: The Code Project Open License (CPOL)

About GridView, HyperLinkField, UrlEncode

By percyboy

Tired of converting HyperLinkField into TemplateField in order to solve UrlEncode? It is right for you.
C# (C# 2.0, C# 3.0, C#), .NET (.NET, .NET 2.0, .NET 3.5, .NET 3.0), ASP.NET, Dev

Posted: 22 Apr 2008
Updated: 22 Apr 2008
Views: 2,772
Announcements



Search    
Advanced Search
Sitemap
4 votes for this Article.
Popularity: 2.41 Rating: 4.00 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
2 votes, 50.0%
3
0 votes, 0.0%
4
2 votes, 50.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Unlike BoundField, HyperLinkField doesn't have a property named "HtmlEncode" or "UrlEncode", etc. But, it's quite normal when you have to do a "UrlEncode" 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, it will be more common.

Background

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

Using the code

Following code gives you another choice, avoiding convertion into TemplateField and hacking for UrlEncode feature for 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 UrlEncode hacking, as the parameter. It'll be OK! Quite simply, isn't it?

Points of Interest

Just like the guys left comments after the post, I also opposite Microsoft's strict backward compatibility policy. While different versions of .NET applications can run side-by-side, why give so much attension on it? It's just my opion.

License

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

About the Author

percyboy


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
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralVery ClevermemberMichael Chick11:18 29 Apr '08  
GeneralAdd some source codemember leppie 1:17 22 Apr '08  
GeneralRe: Add some source codememberpercyboy2: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:
Copyright 2008 by percyboy
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project