EmptyDataTemplate 和 EmptyDataText 在 GridView 中不起作用

发布于 2024-09-03 21:12:51 字数 623 浏览 9 评论 0原文

我似乎无法让 GridViewEmptyDataTemplateEmptyDataText 工作。

我正在代码隐藏中获取 GridView 内容并使用 DataBind() 附加它们。我尝试将它们设置为 null 和空的 List,在这两种情况下,我都将文本放入 EmptyDataTemplateEmptyDataText不显示

我做错了什么?

编辑(代码片段)

这是我的GridView:

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

我已经尝试过这两个来绑定数据:

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

grid.DataSource = null;
grid.DataBind();

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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

仙女山的月亮 2024-09-10 21:12:51

此问题是由使用所谓的 CSS 友好控制适配器 引起的。启用它们后(它们似乎默认启用),EmptyDataTemplateEmptyDataText 无法按预期工作。

要禁用适配器,请转到 App_Browsers 文件夹,然后在 CSSFriendlyAdapters.browser 文件中注释掉以下部分(或与您正在使用的控件相关的部分) ):

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

最大的问题是样式会消失。

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.

洛阳烟雨空心柳 2024-09-10 21:12:51

我遇到了类似的问题,并注意到如果我的数据源为空,我的 DataBind 周围有逻辑忽略数据绑定方法。

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

流殇 2024-09-10 21:12:51

EmptyDataTemplate 将覆盖任何 EmptyDataText,因此您可能以某种方式错误地定义了模板,这导致您看不到 EmptyDataText

如果您将空列表绑定到 GridView,它应该显示 EmptyDataText,只要您没有冲突的 EmptyDataTemplate,例如

<%-- GridTest.ascx --%>
<asp:GridView ID="Grid" runat="server" EmptyDataText="No Data!" />

// GridTest.ascx.cs
Grid.DataSource = new List<object>();
Grid.DataBind();

您是否在做不同的事情比这个?

The EmptyDataTemplate will override any EmptyDataText, so it could be that you've defined the template incorrectly somehow, which is keeping you from seeing the EmptyDataText.

If you're binding an empty list to the GridView, it should display the EmptyDataText, so long as you don't have a conflicting EmptyDataTemplate, e.g.

<%-- GridTest.ascx --%>
<asp:GridView ID="Grid" runat="server" EmptyDataText="No Data!" />

// GridTest.ascx.cs
Grid.DataSource = new List<object>();
Grid.DataBind();

Are you doing something different than this?

七堇年 2024-09-10 21:12:51

用户可以使用空数据模板吗......

    <Columns>
             ............
             ............
             ............
    <Columns>
<EmptyDataTemplate>
        <asp:Label ID="lblEmptyTxt" runat="server" Text="No Data"></asp:Label>
      </EmptyDataTemplate>
                        </asp:GridView>

Can User Empty Data Template.....

    <Columns>
             ............
             ............
             ............
    <Columns>
<EmptyDataTemplate>
        <asp:Label ID="lblEmptyTxt" runat="server" Text="No Data"></asp:Label>
      </EmptyDataTemplate>
                        </asp:GridView>
泛泛之交 2024-09-10 21:12:51

不要做我所做的事情 - GridView ForeColor 属性未设置,它默认为白色,这也是我的 BackColor 属性!

And don't do what I did - The GridView ForeColor property wasn't set and it defaults to white which was also my BackColor property!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文