Reputation:
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?
Upvotes: 4
Views: 31900
Reputation: 1078
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;
}
}
Upvotes: 0
Reputation: 71
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!
Upvotes: 2