13 Mar 2006

HTMLEncode and GridView

One of my friends asked me how come this blog doesn't contain anything geekish since I am so geek. Well here goes :D :D :D :D

One of my biggest complaint about ASP.NET 2.0 is GridView which is a great replacement for Datagrid. As usual, Microsoft created a wonderful web control that is simple to use and yet sophisticated. However, it is like all things Microsoft: buggy and do-it-my-wayish.

If you had used the Datagrid from ASP.NET 1.0/1.1 you would know that you can format numeric and date data in the cells using the DataFormatString attribute. For GridView, there is also a DataFormatString attribute. However, there is also this new feature called HTMLEncode which is turned on by default. Basically, it HTMLEncodes the data in the cells to make sure they turn out the way they are supposed to. Sounds like a great feature. But here is the problem. If you use the DataFormatString, you need to set the HTMLEncode to false, otherwise the DataFormatString will not get applied. This is supposed to prevent CSS! (huh?) And it is not mentioned anywhere in the documentation. I spent hours trying to figure out what I did wrong. Only a search on the trusty Google did I find the solution in a blog entry.

One of the guys said in the comments of the blog entry:
I have the same problem. Happy to see I am not just crazy...
That is not the worst. It seems the problem has been around since beta! The least the MSDN people could do is to paste this odd behaviour all over the documentation if they can't fix it!

The other thing I hate about the GridView is that you cannot use it to update a DataSet row using code. It is designed to work hand-in-hand with a DataSource. So you are supposed to use the DataSource to update the row. There is a RowUpdating event (before the update) and a RowUpdated event (after update) but no RowUpdate event. You cannot update the row to a DataSet by code. You need to use a DataSource, in my case, a ObjectDataSource. It is the usual "Do it my way! Or don't do it!" Sigh!

Hey! I might be wrong, if you know an elegant way of solving these problem, it would like to hear from you! :P

No comments: