Thursday, July 2, 2009

UpdatePanel doesn't update

Once upon a time I had a master page with s number of Content placeholders. Also I had a page with an UpdatePanel in one Content and a GridView in another. What I wanted was the GridView row clicks to trigger UpdatePanels's refresh.

I tried OnSelectedIndexChanged (adding a LinkButton of Select command in the GW) with no success.

I tried to add another UpdatePanel2 as parent for the GridView with ChildrenAsTriggers=true, catch LinkButtons' OnClicks and update UpdatePanel 1 from there - the breakpoints didn't hit again.

The fast, dirty and easy solution was to replace those LinkButtons with and add following function (found somewhere in Internet) to its onclick:

  function forceAJAXPostback(eventTarget, eventArgument)
  {
  var prm = Sys.WebForms.PageRequestManager.getInstance(); 
  prm._doPostBack(eventTarget, eventArgument);
  }

Then in Page_Load I just parsed Request.Form["__EVENTTARGET"], and if it was what I passed (some constant), then Request.Form["__EVENTARGUMENT"] was used to update the UpdatePanel. UpdatePanel was set to conditional update.

Alternatively, you can pass a button id as eventTarget - it doesn't even have to be the UniqueId, add the button's Click to the UpdatePanel's triggers and do what you need in the button's Click handler. The button also doesn't have to be displayed.