Monday, March 26, 2012

Proper time to add UpdatePanel trigger

What is the proper time to add a trigger to an UpdatePanel from code-behind?

I currently have a control that acts as a NamingContainer. I am trying to have it register a couple of its buttons as async triggers if it senses that it is a child of an UpdatePanel. This is because these controls are databound (there are multiples of them in one UpdatePanel) so I cannot add the IDs of the controls declaratively.

I tried adding them in the PreRender phase of the control, but when I try it out I get a nasty exception on Render during a postback (below). I can only assume (from reflection) that initially, the triggers are being created when the controls are recreated from ViewState, then the controls those triggers reference get tossed aside when the list databinds after the change, so there's a null reference when the triggers from the first set try to find their real controls. However, I don't have any idea how to avoid this. Any help would be greatly appreciated.

Object reference not set to an instance of an object. 0.273490066474929 0.000559
at System.Web.UI.AsyncPostBackTrigger.HasTriggered()
at System.Web.UI.UpdatePanelTriggerCollection.HasTriggered()
at System.Web.UI.UpdatePanel.get_RequiresUpdate()
at System.Web.UI.PageRequestManager.ProcessUpdatePanels()
at System.Web.UI.PageRequestManager.RenderPageCallback(HtmlTextWriter writer, Control pageControl)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.Page.Render(HtmlTextWriter writer)
at IDM.Manage.UI.Page.Render(HtmlTextWriter writer) in C:\Projects\IDM.Manage\IDM.Manage\UI\Page.cs:line 39

I had this same problem when trying to add a trigger in the Page_Load.

It worked fine when I moved it to the Page_Init

No comments:

Post a Comment