BoundField.ReadOnly Property

Definition

Gets or sets a value indicating whether the value of the field can be modified in edit mode.

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

Property Value

true to prevent the value of the field from being modified in edit mode; otherwise, false. The default is false.

Examples

The following code example demonstrates how to use the ReadOnly property to prevent the values of the key field displayed in a BoundField object from being modified in edit mode.


<%@ 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 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"
            readonly="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 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" %>

<!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 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"
            readonly="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 CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country Where (CustomerID = @CustomerID)"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

Remarks

Use the ReadOnly property to prevent the values displayed in a BoundField object from being modified. When a data-bound control is in edit mode, fields that are read-only are displayed as plain text, without any editing controls.

Note

The ReadOnly property applies only to edit mode. To prevent fields from showing up in insert mode, use the InsertVisible property.

Applies to

See also