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

推荐订阅源

T
The Blog of Author Tim Ferriss
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
云风的 BLOG
云风的 BLOG
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Palo Alto Networks Blog
D
Docker
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
Schneier on Security
Engineering at Meta
Engineering at Meta
I
InfoQ
L
LangChain Blog
Cyberwarzone
Cyberwarzone
T
Tenable Blog
WordPress大学
WordPress大学
P
Privacy & Cybersecurity Law Blog
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Jina AI
Jina AI
C
CERT Recently Published Vulnerability Notes
Scott Helme
Scott Helme
博客园 - 三生石上(FineUI控件)
酷 壳 – CoolShell
酷 壳 – CoolShell
Know Your Adversary
Know Your Adversary
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Last Watchdog
The Last Watchdog
Last Week in AI
Last Week in AI
Cloudbric
Cloudbric
S
SegmentFault 最新的问题
爱范儿
爱范儿
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 叶小钗
AI
AI
T
Tor Project blog
I
Intezer
T
Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
N
News and Events Feed by Topic
Latest news
Latest news
S
Security Affairs
博客园 - Franky
Microsoft Security Blog
Microsoft Security Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog RSS Feed
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
小众软件
小众软件
S
Securelist

博客园 - 龚振

T4 模板编辑。 MVC 表单验证的几种方式: Login failed for user 'IIS APPPOOL\ASP.NET v4.0'. - 龚振 Keycode对照表 使用多字节字符集 syslink 不显示 Windows下JSP开发环境的配置 javascript 大括号,圆括号 清空数据库日志 数据库还原错误 (with move) 打开visual studio 智能感知(intellisense) for Unity Application Blocks (翻译) firefox ie DOM 级兼容性(一) ie ,firefox 兼容性,收集。。。。。 IE6.0、IE7.0 与 FireFox 收集,总是忘,还是收集了,以后总结下 web 版 ftp 数据库迁移可能遇到的问题 将物理内存制作为虚拟硬盘,提高系统速度 NET设计模式总结 自定义控件状态保存 关于Web服务器时间格式问题
关于Gridview templateField 无法 sort
龚振 · 2007-11-02 · via 博客园 - 龚振

在网上基本上没找到好的解决办法,最终还是觉得研究templateField 源码进行修改.
研究发现templateField 和bouldField 都是继承于DataControlField ,其实排序功能在DataControlField里面,所以排序功能是没问题,问题在于templateField 在重写InitializeCell时将cell里面的控件给替换了,可以参考下面代码

 public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {

            base.InitializeCell(cell, cellType, rowState, rowIndex);
            ITemplate headerTemplate = null;
            switch (cellType)
            {
                case DataControlCellType.Header:
                    headerTemplate = this.headerTemplate;
                    break;

                case DataControlCellType.Footer:
                    headerTemplate = this.footerTemplate;
                    break;

                case DataControlCellType.DataCell:
                    headerTemplate = this.itemTemplate;
                    if ((rowState & DataControlRowState.Edit) == DataControlRowState.Normal)
                    {
                        if ((rowState & DataControlRowState.Insert) != DataControlRowState.Normal)
                        {
                            if (this.insertItemTemplate != null)
                            {
                                headerTemplate = this.insertItemTemplate;
                            }
                            else if (this.editItemTemplate != null)
                            {
                                headerTemplate = this.editItemTemplate;
                            }
                        }
                        else if (((rowState & DataControlRowState.Alternate) != DataControlRowState.Normal) && (this.alternatingItemTemplate != null))
                        {
                            headerTemplate = this.alternatingItemTemplate;
                        }
                        break;
                    }
                    if (this.editItemTemplate != null)
                    {
                        headerTemplate = this.editItemTemplate;
                    }
                    break;
            }
            if (headerTemplate != null)
            {
                cell.Text = string.Empty;
                headerTemplate.InstantiateIn(cell);
            }
            else if (cellType == DataControlCellType.DataCell)
            {
                cell.Text = " ";
            }

}
于是决定修改此处代码,使templateField的排序功能表现出来.
修改后代码如下:

 public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {

            base.InitializeCell(cell, cellType, rowState, rowIndex);
            ITemplate headerTemplate = null;
            switch (cellType)
            {
                case DataControlCellType.Header:
                    headerTemplate = this.headerTemplate;
                    break;

                case DataControlCellType.Footer:
                    headerTemplate = this.footerTemplate;
                    break;

                case DataControlCellType.DataCell:
                    headerTemplate = this.itemTemplate;
                    if ((rowState & DataControlRowState.Edit) == DataControlRowState.Normal)
                    {
                        if ((rowState & DataControlRowState.Insert) != DataControlRowState.Normal)
                        {
                            if (this.insertItemTemplate != null)
                            {
                                headerTemplate = this.insertItemTemplate;
                            }
                            else if (this.editItemTemplate != null)
                            {
                                headerTemplate = this.editItemTemplate;
                            }
                        }
                        else if (((rowState & DataControlRowState.Alternate) != DataControlRowState.Normal) && (this.alternatingItemTemplate != null))
                        {
                            headerTemplate = this.alternatingItemTemplate;
                        }
                        break;
                    }
                    if (this.editItemTemplate != null)
                    {
                        headerTemplate = this.editItemTemplate;
                    }
                    break;
            }
            if (headerTemplate != null )
            {
                cell.Text = string.Empty;               
                headerTemplate.InstantiateIn(cell);
                if (cellType == DataControlCellType.Header)
                {
                    this.HeaderText = cell.Text;
                    base.InitializeCell(cell, cellType, rowState, rowIndex);
                }
            }
            else if (cellType == DataControlCellType.DataCell)
            {
                cell.Text = " ";
            }
        }


进测试可以正常运行.

这是针对那些需要在templateField里需要使用HeaderTemplate的,如果不使用就不必修改代码,当然也就要在 templateField里使用HeaderText属性赋值,很多人就是没有附值,而使sort功能失效的其原因是DataControlField里面(

if (headerText.Length == 0) {  headerText = " "; })给屏蔽了.