4

I have two hidden controls:

<asp:HiddenField runat="server" id="pageHeader" />
<asp:HiddenField runat="server" id="pageInformation" />

I am calling following function from master page:

show_tip(this, document.getElementById('ctl00_pageInformation').value, document.getElementById('ctl00_pageHeader').value);

and i am passing values in hidden field on .cs page in page load as follows:

 string message = Request.Form["pageInformation"];
 if (string.IsNullOrEmpty(message))
 {
      ((HiddenField)Master.FindControl("pageHeader")).Value = pageHeading;
      ((HiddenField)Master.FindControl("pageInformation")).Value = pageInformation;
 }

This is working fine, but on page POSTBACK, hidden fields lose their value. How can I retain the values after postback?

Kirtan
  • 21,295
  • 6
  • 46
  • 61
  • Are you assigning any values to those hidden fields on page postback? Please look at your code carefully, and post it here, if possible. – Kirtan Jul 10 '09 at 06:48
  • no i am not doing anything on postback,I ave pasted the code already –  Jul 10 '09 at 07:00
  • Please look at the following 2 links, you can find solution from one of these: - [is there a way to hold the values? - lost in postback](http://stackoverflow.com/questions/2670327/is-there-a-way-to-hold-the-values-lost-in-postback) - [Hidden value assigned in js lost after postback](http://stackoverflow.com/questions/6270085/hidden-value-assigned-in-js-lost-after-postback) Let me know your results. – Arun Rana Oct 25 '11 at 12:09
  • 1
    Just use a `Label` Control and set `visible = false`. The `Hidden` field is not used like this. –  Nov 29 '11 at 16:53
  • 1
    A similar question with good answers: http://stackoverflow.com/questions/3961554/how-can-i-save-asphiddenfield-value-across-postback – John Pick Mar 08 '12 at 07:31

2 Answers2

2

OK this is what you do.

Two functions and a hidden field. The first functions in JS adds a handler which gets the values from the hidden fields and stores them in variables. The second function in JS adds a handler which gets the values from the variables and puts them back into the hidden fields.

<script type="text/javascript">
        var txt1;
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_beginRequest(BeginRequestHandler);
        prm.add_endRequest(EndRequestHandler);

        function BeginRequestHandler(sender, args) {
            txt1 = $get('<%= hdntxt1.ClientID%>').value;
        }
        function EndRequestHandler(sender, args) {
            $get('<%= hdntxt1.ClientID%>').value = txt1;
        }
</script>
<asp:HiddenField runat="server" ID="hdntxt1" Value="" />

You don't actually need to use hidden fields however if other parts of the form need to obtain the values then those values will be handy at any time regardless of postbacks!

Dipesh Patel
  • 71
  • 1
  • 5
0

I guess your hidden field value is getting reset on post back. Try keeping your code inside if block cheking for postback

if(!ispostback)
{
 string message = Request.Form["pageInformation"];
 if (string.IsNullOrEmpty(message))
 {
  ((HiddenField)Master.FindControl("pageHeader")).Value = pageHeading;
  ((HiddenField)Master.FindControl("pageInformation")).Value = pageInformation;
 }

}
Neelam
  • 1,028
  • 12
  • 25