TIPS-GridView DataFormString Not Applied
ASP.NET 2.0 GridView裡加了一個BoundField,資料型別是日期,我打算把DataFormatString設成{0:yyyy-MM-dd HH:mm:ss}讓它顯示得更整齊一點,但網頁上ASP.NET並沒有套用我指定的格式。如果我火大連{0:...}都不加,而把它調成"SHIT"之類的發洩性字眼,GridView倒是乖乖地拉出一大堆狗屎... orz
試了好久,最後查到網路上的相關文章,說將HtmlEncode屬性設為false即可解決DataFormatString設定無效的問題,並視之為BUG。但我最後追到MS關於BoundField.DataFormatString的官方Reference,裡面有一段Note:
When the HtmlEncode property is true, the value of the field is HTML encoded to its string representation before the formatting string is applied. For some objects, such as dates, you might want to control how the object is displayed with a formatting string. In those cases, you must set the HtmlEncode property to false.
套句MS愛用的說法,It's ** BY DESIGN **, not a bug. 沒耐心好好先看完Reference的人就要中招囉!
PS: 雖然官方參考書中就有說明,我覺得應該在Property Window下方顯示DataFormatString Description時就加註這段警語,以造福我等個性急躁、不耐查書的火爆浪子。算了算,繞這一圈耗了近半小時,我寶貴的青春哪~~~