惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News | PayPal Newsroom
P
Proofpoint News Feed
Cyberwarzone
Cyberwarzone
C
Cisco Blogs
SecWiki News
SecWiki News
Know Your Adversary
Know Your Adversary
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Vercel News
Vercel News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
NISL@THU
NISL@THU
WordPress大学
WordPress大学
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Threat Research - Cisco Blogs
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
Security Archives - TechRepublic
Security Archives - TechRepublic
有赞技术团队
有赞技术团队
L
LINUX DO - 热门话题
Hacker News: Ask HN
Hacker News: Ask HN
V
V2EX
G
GRAHAM CLULEY
TaoSecurity Blog
TaoSecurity Blog
Hugging Face - Blog
Hugging Face - Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
F
Fortinet All Blogs
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
Latest news
Latest news
The Hacker News
The Hacker News
aimingoo的专栏
aimingoo的专栏
T
Troy Hunt's Blog
S
Schneier on Security
I
Intezer
Google DeepMind News
Google DeepMind News
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threatpost
爱范儿
爱范儿
The Register - Security
The Register - Security
S
SegmentFault 最新的问题
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
宝玉的分享
宝玉的分享
Recent Commits to openclaw:main
Recent Commits to openclaw:main
美团技术团队
B
Blog RSS Feed

博客园 - 破宝

imdict-chinese-analyzer .NET转写版 SQLite全文检索(2) SQLite全文检索(1) 80块钱毁掉“猪八戒”的信誉 有点郁闷:MSDN文档中MidpointRounding.AwayFromZero的翻译错误 当 ASP.net Mobile Controls 碰到“中国特色”的 CMWAP / UNIWAP 闲话“正版”:正版软件和盗版软件的区别到底是什么? 闲话“正版”:真是因为“缺钱”吗? 又一个疑似Bug: XmlDataSource 控件的 Data 属性动态改变时,缓存不会自动失效 立此存照:System.Net.Mail 的 bug 关于 GridView,HyperLinkField,UrlEncode WPF教程(译文)(第二部分) 破宝(percyboy)的自选精华版 破宝(percyboy)的留言板(2008/3~) WPF 教程(译文)(第一部分) Atlas 学习记录 ajaxnet4j beta Releases -- A Java Implementation of Ajax.NET Professional Library 好消息:Ajax.NET Professional open-sourced 及 ajaxnet4j 即将发布 NDoc Reloading: Kevin 留给我们的 NDoc 2.0 Alpha
About GridView, HyperLinkField, UrlEncode
破宝 · 2008-04-18 · via 博客园 - 破宝

I suppose you were searching the keywords in the title before entering this page. The problem may be:

In a GridView (ASP.NET 2.0), you want to use a HyperLinkField, but you find it doesn't support UrlEncode, while you are planning to pass some variables via URLs. The bug report shows that Microsoft doesn't have any plan on adding such a property, because their policy on backward compatibility between different version of .net frameworks.

I also made a lot of searching and browsing. The popular way to solve this problem always is, to tranform your HyperLinkField into TemplateField, and do UrlEncode by yourself via HttpUtility.UrlEncode method.

Following codes give you another choice, avoiding such a tranformation, and working for your UrlEncode needs. Just have a try!

    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)
                {
                    log.Debug(e.Row.RowType.ToString());
                    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);
                        }
                    }
                }
            }
        };
    }

Pleased if you find it useful.