增加HiddenField,其实是为了让整个状态管理机制的应用程度更加全面。因为不管是ViewState、Cookie还是Session,都有其失效的时候,比如用户因某种需求要求设置ViewState为false,或者环境条件限制使用Cookie,或者用户长时间没有动作导致Session过期等等,那这个时候HiddenField无疑是最佳选择。
HiddenField控件的作用简单的说是用于存储需要在向服务器的发送间保持的值。它作为 <input type= "hidden"/> 元素呈现,并且通过添加runat=“server”就可以使它成为标准的HTML服务器控件。下面列出的是ASP.NET HiddenField Web服务器控件可以使用的属性和事件。
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
下面的代码显示的是改控件的基本使用。
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
你可以将上面代码中的 <asp:HiddenField id=HiddenField1 runat=Server />改为<input type=hidden id=HiddenField1 runat=Server >也是可以的
在使用上面代码里,如果你从浏览器里查看源代码会得到如下的信息:<form name="Form1" method="post" action="Default.aspx" id="Form1">这是因为HiddenField是通过HTTP协议进行传递数据的,所以如果你通过" method="get"或者链接打开新的窗体页,那么HiddenField并不可用。
另外,HiddenField并不是取代Session来维护状态的,在上面例子里,虽然你点击一次按钮可以显示你点击的次数但是并不是说它可以记录你的状态信息。如果你重新打开浏览器那么你看到的此处仍然是0而不是3.
HiddenField事件
HiddenField较为常用的是ValueChanged事件,该事件在Value值发生改变时触发该事件。然而在实际使用时,要知道页面记载顺序。在页面回传过程中,具体的页面周期你可以到如下网站查看http://msdn2.microsoft.com/zh-cn/library/ms178472.aspx下面的例子说明了这个问题
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
运行上面的代码,得到的结果正如你看到的,每次单击时,Button确实改变了HiddenField的值,但是我们在HiddenField1_ValueChanged里定义的输出并没有执行,换句话说页面并没有执行ValueChanged事件要理解这个问题还需要了解页面的声明周期,在页面周期过程中,你可以看到在Page_Init里是读取或初始化控件属性,然后再是Control events.这里的事件意思是说: 在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事件呢?
此时,虽然这里已经将Value值改变,但是在Page_Init里保存的这是因为当前单击Button按钮时,虽然改变了HiddenField但同样又一次触发了页面的回复,也就是虽然在上一此HiddenValue的值为0,而此次将其值改为1,但是在页面回传后,由于ViewState会保存上次的装(这里是1),所以在Page_Init里,认为HiddenField的初始值为1,而本次还是1,使得它感觉数据没有变化,所以仍然不会触发ValueChanged事件
当然,你可以禁用HiddenField来进行处理,就可以执行ValueChanged事件,但是事实上你禁用ViewState后,页面不再保存ViewState的值使得页面认为每一次请求HiddenField的都是新的,例如如下代码:你并没有改变Hiddenfield的值,但是仍然每次都执行。
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
利用HiddenFile 并结合ASP.NET2.0提供的“交叉Cross Page”页面可以实现页面数据的传递,该情况针对这样的一种方案:在一个注册页面里,需要用户输入数据,由于备注一栏的数据可能很多,可以在新窗口可以利用类似FreeTextBox的控件让用户格式化文本,并在输入完毕后,返回原注册页面。关于这种情况,以后会介绍