ASP.NET 2.0 的 TextBox 控件的 ReadOnly 属性
假设你希望在 web 页面上有一个不可以被用户编辑的文本框 (TextBox),但是你希望能够通过客户脚本更改文本框的值,并且更改后的结果能被服务器端获得。
如果你使用 TextBox1.ReadOnly = true,那么文本框的值是可以被客户脚本更改的,但是更改后的值却无法被服务器端获得。
可以试一下下面的代码:
可是如果你把 TextBox1.ReadOnly = true; 换成 TextBox1.Attributes["contentEditable"] = "false"; 你就可以在服务器端得到 TextBox1 的值。
看一下反射回来的 LoadPostData 的实现
可以看出,如果设置了ReadOnly为true,从客户端传回的新的值是不被设置到Text属性的。
[来源:AppDev-SYSK 118]
如果你使用 TextBox1.ReadOnly = true,那么文本框的值是可以被客户脚本更改的,但是更改后的值却无法被服务器端获得。
可以试一下下面的代码:
<%@ 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">
protected void Page_Load (object sender, EventArgs e)
{
TextBox1.ReadOnly = true;
//TextBox1.Attributes["contentEditable"] = "false";
}
protected void Button1_Click (object sender, EventArgs e)
{
Response.Write (TextBox1.Text + "<br>");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button2" type="button" value="Change Text via Client-Side Script"
onclick="ChangeText();" />
</div>
<asp:TextBox ID="TextBox1" runat="server">initial text</asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="See Value on the Server-Side" />
</form>
<script language="javascript" type="text/javascript">
<!--
function ChangeText() {
form1["TextBox1"].setAttribute("innerText", "abc");
}
-->
</script>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load (object sender, EventArgs e)
{
TextBox1.ReadOnly = true;
//TextBox1.Attributes["contentEditable"] = "false";
}
protected void Button1_Click (object sender, EventArgs e)
{
Response.Write (TextBox1.Text + "<br>");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button2" type="button" value="Change Text via Client-Side Script"
onclick="ChangeText();" />
</div>
<asp:TextBox ID="TextBox1" runat="server">initial text</asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="See Value on the Server-Side" />
</form>
<script language="javascript" type="text/javascript">
<!--
function ChangeText() {
form1["TextBox1"].setAttribute("innerText", "abc");
}
-->
</script>
</body>
</html>
可是如果你把 TextBox1.ReadOnly = true; 换成 TextBox1.Attributes["contentEditable"] = "false"; 你就可以在服务器端得到 TextBox1 的值。
看一下反射回来的 LoadPostData 的实现
protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
{
base.ValidateEvent (postDataKey);
string text1 = this.Text;
string text2 = postCollection[postDataKey];
if (!this.ReadOnly && !text1.Equals (text2, StringComparison.Ordinal))
{
this.Text = text2;
return true;
}
return false;
}
{
base.ValidateEvent (postDataKey);
string text1 = this.Text;
string text2 = postCollection[postDataKey];
if (!this.ReadOnly && !text1.Equals (text2, StringComparison.Ordinal))
{
this.Text = text2;
return true;
}
return false;
}
可以看出,如果设置了ReadOnly为true,从客户端传回的新的值是不被设置到Text属性的。
[来源:AppDev-SYSK 118]
posted on 2006-05-12 16:33 Easy Company 阅读(435) 评论(0) 收藏 举报
浙公网安备 33010602011771号