Archive for October, 2009

PostHeaderIcon ASP.Net UpdatePanel Triggers

Any control residing inside an updatepanel automatically causes a partial refresh and updates the update panel. However, sometimes you want to trigger an update on an update panel from a control that is outside of the control panel and that is where triggers come in.


<asp:UpdatePanel ID="MyUpdatePanel" UpdateMode="Conditional" runat="server">
  <Triggers>
    <asp:AsyncPostBackTrigger ControlID="MyButton" EventName="OnClick" /> 
  </Triggers>
  <ContentTemplate>
  </ContentTemplate>
</asp:UpdatePanel>

This works fine a dandy however if you are in some sort of template e.g. a create user wizard template you are bound to get the following error
A control with ID ‘MyButton’ could not be found for the trigger in UpdatePanel ‘MyUpdatePanel’.

My first approach was to use the ScriptManager RegisterAsyncPostBackControl

Master.GetScriptManager.RegisterAsyncPostBackControl(MyButton);

Two Notes
<li>The script manager is accessed via a property on the master page
<li>The MyButton is referenced via a findcontrol call

This does not work since the postback is not tied to an update panel and causes a full postback. The solution is to register a trigger on the update panel from the code behind page like so


UpdatePanel MyUpdatePanel = ControlFinder.FindChildControl<UpdatePanel>(Page, "MyUpdatePanel");
AsyncPostBackTrigger MyUpdatePanelTrigger = new AsyncPostBackTrigger();
MyUpdatePanelTrigger.ControlID = MyButton.UniqueID;
MyUpdatePanelTrigger.EventName = "Click";
MyUpdatePanel.Triggers.Add(MyUpdatePanelTrigger);