BoundField.ApplyFormatInEditMode 属性

定义

获取或设置一个值,该值指示包含 DataFormatString 对象的数据绑定控件处于编辑模式时,BoundField 属性指定的格式化字符串是否应用到字段值。

public:
 virtual property bool ApplyFormatInEditMode { bool get(); void set(bool value); };
public virtual bool ApplyFormatInEditMode { get; set; }
member this.ApplyFormatInEditMode : bool with get, set
Public Overridable Property ApplyFormatInEditMode As Boolean

属性值

若要在编辑模式中将格式化字符串应用到字段值,则为 true;否则为 false。 默认值为 false

示例

下面的代码示例演示如何在控件处于编辑模式时GridView使用 ApplyFormatInEditMode 属性将格式字符串应用于字段值。 然后,在数据源中更新值之前,以编程方式删除格式设置。


<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void CustomersGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
  {
    
    // Use the NewValues property to retrieve the updated CustomerID
    // value entered by the user.
    String customerID = e.NewValues["CustomerID"].ToString();
  
    // Remove the formating applied by the DataFormatString property.
    if(customerID.StartsWith("D:"))
    {
      e.NewValues["CustomerID"] = customerID.Substring(2);
    }

  }
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>BoundField Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>BoundField Example</h3>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="false"
        autogenerateeditbutton="true"
        allowpaging="true" 
        datakeynames="CustomerID"
        onrowupdating="CustomersGridView_RowUpdating"   
        runat="server">
         
        <columns>
          <asp:boundfield datafield="CustomerID"
            dataformatstring="D:{0}"
            applyformatineditmode="true"       
            headertext="Customer ID"/>
          <asp:boundfield datafield="CompanyName"
            convertemptystringtonull="true"
            headertext="Customer Name"/>
          <asp:boundfield datafield="Address"
            convertemptystringtonull="true"
            headertext="Address"/>
          <asp:boundfield datafield="City"
            convertemptystringtonull="true"
            headertext="City"/>
          <asp:boundfield datafield="PostalCode"
            convertemptystringtonull="true"
            headertext="ZIP Code"/>
          <asp:boundfield datafield="Country"
            convertemptystringtonull="true"
            headertext="Country"/>
        </columns>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers Set CustomerID=@CustomerID, CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country Where (CustomerID = @CustomerID)"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

<%@ Page language="VB" AutoEventWireup="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Sub CustomersGridView_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles CustomersGridView.RowUpdating
    
    ' Use the NewValues property to retrieve the updated CustomerID
    ' value entered by the user.
    Dim customerID As String = e.NewValues("CustomerID").ToString()
  
    ' Remove the formating applied by the DataFormatString property.
    If customerID.StartsWith("D:") Then
    
      e.NewValues("CustomerID") = customerID.Substring(2)
      
    End If

  End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>BoundField Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>BoundField Example</h3>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="false"
        autogenerateeditbutton="true"
        allowpaging="true" 
        datakeynames="CustomerID"  
        runat="server">
         
        <columns>
          <asp:boundfield datafield="CustomerID"
            dataformatstring="D:{0}"
            applyformatineditmode="true"       
            headertext="Customer ID"/>
          <asp:boundfield datafield="CompanyName"
            convertemptystringtonull="true"
            headertext="Customer Name"/>
          <asp:boundfield datafield="Address"
            convertemptystringtonull="true"
            headertext="Address"/>
          <asp:boundfield datafield="City"
            convertemptystringtonull="true"
            headertext="City"/>
          <asp:boundfield datafield="PostalCode"
            convertemptystringtonull="true"
            headertext="ZIP Code"/>
          <asp:boundfield datafield="Country"
            convertemptystringtonull="true"
            headertext="Country"/>
        </columns>
                
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers Set CustomerID=@CustomerID, CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country Where (CustomerID = @CustomerID)"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

注解

(、 FormViewGridView) 等DetailsView数据绑定控件具有不同的显示模式,允许用户读取、编辑或插入记录。 默认情况下,仅当数据绑定控件处于只读模式时, DataFormatString 属性指定的格式字符串才会应用于字段值。 若要将格式设置字符串应用于数据绑定控件处于编辑模式时显示的值,请将 ApplyFormatInEditMode 属性设置为 true

注意

BoundField 数据源中更新字段时,对象不会自动删除格式。 如果不希望将格式设置为存储值的一部分,则需要以编程方式删除格式设置。

适用于

另请参阅