增加hiddenfield,其实是为了让整个状态管理机制的应用程度更加全面。因为不管是viewstate、cookie还是session,都有其失效的时候,比如用户因某种需求要求设置viewstate为false,或者环境条件限制使用cookie,或者用户长时间没有动作导致session过期等等,那这个时候hiddenfield无疑是最佳选择。
hiddenfield控件的作用简单的说是用于存储需要在向服务器的发送间保持的值。它作为 <input type= "hidden"/> 元素呈现,并且通过添加runat=”server”就可以使它成为标准的html服务器控件。下面列出的是asp.net hiddenfield web服务器控件可以使用的属性和事件。
<asp:hiddenfield enabletheming="true|false" enableviewstate="true|false" id="string" ondatabinding="databinding event handler" ondisposed="disposed event handler" oninit="init event handler" onload="load event handler" onprerender="prerender event handler" onunload="unload event handler" onvaluechanged="valuechanged event handler" runat="server" skinid="string" value="string" visible="true|false" /> |
<html> <head> <script language="c#" runat="server"> void button1_click(object sender, eventargs e) { if (hiddenfield1.value == string.empty) hiddenfield1.value = "0"; hiddenfield1.value = (convert.toint32(hiddenfield1.value)+1).tostring(); label1.text = hiddenfield1.value; } </script> </head> <body> <h3><font face="verdana">hiddenfield</font></h3> <form runat=server> <asp:hiddenfield id=hiddenfield1 runat=server /> <asp:button id=button1 text="单击按钮" runat="server" /> 单击 <asp:label id=label1 text="0" runat=server /> 次 </form> </body> </html> |
<form name="form1" method="post" action="default.aspx" id="form1"> |
hiddenfield事件
hiddenfield较为常用的是valuechanged事件,该事件在value值发生改变时触发该事件。然而在实际使用时,要知道页面记载顺序。在页面回传过程中,具体的页面周期你可以到如下网站查看
http://msdn2.microsoft.com/zh-cn/library/ms178472.aspx
下面的例子说明了这个问题
<head> <script runat="server" language="c#"> protected void page_load(object sender, eventargs e) { response.write("<p>页面的page_load事件触发,触发时间是:" + datetime.now.tostring()); if (hiddenfield1.value == string.empty) hiddenfield1.value = "0"; } protected void button1_click(object sender, eventargs e) { response.write("<p>button1_click为改变hidden的值前事件触发,触发时间是:" + datetime.now.tostring()); hiddenfield1.value = (convert.toint32(hiddenfield1.value) + 1).tostring(); label1.text = hiddenfield1.value; } protected void hiddenfield1_valuechanged(object sender, eventargs e) { response.write("<p>hiddenfield的 valuechanged事件触发,触发时间是:" + datetime.now.tostring()); } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:hiddenfield id="hiddenfield1" runat="server" onvaluechanged="hiddenfield1_valuechanged" /> </div> <asp:label id="label1" runat="server" text="label"></asp:label> <br /> <asp:button id="button1" runat="server" onclick="button1_click" text="button" /> </form></body> </html> |
<%@ page enableviewstate="false" %> <head> <script runat="server" language="c#"> protected void page_load(object sender, eventargs e) { if (hiddenfield1.value == string.empty) hiddenfield1.value = "111"; } protected void button1_click(object sender, eventargs e) { // hiddenfield1.value = (convert.toint32(hiddenfield1.value) + 1).tostring(); label1.text = textbox1.text; } protected void hiddenfield1_valuechanged(object sender, eventargs e) { response.write("changed." + datetime.now.tostring()); response.write(hiddenfield1.value); response.write(textbox1.text); } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:hiddenfield id="hiddenfield1" runat="server" onvaluechanged="hiddenfield1_valuechanged" /> </div> <asp:label id="label1" runat="server" text="label"></asp:label> <br /> <asp:textbox runat=server id=textbox1></asp:textbox> <asp:button id="button1" runat="server" onclick="button1_click" text="button" /> </form> </body> </html> |
新闻热点
疑难解答
图片精选