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

推荐订阅源

云风的 BLOG
云风的 BLOG
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
IT之家
IT之家
Recent Announcements
Recent Announcements
B
Blog
D
Docker
V
V2EX
GbyAI
GbyAI
L
LangChain Blog
博客园 - Franky
U
Unit 42
T
The Blog of Author Tim Ferriss
A
About on SuperTechFans
博客园 - 【当耐特】
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
博客园_首页
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
量子位
Blog — PlanetScale
Blog — PlanetScale
罗磊的独立博客

博客园 - Super Sky

2016年了 14年了,离上一篇2年了,做个记号 忙完这段时间后要做的事情 android比赛 计算机前沿技术造就亿万富翁 javamail 转 异常 关于异常 xml生成和解析 转载 ibatis 转ibatis多表操作 转载 联合查询 in ibatis 我想出国读硕士了 迅雷在哪里,迅雷在这里 二维游戏开发的点滴 盟军敢死队 用c语言开发游戏 快乐的痛 笑着哭 所谓机遇 顽固文件删除
模板里面的时间(网上找的)
Super Sky · 2012-02-24 · via 博客园 - Super Sky

(AKA, the DataFormatString="{0:M-dd-yyyy}" Problem)

A very common desire is to set a column of a GridView to display just the month, day and year of a DateTime type. The problem is the by default, the HtmlEncode property of the BoundField attribute ( <asp:BoundField …) is set to True. The reason for this (as pointed out in the documentation of this attribute) is that this helps prevent cross-site scripting attacks and malicious content from being displayed. Microsoft recommends that the HtmlEncode attribute be enabled whenever possible.

The problem is that if this field is enabled, you can not pass format information to the boundfield control. That is, if you try the following code, you will not get the desired result.

<columns>
<asp headertext="CreationDate" dataformatstring="{0:M-dd-yyyy}"
datafield="CreationDate" :BoundField />
</columns>
</asp>

You have two choices to make this work as you would expect. The first choice is to simply set HtmlEncode to false as follows:

<asp id="GridView1" runat="server" :GridView>
<columns>
<asp headertext="CreationDate" dataformatstring="{0:M-dd-yyyy}"
datafield="CreationDate" :BoundField HtmlEncode="false" />
</columns>

The second choice is to make the column a template and simply set the format string directly in the Label or Text Fields as follows.

<asp id="GridView3" runat="server" :GridView>
<columns>
<asp headertext="CreationDate" :TemplateField>
<edititemtemplate>
<asp id="Label1" runat="server" Label.Text='<%# Eval("CreationDate", "{0:M-dd-yyyy}") %>'>
</asp>
</edititemtemplate>
<itemtemplate>
<asp id="Label1" runat="server" Label.Text='<%# Bind("CreationDate", "{0:M-dd-yyyy}") %>'>;
</asp>
</itemtemplate>
</asp>
</columns>
</asp>

Below is a screen shot of what the three scenarios discussed above look like.

Output of all three scenarios

Good luck with your coding and hopefully, this will be one nasty you can put behind you.