为什么HiddenField值在回发时仍然存在,而其他控件丢失了它们的内容? [英] Why HiddenField value persist on postback while other controls lose their contents?

查看:90
本文介绍了为什么HiddenField值在回发时仍然存在,而其他控件丢失了它们的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有GridView,其中有一个FooterRow,我在其中使用标签显示每列的总计.

I have GridView which has a FooterRow where I am using labels to display Totals of each column.

我正在访问JQuery中的GridView,并且使用JQuery成功计算了总数,并显示在GridView的每一列的FooterRow中.

I am accessing GridView in JQuery and the totals are calculated successfully using JQuery and displayed in FooterRow of each column of GridView.

 <asp:TemplateField HeaderText="Age">
    <ItemTemplate>
      <asp:Label ID="lblAge" Text='<%# Eval("Age") %>' runat="server"></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
      <asp:Label ID="lbltotalAge" runat="server"></asp:Label>
    </FooterTemplate>
 </asp:TemplateField>

但是,当我单击提交"按钮并尝试在按钮单击事件内部的代码中访问这些值时,所有总计"值都不会显示,而是保留为默认的零值.

But when I click submit button and try to access these values in code behind inside button click event , all the Total values are not shown and remain as default Zero value.

string totalAge = ((Label)GrdV.FooterRow.FindControl("lbltotalAge")).Text; // always 0

为解决此问题,我已按照

To overcome this I have used HiddenField controls outside the GridView as suggested in this previous POST.
After calculating Total values in Jquery function I copy these values to respective HiddenFields and successfully getting the values in codebehind.

我的问题是为什么会这样?隐藏字段有什么特别之处?

My question is why is this so ? Whats special about hiddenfields ?

推荐答案

不是因为隐藏字段,而是因为将呈现为html输入控件的呈现为回传其值,因此您可以拥有发回帖子后再来一次.

Is not because are hidden fields, but because there are rendered as html input controls that post back their value and so you can have it again after the post back.

其他未呈现为输入控件的控件,它们无法回传其值,并且asp.net将先前的值保存在页面的viewstate中,这也是一个隐藏的输入控件,用于保存该数据并将其回传.因此,在回发之后,在asp.net背后的代码上解码视图状态,并将值提供给该控件.

The other controls that not rendered as input controls, they can not post back their value, and asp.net saves the previous value on viewstate of the page, which is again a hidden input control that hold that data and post them back. So after the post back, on code behind asp.net decode the view state, and give the values to that controls.

例如,TextBox渲染作为输入控件,而Literal不是.

For example, the TextBox render as input control, the Literal is not.

这篇关于为什么HiddenField值在回发时仍然存在,而其他控件丢失了它们的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆