ClientScriptManager.GetPostBackEventReference Method

Brian Liang picture Brian Liang · Nov 5, 2008 · Viewed 11.9k times · Source

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.

Thanks

Answer

stevemegson picture stevemegson · Nov 6, 2008

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

href="javascript:__doPostBack('ctl00$LinkButton1','')"

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.