Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EmptyDataTemplate and EmptyDataText not working in GridView

I can't seem to get either EmptyDataTemplate or EmptyDataText of a GridView to work.

I'm fetching the GridView contents in de codebehind and attaching them with using DataBind(). I've tried having them as null and as an empty List, and in both cases the text I put into EmptyDataTemplate or EmptyDataText is not displayed.

What am I doing wrong?

EDIT (Code snippet)

This is my GridView:

<asp:GridView ID="grid" runat="server" EmptyDataText="EMPTY">
</asp:GridView>

And I've tried these two for binding the data:

grid.DataSource = new List<object>();
grid.DataBind();

grid.DataSource = null;
grid.DataBind();
like image 444
Farinha Avatar asked Jun 03 '10 13:06

Farinha


2 Answers

This problem is caused by using the so-called CSS-Friendly Control Adapters. With them enabled (and they seem to be enabled by default), EmptyDataTemplate and EmptyDataText don't work as expected.

To disable the adapters, go to the App_Browsers folder, and in the CSSFriendlyAdapters.browser file, comment out the following section (or the section related to the control you're using):

<adapter controlType="System.Web.UI.WebControls.GridView"
               adapterType="CSSFriendly.GridViewAdapter" />

The big problem is the styles will go away.

like image 169
Farinha Avatar answered Sep 24 '22 16:09

Farinha


I ran into a similar problem and noticed I had logic around my DataBind to ignore the databinding method if my datasource was empty.

like image 36
TreK Avatar answered Sep 23 '22 16:09

TreK