ASP.NET 2.0里textbox的Readonly属性为真时如何取值?
2006-09-26 10:08
429 查看
readonly为真就是只读,也就是你输入的值根本textbox没有接受,取出来的当然是空咯。
或者你用其他的方式赋值给textbox和一般取值一样。
[来源:AppDev-SYSK 118] 有时候,我们不希望用户直接编辑TextBox,而是希望通过客户端脚本的方式来设置内容,一般的做法是设置TextBox的属性ReadOnly为true。但在ASP.NET 2.0里有了变化,设置了ReadOnly为true的TextBox,在服务器端不能通过Text属性获取在客户端设置的新内容,在Reflector里比较一下LoadPostData的实现
.NET 1.1中,
bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
{
string text1 = this.Text;
string text2 = postCollection[postDataKey];
if (!text1.Equals(text2))
{
this.Text = text2;
return true;
}
return false;
}
.NET 2.0中,
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;
}
就可以看出,如果设置了ReadOnly为true,从客户端传回的新的值是不被设置到Text属性的。
想要保持.NET 1.*中的行为,建议的做法是设置客户端属性ContentEditable=false,参考
SYSK 118: ReadOnly or ContentEditable?
http://blogs.msdn.com/irenak/archive/2006/05/03/589085.aspx
其实如果是设置客户端属性的话,设置客户端的readonly属性应该也是可以的:
TextBox1.Attributes["readonly"] = "true";
SYSK 118: ReadOnly or ContentEditable?
Consider this: you want a text box on a web page to be not editable by the user, but you want to be able to change the text box’s contents in client side script and see the updated text on the server.
Did you know that if you set TextBox1.ReadOnly = true, the value set by the client side script will not be visible on the server? Try it for yourself… Here is the code:
<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>
public partial class MyForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.ReadOnly = true;
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(TextBox1.Text + "<br>");
}
}
However, if instead of setting TextBox1.ReadOnly property you set ContentEditable attribute to false, you’ll get the behavior you’re looking for:
TextBox1.Attributes["contentEditable"] = "false";
或者你用其他的方式赋值给textbox和一般取值一样。
[来源:AppDev-SYSK 118] 有时候,我们不希望用户直接编辑TextBox,而是希望通过客户端脚本的方式来设置内容,一般的做法是设置TextBox的属性ReadOnly为true。但在ASP.NET 2.0里有了变化,设置了ReadOnly为true的TextBox,在服务器端不能通过Text属性获取在客户端设置的新内容,在Reflector里比较一下LoadPostData的实现
.NET 1.1中,
bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
{
string text1 = this.Text;
string text2 = postCollection[postDataKey];
if (!text1.Equals(text2))
{
this.Text = text2;
return true;
}
return false;
}
.NET 2.0中,
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;
}
就可以看出,如果设置了ReadOnly为true,从客户端传回的新的值是不被设置到Text属性的。
想要保持.NET 1.*中的行为,建议的做法是设置客户端属性ContentEditable=false,参考
SYSK 118: ReadOnly or ContentEditable?
http://blogs.msdn.com/irenak/archive/2006/05/03/589085.aspx
其实如果是设置客户端属性的话,设置客户端的readonly属性应该也是可以的:
TextBox1.Attributes["readonly"] = "true";
SYSK 118: ReadOnly or ContentEditable?
Consider this: you want a text box on a web page to be not editable by the user, but you want to be able to change the text box’s contents in client side script and see the updated text on the server.
Did you know that if you set TextBox1.ReadOnly = true, the value set by the client side script will not be visible on the server? Try it for yourself… Here is the code:
<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>
public partial class MyForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.ReadOnly = true;
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(TextBox1.Text + "<br>");
}
}
However, if instead of setting TextBox1.ReadOnly property you set ContentEditable attribute to false, you’ll get the behavior you’re looking for:
TextBox1.Attributes["contentEditable"] = "false";
相关文章推荐
- ASP.NET 2.0中TextBox服务器控件的ReadOnly和Disabled属性
- ASP.NET 2.0中TextBox服务器控件的ReadOnly和Disabled属性
- ASP.NET 2.0中TextBox服务器控件的ReadOnly和Disabled属性
- .NET 2.0 中textbox的Readonly属性为真时如何取值
- ASP.NET 2.0中TextBox服务器控件的ReadOnly和Disabled属性
- ASP.NET 2.0中TextBox服务器控件的ReadOnly和Disabled属性
- ASP.NET 2.0中TextBox服务器控件的ReadOnly和Disabled属性
- ASP.NET(C#)中将TextBox控件的Readonly属性设置为True时后台直接取值取不到的解决方案
- ASP.NET 2.0中TextBox服务器控件的ReadOnly和Disabled属性
- ASP.NET 2.0中TextBox服务器控件的ReadOnly和Disabled属性
- ASP.NET 2.0中TextBox服务器控件的ReadOnly和Disabled属性
- asp.net 2.0的TextBox遭遇ReadOnly=True时ViewState不回传的问题
- ASP.Net TextBox 只读(ReadOnly)时后台不能赋值取值
- Asp.net 2.0 中如何调用用户控件(UserControl)的相关方法或属性/如何传参等
- ASP.NET 2.0中ReadOnly的TextBox
- asp.net 的 textbox控件怎么设置readonly属性
- ASP.NET 2.0中ReadOnly的TextBox
- asp.net 1.0的问题,2.0的Button有个OnClientClick属性,1.0里面如何用?
- (转)ASP.NET 2.0中如何动态修改Header属性如添加Meta标签
- asp.net 2.0的TextBox遭遇ReadOnly=True时ViewState不回传的问题