TextBox.ReadOnly 属性
定义
public:
virtual property bool ReadOnly { bool get(); void set(bool value); };
[System.ComponentModel.Bindable(true)]
public virtual bool ReadOnly { get; set; }
[System.ComponentModel.Bindable(true)]
[System.Web.UI.Themeable(false)]
public virtual bool ReadOnly { get; set; }
[<System.ComponentModel.Bindable(true)>]
member this.ReadOnly : bool with get, set
[<System.ComponentModel.Bindable(true)>]
[<System.Web.UI.Themeable(false)>]
member this.ReadOnly : bool with get, set
Public Overridable Property ReadOnly As Boolean
属性值
如果不能更改 TextBox 控件的内容,则为 true;否则为 false。true if the contents of the TextBox control cannot be changed; otherwise, false. 默认值是 false。The default value is false.
- 属性
示例
下面的代码示例演示如何使用 ReadOnly 属性来防止更改控件中显示的文本 TextBox 。The following code example demonstrates how to use the ReadOnly property to prevent any changes to the text displayed in the TextBox control.
重要
此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。This example has a text box that accepts user input, which is a potential security threat. 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 有关详细信息,请参阅脚本侵入概述。For more information, see Script Exploits Overview.
<%@ Page Language="C#" AutoEventWireup="True" %>
<!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>
<title> MultiLine TextBox Example </title>
<script runat="server">
protected void SubmitButton_Click(Object sender, EventArgs e)
{
Message.Text = "Thank you for your comment: <br />" + Comment.Text;
}
protected void Check_Change(Object sender, EventArgs e)
{
Comment.Wrap = WrapCheckBox.Checked;
Comment.ReadOnly = ReadOnlyCheckBox.Checked;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3> MultiLine TextBox Example </h3>
Please enter a comment and click the submit button.
<br /><br />
<asp:TextBox ID="Comment"
TextMode="MultiLine"
Columns="50"
Rows="5"
runat="server"/>
<br />
<asp:RequiredFieldValidator
ID="Value1RequiredValidator"
ControlToValidate="Comment"
ErrorMessage="Please enter a comment.<br />"
Display="Dynamic"
runat="server"/>
<asp:CheckBox ID="WrapCheckBox"
Text="Wrap Text"
Checked="True"
AutoPostBack="True"
OnCheckedChanged="Check_Change"
runat="server"/>
<asp:CheckBox ID="ReadOnlyCheckBox"
Text="ReadOnly"
Checked="False"
AutoPostBack="True"
OnCheckedChanged="Check_Change"
runat="server"/>
<asp:Button ID="SubmitButton"
Text="Submit"
OnClick="SubmitButton_Click"
runat="server"/>
<hr />
<asp:Label ID="Message"
runat="server"/>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!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>
<title> MultiLine TextBox Example </title>
<script runat="server">
Protected Sub SubmitButton_Click(sender As Object, e As EventArgs )
Message.Text = "Thank you for your comment: <br />" + Comment.Text
End Sub
Protected Sub Check_Change(sender As Object, e As EventArgs )
Comment.Wrap = WrapCheckBox.Checked
Comment.ReadOnly = ReadOnlyCheckBox.Checked
End Sub
</script>
</head>
<body>
<form id="form1" runat="server">
<h3> MultiLine TextBox Example </h3>
Please enter a comment and click the submit button.
<br /><br />
<asp:TextBox ID="Comment"
TextMode="MultiLine"
Columns="50"
Rows="5"
runat="server"/>
<br />
<asp:RequiredFieldValidator
ID="Value1RequiredValidator"
ControlToValidate="Comment"
ErrorMessage="Please enter a comment.<br />"
Display="Dynamic"
runat="server"/>
<asp:CheckBox ID="WrapCheckBox"
Text="Wrap Text"
Checked="True"
AutoPostBack="True"
OnCheckedChanged="Check_Change"
runat="server"/>
<asp:CheckBox ID="ReadOnlyCheckBox"
Text="ReadOnly"
Checked="False"
AutoPostBack="True"
OnCheckedChanged="Check_Change"
runat="server"/>
<asp:Button ID="SubmitButton"
Text="Submit"
OnClick="SubmitButton_Click"
runat="server"/>
<hr />
<asp:Label ID="Message"
runat="server"/>
</form>
</body>
</html>
注解
使用 ReadOnly 属性指定是否 TextBox 可以更改控件的内容。Use the ReadOnly property to specify whether the contents of the TextBox control can be changed. 如果将此属性设置为, true 则将阻止用户输入值或更改现有值。Setting this property to true will prevent users from entering a value or changing the existing value. 请注意,控件的用户 TextBox 无法更改此属性; 只有开发人员才能这样做。Note that the user of the TextBox control cannot change this property; only the developer can.
重要
Text TextBox ReadOnly 当发生回发时,将属性设置为的控件的值 true 将发送到服务器,但服务器不对只读文本框进行处理。The Text value of a TextBox control with the ReadOnly property set to true is sent to the server when a postback occurs, but the server does no processing for a read-only text box. 这可以防止恶意用户更改 Text 只读值。This prevents a malicious user from changing a Text value that is read-only. Text除非由服务器端代码修改,否则属性的值将保留在回发之间的视图状态中。The value of the Text property is preserved in the view state between postbacks unless modified by server-side code.
无法通过主题或样式表主题设置此属性。This property cannot be set by themes or style sheet themes. 有关详细信息,请参阅 ThemeableAttribute 和 ASP.NET 主题和外观。For more information, see ThemeableAttribute and ASP.NET Themes and Skins.