共用方式為


BoundField.NullDisplayText 屬性

定義

取得或設定欄位值為 null 時所顯示的欄位標題。

public:
 virtual property System::String ^ NullDisplayText { System::String ^ get(); void set(System::String ^ value); };
public virtual string NullDisplayText { get; set; }
member this.NullDisplayText : string with get, set
Public Overridable Property NullDisplayText As String

屬性值

String

當欄位值為 null 時所顯示的欄位標題。 預設為空字串 (""),表示這個屬性未設定。

範例

下列程式碼範例示範如何使用 NullDisplayText 屬性來指定自訂標題來顯示 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 runat="server">
    <title>BoundField NullDisplayText Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>BoundField NullDisplayText Example</h3>

      <asp:gridview id="DiscountsGridView" 
        datasourceid="DiscountsSqlDataSource" 
        autogeneratecolumns="false"
        runat="server">
                
        <columns>
                
          <asp:boundfield datafield="discounttype"
            nulldisplaytext="No Data"
            headertext="Discount Type"/>
                  
          <asp:boundfield datafield="stor_id"
            nulldisplaytext="No Data"
            headertext="Store ID"/> 
                    
          <asp:boundfield datafield="lowqty"
            nulldisplaytext="No Data"
            headertext="Low Quantity"/>
                    
          <asp:boundfield datafield="highqty"
            nulldisplaytext="No Data"
            headertext="High Quantity"/>
                    
          <asp:boundfield datafield="discount"
            nulldisplaytext="No Data"
            dataformatstring="{0:F4}%" 
            itemstyle-horizontalalign="Right" 
            headertext="Discount"/>     
                
        </columns>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="DiscountsSqlDataSource"  
        selectcommand="SELECT [discounttype], [stor_id], [lowqty], [highqty], [discount] FROM [discounts]"
        connectionstring="<%$ ConnectionStrings:PubsConnectionString%>"
        runat="server">
      </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 runat="server">
    <title>BoundField NullDisplayText Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>BoundField NullDisplayText Example</h3>

      <asp:gridview id="DiscountsGridView" 
        datasourceid="DiscountsSqlDataSource" 
        autogeneratecolumns="false"
        runat="server">
                
        <columns>
                
          <asp:boundfield datafield="discounttype"
            nulldisplaytext="No Data"
            headertext="Discount Type"/>
                  
          <asp:boundfield datafield="stor_id"
            nulldisplaytext="No Data"
            headertext="Store ID"/> 
                    
          <asp:boundfield datafield="lowqty"
            nulldisplaytext="No Data"
            headertext="Low Quantity"/>
                    
          <asp:boundfield datafield="highqty"
            nulldisplaytext="No Data"
            headertext="High Quantity"/>
                    
          <asp:boundfield datafield="discount"
            nulldisplaytext="No Data"
            dataformatstring="{0:F4}%" 
            itemstyle-horizontalalign="Right" 
            headertext="Discount"/>     
                
        </columns>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                        -->
      <asp:sqldatasource id="DiscountsSqlDataSource"  
        selectcommand="SELECT [discounttype], [stor_id], [lowqty], [highqty], [discount] FROM [discounts]"
        connectionstring="<%$ ConnectionStrings:PubsConnectionString%>"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

備註

有時候,欄位的值會儲存為數據源中的 Null。 您可以藉由設定 NullDisplayText 屬性,指定自訂標題來顯示具有 Null 值的欄位。 如果未設定此屬性,null 域值會顯示為空字串, (「」「) 。 在資料繫結控制項中更新或插入記錄時,如果使用者輸入此屬性所指定的值, (資料繫結控制項中欄位的空白字串) ,則該值會自動轉換成資料來源中的 Null。

注意

若要將空字串域值轉換成 Null 值,請將 ConvertEmptyStringToNull 屬性設定為 true

適用於

另請參閱