ClientScriptManager.GetPostBackEventReference Method


I'm trying to understand what this method means as I'm reading this and have wondered what uses this may have. I don't quite understand the example given.

Can anyone give another explanation of it. Examples would help.


Best Solution

The simplest example is a LinkButton. Drop one in a page and look at the HTML it generates. You'll see something like.


GetPostBackEventReference allows you to get that piece of JavaScript, so that you can trigger that postback from elsewhere. However you run that bit of JavaScript, a postback will happen and the OnClick event will fire on the server just as if you'd clicked the LinkButton. The example on MSDN wires up a similar bit of JavaScript to the links to trigger server-side events on the GridView.

The more practical uses are when you want to handle postbacks in a custom control. Your control implements IPostBackEventHandler to handle the postbacks on the server, and you use GetPostBackEventReference to get the JavaScript that will trigger those postbacks.