当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET2.0中TextBox的两个有趣的属性

ASP.NET
使用函数传递参数来执行相应的数据库操作
如何实现在窗体和窗体之间进行传递数据
ASP.NET中文显示之两种解决方法
ASP.NET、JSP及PHP之间的抉择
ASP.NET 2.0发送电子邮件中存在的问题
谈谈HtmlControl与WebControl的区别与用途
从ASP.NET 1.1升级到ASP.NET 2.0要考虑的Cookie问题
通过系统配置来提高ASP.NET应用程序的稳定性
妙用ASP2.0中的URL映射改变网址
AJAX实现web页面中级联菜单的设计
ASP.NET跨页面传值技巧总结
再议ASP.NET DataGrid控件中的“添加新行”功能
Geometry 对象浅析
重构CollapsibleSplitter
如何利用.NET Framework使用RSS feed
ASP.NET获取IP与MAC地址的方法
在ASP.NET 2.0中使用样式、主题和皮肤
ASP.NET中为GridView添加删除提示框
ASP.NET 2.0,无刷新页面新境界
看看一个.net版对话框控件

ASP.NET2.0中TextBox的两个有趣的属性


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 37 ::
收藏到网摘: n/a

    在以前的ASP.NET 1.x版本中,设置为ReadOnly的TextBox控件在客户端更改了值后,在服务器端仍然可以得到修改后的值,但在ASP.NET 2.0中,这种做法已经限制。这是为了提高应用程序安全性所考虑的。下面就是TextBox控件获得数据的内部方法,由此可以看出ReadOnly的限制:
  
  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,在服务器端不能得到修改的值,如果实在要用这个属性,那之后使用隐藏表单域的方法来实现了