views:

446

answers:

1

I have an event handler in C# code-behind for an .aspx page. Each time this event handler fires I want it to update a Javascript variable. I thought I could do something like this.

int scale = (int)myObject.Scale;
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "JSVariables", "scale=" + scale, true);

However, as it was pointed out to me earlier here, RegisterClientScript checks first to see whether or not the script has been registered and if it has it does nothing. In essence the variable is updated once then never again.

Is it possible to have this value continuously updated?

The most I can really do with Ajax is to have a static web method. For a couple of reasons that are unnecessary to explain here I can't use a WCF or web service, and the problem with the static web method is the static bit.

Is there any other way?

I was thinking maybe some session trickery but I'm not sure yet.

+2  A: 

Might be a good idea to take a mental step back and re-examine what you are actually trying to do, and see if there is another way to accomplish it. For instance, what is triggering the event handler to fire in the first place. If it is some action in the browser/javascript that occurs that might cause the event handler to fire, it might be easier to just initiate an HttpRequest whenever that action occurs, and see if the server side variable has changed.

Chris Lawlor