BoundField.ReadOnly 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置一个值,该值指示是否可以在编辑模式中修改字段的值。
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
属性值
若要防止字段的值在编辑模式中被修改,则为 true
;否则为 false
。 默认值为 false
。
示例
下面的代码示例演示如何使用 ReadOnly 该属性来防止在编辑模式下修改对象中显示的 BoundField 键字段的值。
<%@ 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>
注解
ReadOnly使用该属性防止对象中显示的BoundField值被修改。 当数据绑定控件处于编辑模式时,只读字段显示为纯文本,而不显示任何编辑控件。
备注
该 ReadOnly 属性仅适用于编辑模式。 若要防止字段以插入模式显示,请使用 InsertVisible 该属性。