700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > ASP.NET 2.0中TextBox服务器控件的ReadOnly和Disabled属性

ASP.NET 2.0中TextBox服务器控件的ReadOnly和Disabled属性

时间:2024-01-21 13:18:49

相关推荐

ASP.NET 2.0中TextBox服务器控件的ReadOnly和Disabled属性

在以前的 1.x版本中,设置为ReadOnly的TextBox控件在客户端更改了值后,在服务器端仍然可以得到修改后的值,但在 2.0中,这种做法已经限制。这是为了提高应用程序安全性所考虑的。下面就是TextBox控件获得数据的内部方法,由此可以看出ReadOnly的限制:

protected virtual bool LoadPostData( string postDataKey,NameValueCollectionpostCollection)

{

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 ;

}

这里限制的只是Text属性,而没有限制提交数据的名称/值的NameValueCollection,因此,通过Request["表单名称"]的方法仍然可以得到值的。下面的例子充分说明了这一点,并且提供了既使用ReadOnly,又可以通过Text属性获得值的方法:

<% @PageLanguage = " C# " EnableViewState = " false " %>

<! DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< script runat ="server" >

protected void Button1_Click(objectsender,EventArgse)

{

Response.Write( " <li>TextBox1= " + TextBox1.Text);

Response.Write( " <li>TextBox2= " + TextBox2.Text);

Response.Write( " <li>TextBox3= " + TextBox3.Text);

Response.Write( " <li>Request.Form[TextBox1]= " + Request.Form[TextBox1.UniqueID]);

Response.Write( " <li>Request.Form[TextBox2]= " + Request.Form[TextBox2.UniqueID]);

Response.Write( " <li>Request.Form[TextBox3]= " + Request.Form[TextBox3.UniqueID]);

}

protected void Page_Load(objectsender,EventArgse)

{

TextBox3.Attributes.Add( " readonly " , " readonly " );

}

</ script >

< script type ="text/javascript" >

// <![CDATA[

function SetNewValue()

{

document.getElementById( ' <%=TextBox1.ClientID%> ' ).value = " TextBox1newValue " ;

document.getElementById( ' <%=TextBox2.ClientID%> ' ).value = " TextBox2newValue " ;

document.getElementById( ' <%=TextBox3.ClientID%> ' ).value = " TextBox3newValue " ;

}

// ]]>

</ script >

< html xmlns ="/1999/xhtml" >

< head runat ="server" >

< title > 2.0中TextBox控件与ReadOnly和Enabled属性 </ title >

</ head >

< body >

< form id ="form1" runat ="server" >

< span > TextBox1ReadOnly: </ span >

< asp:TextBox ID ="TextBox1" runat ="server" ReadOnly ="True" Text ="TextBox1OldValue" ></ asp:TextBox >< br />

< span > TextBox2Enabled: </ span >

< asp:TextBox ID ="TextBox2" runat ="server" Enabled ="False" Text ="TextBox2OldValue" ></ asp:TextBox >< br />

< span > TextBox3ReadOnly: </ span >

< asp:TextBox ID ="TextBox3" runat ="server" Text ="TextBox3OldValue" ></ asp:TextBox >< br />

< br />

< asp:Button ID ="Button2" runat ="server" Text ="修改新值" OnClientClick ="SetNewValue();returnfalse;" />

< asp:Button ID ="Button1" runat ="server" Text ="提交" OnClick ="Button1_Click" />

</ form >

</ body >

</ html >

对于disabled的TextBox,在服务器端不能得到修改的值,如果实在要用这个属性,那之后使用隐藏表单域的方法来实现了。

ReadOnly属性的TextBox在客户端会展现成这样的标记:

<input readonly = "readonly">

Enabled属性的TextBox在客户端会展现成这样的标记:

<input disabled="disabled">

按照W3C的规范:/TR/REC-html40/interact/forms.html#h-17.12

设置为disabled的input将会有下面的限制:

不能接收焦点 使用tab键时将被跳过 可能不是successful的

设置为readonly的input将会有下面的限制:

可以接收焦点但不能被修改 可以使用tab键进行导航 可能是successful的

只有successful的表单元素才是有效数据,也即是可以进行提交。disabled和readonly的文本输入框只能通过脚本进行修改value属性。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。