Asp – AJAX.NET request handlers – set hidden field

ajaxasp.net

I'm trying to set the value of a hiddenfield control in an AJAX initialize request handler. However on the server the hidden field control always contains the value for the previous postback. I presume the viewstate is being prepared / send before I set the hidden field in the initialize request handler.
Is there any way to set the hidden field so that is passes the new value through or perhaps pass a value to the server via another mechanism.

This is the code I'm using:

var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(MyPage_initializeRequestHandler);

function MyPage_initializeRequestHandler(sender, args)
{
    var hiddenField1= $get('hiddenField1');

    if (hiddenField1 != null)
    {
        hiddenField1.value = 'test';
    }
}

Many thanks.

Best Solution

Are you using update panels?

If you are then you need to make sure that the hidden field is inside the update panel that is being refreshed, otherwise the new value will not be sent to the browser.

Also, how are you creating the hidden field, if it's part of an update panel post back you should be using ScriptManager.RegisterHiddenField.

HTH's