Mental Jetsam

By Peter Finch

Using a Microsoft Atlas UpdatePanel on Master Pages with multiple ContentPlaceHolders

Posted by pcfinch on April 4, 2007

ASP.NET Atlas Master Page StructureThis scenario is pretty straight forward. You have a MasterPage with 2 ContentPlaceHolders, one for the menu on the left and one for the content on the right (A simple label in this case). When you select a menu item on the left you want to update the content page on the right using AJAX.

Normally, all you have to do to accomplish this is to add an Atlas UpdatePanel.

1. Add the ScriptManager to the MasterPage.

<asp:scriptmanager id="scriptmanager1" runat="server"></asp:scriptmanager>

2. Add the UpdatePanel to the corresponding Content section of the Content page

3. Add the controls to the UpdatePanel that you want to update using AJAX.

4. Add a Trigger to the UpdatePanal that references the control (e.g. Menu1) and the event (e.g. MenuItemClick). This can be done manually in the HTML source view or from the Triggers field, in the Behavior properties section.

<asp:AsyncPostBackTrigger ControlID="Menu1" EventName="MenuItemClick"></asp:AsyncPostBackTrigger>

Normally, with Atlas, this is all you have to do, however, when using Master Pages you will get an error on the page something like this.

A control with ID 'Menu1' could not be found for the trigger in UpdatePanel 'UpdatePanel1'.]

The reason for this message is that Controls in Content pages of Master pages do not actually have their original ID’s. For example, although Menu1’s ID is “Menu1” it’s actual UniqueID is something like “ctl00$Menu$Menu1”. The real problem is that you do not know this until runtime. You could add a AsyncPostBackTrigger() manually to the UpdatePanel.Triggers collection at runtime in the OnInit() function using static strings, but this is not very maintainable.

The following method simply looks through the triggers on the UpdatePanel and for the one that ends with the ID of the Menu1 control it simply changes the trigger ControlID to the UniqueID of Menu1 as runtime. This MUST happen in the OnInit() method of the Content Page. Although you have to hardcode the Menu1 item (and any other trigger controls), it means that you can change the Menu’s ID or even the EventName without having to touch the code.

protected override void OnInit(EventArgs e)
{
   base.OnInit(e);
   foreach (AsyncPostBackTrigger apbt in UpdatePanel1.Triggers) {
      if (Menu1.UniqueID.EndsWith(apbt.ControlID))
         apbt.ControlID = Menu1.UniqueID;
   }
}

10 Responses to “Using a Microsoft Atlas UpdatePanel on Master Pages with multiple ContentPlaceHolders”

  1. Mrali said

    Hello,
    How to prevent the master page from being refreshed when calling different content pages ?

  2. dave said

    Hi There,
    One quick think to check is the xhtml tag in the webconfig file. If it’s “legacy” then change it back to “transitional”.

    i have the same scenerio, but for some reason when i click on the menu item to updatethe content page it doesn’t do it. If you have a sample it would be realy helpful.

    Thanks
    Dave

  3. Jason said

    Great example! worked wonders with the issues I have had trying to update content in other ContentPlaceHolder panels using AJAX update panels

  4. jswanson said

    Nice example! This issue was driving me nuts. Thanks!

  5. Michael said

    After hours of wading through pants examples of convoluted ways to try and fix this problem this article was a complete breath of fresh air. It explained first what the problem is and why concisely and then how to fix it with minimal code. A+ Thanks pcfinch

    Oh and in case its any use to people, I converted mine to VB for my purposes, just replace imgbSearch with your control:

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit

    Dim apbt As AsyncPostBackTrigger
    For Each apbt In UpdatePanel1.Triggers
    If imgbSearch.UniqueID.EndsWith(apbt.ControlID) Then
    apbt.ControlID = imgbSearch.UniqueID
    End If
    Next
    End Sub

  6. […] Does anybody know how to handle the multiple content area trigger problem. I found this code at https://mentaljetsam.wordpress.com/2007/04/04/using-a-microsoft-atlas-updatepanel-on-master-pages-wit…. Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles […]

  7. compugab said

    Yes it works.

    Thank!

  8. Thank you so much! This is exactly what I was looking for! 😀

  9. theBoss said

    Thank you very much – solved my issue perfectly

Leave a reply to Mrali Cancel reply