Thursday, 21 April 2011

Ajax UpdatePanel: with Placeholder control

Helpful Website

1. Create a menu.
2. Depending on the selected menu load the specific control - code should be placed in c# file
3. In the update panel put trigger control id = menu id
4.You must add a ScriptManager control to the page


Menu code:
<asp:MenuItem Text="File">
   <asp:MenuItem Text="Load Control1"></asp:MenuItem>
   <asp:MenuItem Text="Load Control2"></asp:MenuItem>
   <asp:MenuItem Text="Load Control3"></asp:MenuItem>
</asp:MenuItem>

Menu item click event code:

*protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
        MenuItem menu = e.Item;

        string controlPath = string.Empty;

        switch (menu.Text)
        {
            case "Load Control2":
                controlPath = BASE_PATH + "SampleControl2.ascx";
                break;
            case "Load Control3":
                controlPath = BASE_PATH + "SampleControl3.ascx";
                break;
            default:
                controlPath = BASE_PATH + "SampleControl1.ascx";
                break;
        }

        LastLoadedControl = controlPath;
        LoadUserControl();
    }


*private void LoadUserControl()
    {
        string controlPath = LastLoadedControl;

        if (!string.IsNullOrEmpty(controlPath))
        {
            PlaceHolder1.Controls.Clear();
            UserControl uc = (UserControl)LoadControl(controlPath);
            PlaceHolder1.Controls.Add(uc);
        }
    }

Ajax Update panel code:
*<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
         <ContentTemplate>
            <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
         </ContentTemplate>
         <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Menu1" />
          </Triggers>
            
        </asp:UpdatePanel>
Note: 
<contentTemplate>
                 "here put the menu1 item based on which trigger will occur."
</contentTemplate>

Troubleshooting:

Re: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server cou...

Mar 05, 2009 05:19 AM|LINK
 AsyncPostBackTrigger is incorrect.  It's actually required to be a PostBackTrigger.  This is because we *don't want a partial postback; we are redirecting to a new execution thread*. 
The idea is that on the linkbutton's PreRender event we're adding the NewScriptManager.RegisterPostBackControl(MyButton) -- there's no need to set it declaritively in the ASPX page, it won't find it anyway and will throw a runtime error because the LinkButton is inside a databound templated control. 

 PlaceHolder vs ContentPlaceHolder

Nov 01, 2007 12:46 PM|LINK
A PlaceHolder control is used as a container for any controls you wish to add and can be used on any page. A ContentPlaceHolder is used to hold content from Content Pages and can only be used on a Master Page.

No comments:

Post a Comment