No NullDisplayText Property for TemplateField? No Sweat!

Microsoft gave us the BoundField.NullDisplayText property to display some text instead of a blank space in case a field is bound with data that is null. That’s handy, but we all know that a GridView left to its own devices and built with default fields will make crappy, invalid, unpredictable HTML. That’s why I always build a GridView with TemplateFields and connect data on my own without using built-in data sources. Since there is no similar property for a TemplateField (I guess I can understand why since a TemplateField can be anything and not just bound with data), we must handle null display text on our own.

And I do it by setting the text of the Literal in my TemplateField like this:

Text='<%# Convert.ToString(Eval("ColumnName")).Length < 1 ? "null" : Convert.ToString(Eval("ColumnName")) %>'

Leave a Reply

Your email address will not be published. Required fields are marked *