ListView.EmptyDataTemplate 屬性

定義

取得或設定空白樣板的使用者定義內容,當 ListView 控制項繫結至不包含任何資料錄的資料來源時會呈現該空白樣板。

public:
 virtual property System::Web::UI::ITemplate ^ EmptyDataTemplate { System::Web::UI::ITemplate ^ get(); void set(System::Web::UI::ITemplate ^ value); };
[System.ComponentModel.Browsable(false)]
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
[System.Web.UI.TemplateContainer(typeof(System.Web.UI.WebControls.ListView))]
public virtual System.Web.UI.ITemplate EmptyDataTemplate { get; set; }
[<System.ComponentModel.Browsable(false)>]
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
[<System.Web.UI.TemplateContainer(typeof(System.Web.UI.WebControls.ListView))>]
member this.EmptyDataTemplate : System.Web.UI.ITemplate with get, set
Public Overridable Property EmptyDataTemplate As ITemplate

屬性值

物件,包含空白樣板的自訂內容。 預設值為 null,表示這個屬性尚未設定。

屬性

範例

下列範例示範如何定義空白範本的自訂範本。

<%@ Page language="C#" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView EmptyDataTemplate Example</title>
    <style type="text/css">
        .emptyTable { background-color:Aqua; }
    </style>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListView EmptyDataTemplate Example</h3>
                 
      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        runat="server">
        <LayoutTemplate>
          <table runat="server" id="tblProducts">
            <tr runat="server" id="itemPlaceholder" />
          </table>
         </LayoutTemplate>
         <ItemTemplate>
            <tr runat="server">
              <td>
                <asp:Label ID="FirstNameLabel" runat="Server" Text='<%#Eval("FirstName") %>' />
              </td>
              <td>
                <asp:Label ID="LastNameLabel" runat="Server" Text='<%#Eval("LastName") %>' />
              </td>
            </tr>
          </ItemTemplate>
          <EmptyDataTemplate>
              <table class="emptyTable" cellpadding="5" cellspacing="5">
                <tr>
                  <td>
                    <asp:Image ID="NoDataImage"
                      ImageUrl="~/Images/NoDataImage.jpg" 
                      runat="server"/>
                  </td>
                  <td>
                    No records available.
                  </td>
                </tr>
              </table>
          </EmptyDataTemplate>
      </asp:ListView>

      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->
      
      <!-- The select query for the following SqlDataSource     -->
      <!-- control is intentionally set to return no results    -->
      <!-- to demonstrate the empty data template               -->       
      <asp:SqlDataSource ID="ContactsDataSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
            SelectCommand="SELECT [ContactID], [FirstName], [LastName] 
              FROM Person.Contact WHERE [ContactID]=1000">
      </asp:SqlDataSource>
      
    </form>
  </body>
</html>
<%@ Page language="VB" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView EmptyDataTemplate Example</title>
    <style type="text/css">
        .emptyTable { background-color:Aqua; }
    </style>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListView EmptyDataTemplate Example</h3>
                 
      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        runat="server">
        <LayoutTemplate>
          <table runat="server" id="tblProducts">
            <tr runat="server" id="itemPlaceholder" />
          </table>
         </LayoutTemplate>
         <ItemTemplate>
            <tr runat="server">
              <td>
                <asp:Label ID="FirstNameLabel" runat="Server" Text='<%#Eval("FirstName") %>' />
              </td>
              <td>
                <asp:Label ID="LastNameLabel" runat="Server" Text='<%#Eval("LastName") %>' />
              </td>
            </tr>
          </ItemTemplate>
          <EmptyDataTemplate>
              <table class="emptyTable" cellpadding="5" cellspacing="5">
                <tr>
                  <td>
                    <asp:Image ID="NoDataImage"
                      ImageUrl="~/Images/NoDataImage.jpg" 
                      runat="server"/>
                  </td>
                  <td>
                    No records available.
                  </td>
                </tr>
              </table>
          </EmptyDataTemplate>
      </asp:ListView>

      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->
      
      <!-- The select query for the following SqlDataSource     -->
      <!-- control is intentionally set to return no results    -->
      <!-- to demonstrate the empty data template               -->       
      <asp:SqlDataSource ID="ContactsDataSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
            SelectCommand="SELECT [ContactID], [FirstName], [LastName] 
              FROM Person.Contact WHERE [ContactID]=1000">
      </asp:SqlDataSource>
      
    </form>
  </body>
</html>

備註

當系結至控制項的資料來源不包含任何記錄,且 InsertItemPosition 屬性設定 InsertItemPosition.None 為 時,空白範本會顯示在 ListView 控制項中。 範本會轉譯,而不是 LayoutTemplate 範本。 InsertItemPosition如果 屬性設定為 以外的 InsertItemPosition.None 值,則 EmptyDataTemplate 不會轉譯範本。

您可以使用 屬性,為空範本 EmptyDataTemplate 定義自訂使用者介面 (UI) 。 若要以宣告方式指定空白範本的自訂範本,請在 控制項內 ListView 新增 和 EmptyDataTemplate 元素。 然後,您可以將範本的內容新增至 EmptyDataTemplate 元素。

適用於

另請參閱