0

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

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 ?

Community
  • 1
  • 1
Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133
  • You may have used an update panel in the page and your hidden field is inside of that Update Panel's Content Template. During post back it updates hidden field value since it is an client side control. – tarzanbappa Feb 21 '14 at 07:31
  • `` is just text, not control. You may know by checking outputted html source code. – Jumpei Feb 21 '14 at 08:00

1 Answers1

2

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.

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.

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

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • is their any alternate to using hiddenfields / – Mudassir Hasan Feb 21 '14 at 09:29
  • @mhasan Any type of input control is post data, the TextBox is rendered on input control for example. Look the types of :http://dev.w3.org/html5/markup/input.html – Aristos Feb 21 '14 at 09:43
  • something very strange.. I am using textbox to show Total values ... when I do textbox property `Readonly=true` then I am not able to access the textbox value but when I set `Enabled="false" `, value is captured in codebehind ... I don't want gery text color for `Enabled="flase"` because it makes text harder too read... – Mudassir Hasan Feb 21 '14 at 10:31
  • @mhasan If you can not get the value of it using asp.net for any reason, then use the `Form[ControlID.UniqueID]` to see what is post back. – Aristos Feb 21 '14 at 11:06
  • I used `input type="text readonly="true">` and it works now..thanks for helping out. – Mudassir Hasan Feb 21 '14 at 11:08