数据源为空GridView显示表头和提示

这篇博客介绍了三种在ASP.NET中使GridView在数据源为空时仍能显示表头和提示信息的方法。方法一利用EmptyTemplate添加静态Table;方法二针对DataTable和集合类数据源,返回空行或实体;方法三是通过继承GridView并重写Render方法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 方法一:采用GridView中的EmptyTemplate来实现,模版中写一个静态的table;

下面贴出aspx代码:

<head runat="server">
    <title>GridView使用EmptyTemplate来使无数据时显示表头</title>
</head>
<body>
    <form id="form1" runat="server">
    <strong>GridView使用EmptyTemplate来使无数据时显示表头</strong>
    <div>
        <asp:GridView ID="GridView1" runat="server" DataSourceID="AccessDataSource1" BackColor="White" BorderColor="#336666" Width="600px"
            BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal">
            <FooterStyle BackColor="White" ForeColor="#333333" />
            <RowStyle BackColor="White" ForeColor="#333333" />
            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
            <EmptyDataTemplate>
                <table cellpadding="0" cellspacing="0" width="600px">
                    <tr>
                       <td width="100px">
                          用户编号
                       </td>
                       <td width="100px">
                          用户姓名
                       </td>
                       <td width="100px">
                          用户类型
                       </td>
                       <td width="100px">
                          用户年龄
                       </td>
                       <td width="100px">
                          电子邮件
                       </td>
                       <td width="100px">
                          联系地址
                       </td>
                    </tr>
                    <tr>
 &n

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值