|
 |
栏目导栏 |
|
| |
|
|
|
|
 |
资料搜索 |
|
| |
|
|
|
|
 |
热门文章 |
|
| |
|
|
|
|
 |
最新文章 |
|
| |
|
|
|
| |
| |
|
|
|
| |
| ASP.NET 2.0中使用HiddenField控件 |
|
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论坛讨论 |
|
|
|
|
|