linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘网络学院网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > 开发语言 > ASP >
栏目导栏
  php
  JSP
  ASP
  asp.net
  JAVA
  c/c++/c#
  perl
  JavaScript
  Basic
  Delphi
资料搜索
热门文章
·由HTTP 500 Internal server e
·会员系统“找回密码”的制作方
·ASP实现数字和字母组合并生成图
·简单的asp验证码程序示例
·用ASP制作强大的搜索引擎
·如何让图片自动缩放以适合界面
·如何用foreach遍历页面上所有的
·如何实现站点的RSS输出
·无组件实现文件上传/下载
·ASP如何获取真实IP地址
·asp中如何使用 HTML 表格
·采用XMLHTTP编写一个天气预报的
·80004005错误及其解决方法
·asp中如何调试 ASP 脚本
·asp+ajax打造无刷新新闻评论系
最新文章
·ASP教程:8、ASP内建对象Respo
·ASP教程:7、ASP内建对象Reque
·ASP教程:6、ASP脚本循环语句
·ASP教程:5、ASP脚本变量、函数
·ASP教程:4、ASP脚本基础
·ASP教程:3、ASP基础
·ASP教程:2、ASP简介
·ASP教程:1、Are you ready?
·ASP中使用SQL语句教程
·ASP进度条
·由HTTP 500 Internal server e
·Asp无组件生成缩略图
·ASP 系列函数大全(4)
·ASP 系列函数大全(3)
·ASP 系列函数大全(2)
Google
 
ASP.NET 2.0中使用HiddenField控件
[ 作者:  加入时间:2007-11-28 13:44:53  来自:Linux联盟收集整理 ]
 HiddenField控件顾名思义就是隐藏输入框的服务器控件,它能让你保存那些不需要显示在页面上的且对安全性要求不高的数据。也许这个时候应该有这么一个疑问,为什么有了ViewState、Session和Cookie等状态保存机制,还需要用起HiddenField呢? yeDLinux联盟
   yeDLinux联盟
    增加HiddenField,其实是为了让整个状态管理机制的应用程度更加全面。因为不管是ViewState、Cookie还是Session,都有其失效的时候,比如用户因某种需求要求设置ViewState为false,或者环境条件限制使用Cookie,或者用户长时间没有动作导致Session过期等等,那这个时候HiddenField无疑是最佳选择。 yeDLinux联盟
   yeDLinux联盟
    HiddenField控件的作用简单的说是用于存储需要在向服务器的发送间保持的值。它作为 <input type= "hidden"/> 元素呈现,并且通过添加runat=”server”就可以使它成为标准的HTML服务器控件。下面列出的是ASP.net HiddenField Web服务器控件可以使用的属性和事件。 yeDLinux联盟
   yeDLinux联盟
  <asp:HiddenField yeDLinux联盟
   EnableTheming="True|False" yeDLinux联盟
   EnableViewState="True|False" yeDLinux联盟
   ID="string" yeDLinux联盟
   OnDataBinding="DataBinding event handler" yeDLinux联盟
   OnDisposed="Disposed event handler" yeDLinux联盟
   OnInit="Init event handler" yeDLinux联盟
   OnLoad="Load event handler" yeDLinux联盟
   OnPreRender="PreRender event handler" yeDLinux联盟
   OnUnload="Unload event handler" yeDLinux联盟
   OnValueChanged="ValueChanged event handler" yeDLinux联盟
   runat="server" yeDLinux联盟
   SkinID="string" yeDLinux联盟
   Value="string" yeDLinux联盟
   Visible="True|False" yeDLinux联盟
  /> yeDLinux联盟
   yeDLinux联盟
    因为 HiddenField 的值将呈现给客户端浏览器,所以它不适用于存储安全敏感的值。若要为 HiddenField 控件指定值,请使用 Value 属性,请注意是Value而不是Text。事实上HiddenField并没有Text属性,这和DropDownList、CheckBoxList等标准按钮的属性命名方式一致。在标准的属性命名方式中,Text的值是呈现给用户看到的,而Value的值则是通长是通过代码进行控制的。例如你可以让DropDownList的Text属性显示用户名而让它的Value存储用户的编号。 yeDLinux联盟
   yeDLinux联盟
    下面的代码显示的是改控件的基本使用。 yeDLinux联盟
   yeDLinux联盟
  <html> yeDLinux联盟
  <head> yeDLinux联盟
  <script language="C#" runat="server"> yeDLinux联盟
  void Button1_Click(object sender, EventArgs e) yeDLinux联盟
  { yeDLinux联盟
   if (HiddenField1.Value == String.Empty) yeDLinux联盟
    HiddenField1.Value = "0"; yeDLinux联盟
   yeDLinux联盟
    HiddenField1.Value = (Convert.ToInt32(HiddenField1.Value)+1).ToString(); yeDLinux联盟
    Label1.Text = HiddenField1.Value; yeDLinux联盟
  } yeDLinux联盟
  </script> yeDLinux联盟
  </head> yeDLinux联盟
  <body> yeDLinux联盟
  <h3><font face="Verdana">HiddenField</font></h3> yeDLinux联盟
  <form runat=server> yeDLinux联盟
  <asp:HiddenField id=HiddenField1 runat=Server /> yeDLinux联盟
  <asp:Button id=Button1 Text="单击按钮" onclick="Button1_Click" runat="server" /> yeDLinux联盟
  单击 <asp:Label id=Label1 Text="0" runat=server /> 次 yeDLinux联盟
  </form> yeDLinux联盟
  </body> yeDLinux联盟
  </html> yeDLinux联盟
   yeDLinux联盟
    在上面代码中, <asp:HiddenField id=HiddenField1 runat=Server />就定义了一个隐藏控件在按钮的单击事件里计算用户单击的次数,并将改次数赋值给Label1。 yeDLinux联盟
   yeDLinux联盟
    你可以将上面代码中的 <asp:HiddenField id=HiddenField1 runat=Server />改为<input type=hidden id=HiddenField1 runat=Server >也是可以的 yeDLinux联盟
   yeDLinux联盟
    在使用上面代码里,如果你从浏览器里查看源代码会得到如下的信息: yeDLinux联盟
   yeDLinux联盟
  <form name="Form1" method="post" action="Default.aspx" id="Form1"> yeDLinux联盟
   yeDLinux联盟
    这是因为HiddenField是通过HTTP协议进行传递数据的,所以如果你通过" method="get"或者链接打开新的窗体页,那么HiddenField并不可用。 yeDLinux联盟
   yeDLinux联盟
    另外,HiddenField并不是取代Session来维护状态的,在上面例子里,虽然你点击一次按钮可以显示你点击的次数但是并不是说它可以记录你的状态信息。如果你重新打开浏览器那么你看到的此处仍然是0而不是3。 yeDLinux联盟
   yeDLinux联盟
HiddenField事件 yeDLinux联盟
   yeDLinux联盟
    HiddenField较为常用的是ValueChanged事件,该事件在Value值发生改变时触发该事件。然而在实际使用时,要知道页面记载顺序。在页面回传过程中,具体的页面周期你可以到如下网站查看 yeDLinux联盟
   yeDLinux联盟
  http://msdn2.microsoft.com/zh-cn/library/ms178472.ASPx yeDLinux联盟
   yeDLinux联盟
    下面的例子说明了这个问题 yeDLinux联盟
   yeDLinux联盟
  <head> yeDLinux联盟
  <script runat="server" language="C#"> yeDLinux联盟
  protected void Page_Load(object sender, EventArgs e) yeDLinux联盟
  { yeDLinux联盟
   Response.Write("<p>页面的Page_Load事件触发,触发时间是:" + DateTime.Now.ToString()); yeDLinux联盟
   if (HiddenField1.Value == String.Empty) yeDLinux联盟
    HiddenField1.Value = "0"; } yeDLinux联盟
   yeDLinux联盟
    protected void Button1_Click(object sender, EventArgs e) yeDLinux联盟
    { yeDLinux联盟
     Response.Write("<p>Button1_Click为改变Hidden的值前事件触发,触发时间是:" + DateTime.Now.ToString()); yeDLinux联盟
  HiddenField1.Value = (Convert.ToInt32(HiddenField1.Value) + 1).ToString(); yeDLinux联盟
     Label1.Text = HiddenField1.Value; yeDLinux联盟
    } yeDLinux联盟
    protected void HiddenField1_ValueChanged(object sender, EventArgs e) yeDLinux联盟
    { yeDLinux联盟
     Response.Write("<p>HiddenField的 ValueChanged事件触发,触发时间是:" + DateTime.Now.ToString()); } yeDLinux联盟
  </script> yeDLinux联盟
  </head> yeDLinux联盟
   yeDLinux联盟
  <body> yeDLinux联盟
  <form id="form1" runat="server"> yeDLinux联盟
  <div> <asp:HiddenField ID="HiddenField1" runat="server" OnValueChanged="HiddenField1_ValueChanged" /> yeDLinux联盟
  </div> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> yeDLinux联盟
  <br /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> yeDLinux联盟
  </form></body> yeDLinux联盟
  </html> yeDLinux联盟
   yeDLinux联盟
    在这个例子里,我们想要的结果是:当用户单击按钮时,通过按钮的Button1_Click事件更改HiddenField1的Value,然后再触发HiddenField1的HiddenField1_ValueChanged事件,然而事实真的如此吗? yeDLinux联盟
   yeDLinux联盟
    运行上面的代码,得到的结果正如你看到的,每次单击时,Button确实改变了HiddenField的值,但是我们在HiddenField1_ValueChanged里定义的输出并没有执行,换句话说页面并没有执行ValueChanged事件。 yeDLinux联盟
   yeDLinux联盟
    要理解这个问题还需要了解页面的声明周期,在页面周期过程中,你可以看到在Page_Init里是读取或初始化控件属性,然后再是Control events。 yeDLinux联盟
   yeDLinux联盟
    这里的事件意思是说: 在Page_Init事件里,Web页面会接受用户回传的数据,例如将 <span id="Label1">Label</span>赋值给ID为Label1的Text属性,将 <input type="hidden" name="HiddenField1" id="HiddenField1" value="0" />的value值赋值给HiddenField1的Value属性。等所有初始化完毕了,页面才开始执行控件的事件--Button1_Click,在Button事件里将HiddenField的Value值改变。那么这里既然已经将Value值改变为什么没有执行ValueChanged事件呢? yeDLinux联盟
   yeDLinux联盟
    此时,虽然这里已经将Value值改变,但是在Page_Init里保存的这是因为当前单击Button按钮时,虽然改变了HiddenField但同样又一次触发了页面的回复,也就是虽然在上一此HiddenValue的值为0,而此次将其值改为1,但是在页面回传后,由于ViewState会保存上次的装(这里是1),所以在Page_Init里,认为HiddenField的初始值为1,而本次还是1,使得它感觉数据没有变化,所以仍然不会触发ValueChanged事件 yeDLinux联盟
   yeDLinux联盟
    当然,你可以禁用HiddenField来进行处理,就可以执行ValueChanged事件,但是事实上你禁用ViewState后,页面不再保存ViewState的值使得页面认为每一次请求HiddenField的都是新的,例如如下代码: yeDLinux联盟
   yeDLinux联盟
    你并没有改变Hiddenfield的值,但是仍然每次都执行。 yeDLinux联盟
   yeDLinux联盟
  <%@ Page EnableViewState="false" %> yeDLinux联盟
  <head> yeDLinux联盟
  <script runat="server" language="c#"> yeDLinux联盟
  protected void Page_Load(object sender, EventArgs e) yeDLinux联盟
  { yeDLinux联盟
   if (HiddenField1.Value == String.Empty) yeDLinux联盟
    HiddenField1.Value = "111"; yeDLinux联盟
  } yeDLinux联盟
   yeDLinux联盟
  protected void Button1_Click(object sender, EventArgs e) yeDLinux联盟
  { yeDLinux联盟
   // HiddenField1.Value = (Convert.ToInt32(HiddenField1.Value) + 1).ToString(); yeDLinux联盟
   Label1.Text = TextBox1.Text; yeDLinux联盟
  } yeDLinux联盟
   yeDLinux联盟
  protected void HiddenField1_ValueChanged(object sender, EventArgs e) yeDLinux联盟
  { yeDLinux联盟
   Response.Write("Changed." + DateTime.Now.ToString()); yeDLinux联盟
   Response.Write(HiddenField1.Value); yeDLinux联盟
   Response.Write(TextBox1.Text); yeDLinux联盟
  } yeDLinux联盟
   yeDLinux联盟
  </script> yeDLinux联盟
  </head> yeDLinux联盟
  <body> yeDLinux联盟
   yeDLinux联盟
  <form id="form1" runat="server"> <div> yeDLinux联盟
  <asp:HiddenField ID="HiddenField1" runat="server" OnValueChanged="HiddenField1_ValueChanged" /> yeDLinux联盟
  </div> yeDLinux联盟
  <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> yeDLinux联盟
  <br /> yeDLinux联盟
  <asp:TextBox runat=server ID=TextBox1></asp:TextBox> yeDLinux联盟
  <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> yeDLinux联盟
  </form> yeDLinux联盟
  </body> yeDLinux联盟
  </html> yeDLinux联盟
   yeDLinux联盟
    利用HiddenFile 并结合ASP.NET2.0提供的“交叉Cross Page”页面可以实现页面数据的传递,该情况针对这样的一种方案: yeDLinux联盟
  在一个注册页面里,需要用户输入数据,由于备注一栏的数据可能很多,可以在新窗口可以利用类似FreeTextBox的控件让用户 yeDLinux联盟
  格式化文本,并在输入完毕后,返回原注册页面。关于这种情况,以后会介绍。 yeDLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·ASP.NET 2.0 服务器控件之复合控件事件  (2007-11-27 17:33:02)
 ·ASP.NET 2.0高级数据处理之数据绑定  (2007-11-26 16:24:44)
 ·ASP.NET 2.0 里输出文本格式流  (2007-11-26 16:24:15)
 ·用ASP.NET 2.0 FormView控件控制显示  (2007-11-26 16:19:49)
 ·Scott Mitchell 的ASP.NET 2.0数据教程之应用笔记  (2007-11-21 15:54:26)
 ·ASP.NET 2.0 X64的奇怪问题  (2007-11-16 12:40:28)
 ·在 Linux 下运行 ASP.NET 2.0  (2007-11-02 13:44:00)
 ·ASP.NET 2.0如何封装JS和CSS文件为服务器端控件  (2007-10-29 14:13:11)
 ·asp.net 2.0 ajax中使用PopupControlExtender  (2007-10-26 13:13:35)
 ·asp.net 2.0 ajax中实现弹出窗口报警提示  (2007-10-26 13:12:27)