views:

556

answers:

3

I have looked at similar questions on Stack Overflow but haven't been able to find the solution to this particular problem. I have a simple (not really, but let's pretend) ASP.NET page with an update panel inside it.

In this update panel there is a HiddenField control with a value that will be set/updated via my own custom JavaScript. The value is updated as I can see in the DOM using Firebug.

Next to this HiddenField there is a LinkButton which calls the JavaScript to modify the value of the HiddenField using OnClientClick (this works). The LinkButton also has an OnClick handler which is executed after the value has been modified.

However, in the OnClick handler, I get the initial value of the HiddenField and not the value which was set by JavaScript!

Why is this? Is this a case of "PEBKAC"?

EDIT: Tried the same thing with a TextBox without any success. Also tried modifying the value manually using Firebug before submitting the form, without any success.

EDIT 2: I just realised that Page_Load is called before the OnClick handler, which was messing things up. I will not delete the question as other people may have the same problem and may find this question valuable.

A: 

You will have to do it using EndRequestHandler event, provided by the AJAX Update Panel.

Please look at this-->

http://www.codeproject.com/KB/ajax/AfterAjaxUpdate.aspx?display=Print

Bhaskar
That seems to be dealing with stuff that happens *after* an AJAX request, whereas I merely want my `OnClick` handler to get access to the new value.
Deniz Dogan
You will have to consider AJAX as your control is inside the Update Panel.
Bhaskar
A: 

How are you generating the new input on the client? Would it be possible to perform this on the server instead?

This way you can use the just the OnClick instead of both the OnClick and OnClientClick.

Chris
It is not possible to generate the new input on the server.
Deniz Dogan
A: 

This appears to me as if the viewstate makes you a bad joke (as often). What I guess is that when the page posts back, the hidden field is initialized with the value contained in the Viewstate.

Try one thing: Don't access the value directly through yourHiddenField.Value but in the OnInit of your page get it with Request.Forms[yourHiddenField.UniqueId] and cast it to a HiddenField and extract the value. I'm not sure whether this is the right syntax. The "posted" values should be in the Request object somewhere if I remember right. See whether you get the value there. If you get it there, the problem is the viewstate which is mapped back after the OnInit event of the page. Then we have to find a solution for that...

Juri