Skip to main content

Posts

Showing posts from November, 2007

Displaying GridView When No Data Exists

One thing thats midly annoying to me about the new gridview control is that it doesn't display the header row when there is no data. I would like to just have the header displayed like the original table with a single row saying something like "No records found". The grid has a property for emptydata text which doesn't look that great in my opinion. I have seen some people use the <> property to create a new table in there which isn't the cleanest solution. One way to get the gridview to display how you want is to create a custom control that inherts from gidview. Then you can override the CreateChildControls method to create a the gridview with the all the headers and a blank row with one cell and a text message. Here's how to do it: using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Web.UI; using System.Web.UI.WebControls; public class EmptyGridView : GridView { #region Properties /// /// Enabl

Encoding data on GridView

We should encode data before displaying it on any control, Mainly to make your application safe against script injections. You can achieve this in many ways. Following are the few methods to do this: If you are using < asp:template > template then you can simply do this by HtmlEncode="true" attribute For <> , you can use < % "Container.DataItem, "FieldName" % > For other controls also you can use Server.HtmlEndcode() . Let me know if you want to know more about it. Naimish Dave