Programmatically Adding User Controls Inside An UpdatePanel

I’m having trouble dynamically adding controls inside an update panel with partial postbacks. I’ve read many articles on dynamic controls and I understand how to add and maintain them with postbacks but most of that information doesn’t apply and won’t work for partial postbacks. I can’t find any useful information about adding and maintaining them with UpdatePanels. I’d like to do this without creating a web service if it’s possible. Does anyone have any ideas or references to some helpful information?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This is, I think, one of the common pitfalls for programmers but isn’t actually that hard to get it right when you know what is going on (always remember your viewstate!).

the following piece of code explains how things can be done. It’s a simple page where a user can click on a menu which will trigger an action that will add a user control to the page inside the updatepanel.
(This code is borrowed from here, and has lots more of information concerning this topic)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SampleMenu1.aspx.cs" Inherits="SampleMenuPage1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head runat="server">
    <title>Sample Menu</title>
    <form id="form1" runat="server">
        <asp:Menu ID="Menu1" runat="server" OnMenuItemClick="Menu1_MenuItemClick">
                <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>
        <br />
        <br />
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
                <asp:AsyncPostBackTrigger ControlID="Menu1" />

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class PlainSampleMenuPage : System.Web.UI.Page
    private const string BASE_PATH = "~/DynamicControlLoading/";

    private string LastLoadedControl
            return ViewState["LastLoaded"] as string;
            ViewState["LastLoaded"] = value;

    private void LoadUserControl()
        string controlPath = LastLoadedControl;

        if (!string.IsNullOrEmpty(controlPath))
            UserControl uc = (UserControl)LoadControl(controlPath);

    protected void Page_Load(object sender, EventArgs e)

    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";
            case "Load Control3":
                controlPath = BASE_PATH + "SampleControl3.ascx";
                controlPath = BASE_PATH + "SampleControl1.ascx";

        LastLoadedControl = controlPath;

for the code behind.

That’s basically it. You can clearly see that the viewstate is being kept with LastLoadedControl while the controls themselves are dynamically added to the page (inside the updatePanel (actually inside the placeHolder inside the updatePanel) when the user clicks on a menu item, which will send an asynchronous postback to the server.

More information can also be found here:

and of course on the website that holds the example code I used here.

Method 2

I encountered the problem that using the method mentioned above, LoadUserControl() is called twice when handling an event. I’ve read through some other articles and would like to show you my modification:

1) Use LoadViewstate instead of Page_Load to load the user control:

protected override void LoadViewState(object savedState)

    if (!string.IsNullOrEmpty(CurrentUserControl))
        LoadDataTypeEditorControl(CurrentUserControl, panelFVE);

2) Don’t forget to set the control id when loading the usercontrol:
private void LoadDataTypeEditorControl(string userControlName, Control containerControl)
    using (UserControl myControl = (UserControl) LoadControl(userControlName))

        string userControlID = userControlName.Split('.')[0];
        myControl.ID = userControlID.Replace("/", "").Replace("~", "");
    this.CurrentUserControl = userControlName;

Method 3

Try this:

Literal literal = new Literal();
literal.Text = "<script type='text/javascript' src=''>";

You can replace the content of literal with any HTML content you want…

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x