GridView資料來源為空白時顯示表頭方法

來源:互聯網
上載者:User

      通常我們用Gridview綁定資料時會先判斷資料來源是否為空白,如果為空白則不綁定,那麼此時頁面將不會顯示表格,如果希望在沒有資料的情況下依然顯示表頭,該如何?呢?

      EmptyDataTemplate——空資料範本是GridView控制項的屬性之一。EmptyDataTemplate表示當記錄為空白時顯示的定義內容。它用來擷取或設定在GridView 控制項綁定到不包含任何記錄的資料來源時,在GridView.控制項上所顯示的空資料行的內容,這個內容可以根據需要來設定。

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Width="200px"
            BorderWidth="0" BackColor="#DDBBDD">
            <EmptyDataTemplate>
                <table cellspacing="0" style="border-collapse: collapse; width: 100%; background-color: red;
                    margin: -1px">
                    <tr>
                        <th>
                            序號
                        </th>
                        <th>
                            姓名
                        </th>
                        <th>
                            年齡
                        </th>
                    </tr>
                </table>
            </EmptyDataTemplate>
            <Columns>
                <asp:BoundField HeaderText="序號" DataField="ID" />
                <asp:BoundField HeaderText="姓名" DataField="Name" />
                <asp:BoundField HeaderText="年齡" DataField="Age" />
            </Columns>
        </asp:GridView>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.