ASP.NET2.0中TextBox的两个有趣的属性
点击次数:21 次 发布日期:2008-11-26 12:01:13 作者:源代码网
|
源代码网推荐 源代码网推荐 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; 源代码网推荐 } 源代码网推荐 源代码网推荐 这里限制的只是Text属性,而没有限制提交数据的名称/值的NameValueCollection,因此,通过Request["表单名称"]的方法仍然可以得到值的。下面的例子充分说明了这一点,并且提供了既使用ReadOnly,又可以通过Text属性获得值的方法: 源代码网推荐 源代码网推荐 <%@ Page Language="C#" EnableViewState="false" %> 源代码网推荐 源代码网推荐 <!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 Button1_Click(object sender, EventArgs e) 源代码网推荐 { 源代码网推荐 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(object sender, EventArgs e) 源代码网推荐 { 源代码网推荐 TextBox3.Attributes.Add("readonly", "readonly"); 源代码网推荐 } 源代码网推荐 </script> 源代码网推荐 源代码网推荐 <script type="text/javascript"> 源代码网推荐 //<![CDATA[ 源代码网推荐 function SetNewValue() 源代码网推荐 { 源代码网推荐 document.getElementById("<%=TextBox1.ClientID %>").value = "TextBox1 new Value"; 源代码网推荐 document.getElementById("<%=TextBox2.ClientID %>").value = "TextBox2 new Value"; 源代码网推荐 document.getElementById("<%=TextBox3.ClientID %>").value = "TextBox3 new Value"; 源代码网推荐 } 源代码网推荐 //]]> 源代码网推荐 </script> 源代码网推荐 源代码网推荐 <html xmlns="http://www.w3.org/1999/xhtml"> 源代码网推荐 <head runat="server"> 源代码网推荐 <title>ASP.NET 2.0中TextBox控件与ReadOnly和Enabled属性</title> 源代码网推荐 </head> 源代码网推荐 <body> 源代码网推荐 <form id="form1" runat="server"> 源代码网推荐 <span>TextBox1 ReadOnly:</span> 源代码网推荐 <asp:TextBox ID="TextBox1" runat="server" ReadOnly="True" Text="TextBox1 Old Value"></asp:TextBox><br /> 源代码网推荐 <span>TextBox2 Enabled:</span> 源代码网推荐 <asp:TextBox ID="TextBox2" runat="server" Enabled="False" Text="TextBox2 Old Value"></asp:TextBox><br /> 源代码网推荐 <span>TextBox3 ReadOnly:</span> 源代码网推荐 <asp:TextBox ID="TextBox3" runat="server" Text="TextBox3 Old Value"></asp:TextBox><br /> 源代码网推荐 <br /> 源代码网推荐 <asp:Button ID="Button2" runat="server" Text="修改新值" OnClientClick="SetNewValue();return false;" /> 源代码网推荐 <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的规范:http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.12 源代码网推荐 源代码网推荐 设置为disabled的input将会有下面的限制: 源代码网推荐 源代码网推荐 ·不能接收焦点 源代码网推荐 ·使用tab键时将被跳过 源代码网推荐 ·可能不是successful的 源代码网推荐 源代码网推荐 设置为readonly的input将会有下面的限制: 源代码网推荐 源代码网推荐 ·可以接收焦点但不能被修改 源代码网推荐 ·可以使用tab键进行导航 源代码网推荐 ·可能是successful的 源代码网推荐 源代码网推荐 只有successful的表单元素才是有效数据,也即是可以进行提交。disabled和readonly的文本输入框只能通过脚本进行修改value属性。 源代码网推荐 源代码网推荐 http://dev.yesky.com/msdn/167/3003167.shtml 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
