Monday, March 26, 2012

Callback to server (by javascript) which updates an UpdatePanel

I already posted this in http://forums.asp.net/thread/1178950.aspx
but it seems to be a very general questions about callback in ASP.NET 2.0


I have a custom (image map like) control which reacts to clicks bya javascript. the javascript part sets postback values like x, y, andraises a postback by form.submit().

Now when I place thiscontrol on an UpdatePanel, the javascript:form.submit() raises apostback, but not a partial postback...

Do I have to register thejavascript somewhere else (expect RegisterClientScript or so)?

Or isthere a way to make a postback in order that the server updates theUpdatePanel with a new image?
I tried the Callback (implementing ICallBackProvider), but this seems only to be for delivering a stringvalue from server to client (at least I couldnt kick off any partialupdate of the page..)

Regards,

RayHi Ray,

Executing form.submit() will never cause a partial postback. A partial postback occurs within an UpdatePanel if the following is true. The control causing the postback is within the UpdatePanel or the control causing the postback is setup as a trigger of the UpdatePanel. These are the two situations that cause a postback.

In your case, there are a couple of options (or lots of options that I can't think of).

One option is that your custom control's click event could cause a postback. Causing a postback boils down to executing a JavaScript command: "__doPostback(params);". I'm not sure how you're registering the client side JavaScript that sets postback values X and Y, but you can generate the necessary JavaScript to cause a postback by executing "Page.ClientScript.GetPostBackClientHyperlink (params)" in code behind. You then associate this code with the onclick clientside event by executing "this.Attributes.add("onclick",postbackCommand)." It seems that this line of JavaScript would go after whatever other clientside code you needed to execute that set X and Y.

If for some reason doing it this way is not an option, you can always place a hidden button (not .NET hidden but CSS hidden) within the UpdatePanel and programmatically through JavaScript, click it. This presents some problems because you have to have the ClientId to find the exact button you need, which can be difficult as .NET generates this Id for you based upon control hierarchy.

So those are two options for causing a postback in your situation.

Hope this helps.

- Joel

Hi Joel

Thank you, the first option (__doPostback) works great for me.

Regards,

Ray

No comments:

Post a Comment