Pages in ASP.NET Web Forms frequently have to dynamically display various user controls (.ascx) in response to a condition (such as a query string parameter or menu selection). Using LoadControl and a switch-case structure, you may load each control dynamically at runtime rather than generating distinct pages for each control.

Scenario
Suppose you have multiple user controls:

  • Dashboard.ascx
  • NewClient.ascx
  • AddNewClient.ascx
  • masterList.ascx
  • …and many others

You want to load these controls into a placeholder ( ControlHolder ) in your Default.aspx page based on a numeric or string parameter.

Implementation Steps

1. Add a Placeholder in ASPX Page
In your ASPX page ( Default.aspx ), add a PlaceHolder control where dynamic controls will be loaded:
<asp:PlaceHolder ID="ControlHolder" runat="server"></asp:PlaceHolder>

This placeholder acts as a container for your user controls.

2. Load Controls Dynamically in Code-Behind
In your code-behind ( Default.aspx.cs ), you can use a switch-case statement to determine which .ascx control to load:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string pageID = Request.QueryString["PageID"] != null ? Request.QueryString["PageID"].ToString() : ""; // Example: get value from URL ?PageID=1
        LoadUserControl(pageID);
    }
}


private void LoadUserControl(string pageID)
{
    switch (pageID)
    {
        case "1":
            Dashboard dashboared_ctrl = (Dashboard)LoadControl("Dashboard.ascx");
            ControlHolder.Controls.Add(dashboared_ctrl);
            break;

        case "2":
            NewClientmaster newclient_ctrl = (NewClientmaster)LoadControl("NewClient.ascx");
            ControlHolder.Controls.Add(newclient_ctrl);
            break;

        case "3":
            AddNewClientList addnewclient = (AddNewClientList)LoadControl("AddNewClient.ascx");
            ControlHolder.Controls.Add(addnewclient);
            break;

        case "4":
            UCCmasterList uccmaster_ctrl = (UCCmasterList)LoadControl("masterList.ascx");
            ControlHolder.Controls.Add(uccmaster_ctrl);
            break;

        case "5":
            AddClientList addclient_ctrl = (AddClientList)LoadControl("AddClientList.ascx");
            ControlHolder.Controls.Add(addclient_ctrl);
            break;

        case "6":
            ViewIPOMaster viewipo_master = (ViewIPOMaster)LoadControl("ViewMaster.ascx");
            ControlHolder.Controls.Add(viewipo_master);
            break;

        case "7":
            AddIPOMaster addipomastr = (AddIPOMaster)LoadControl("AddMaster.ascx");
            ControlHolder.Controls.Add(addipomastr);
            break;

        case "8":
            AddIPO addipo = (AddIPO)LoadControl("AddIPO.ascx");
            ControlHolder.Controls.Add(addipo);
            break;

        case "9":
            UpdateExchangeIPO updateEx = (UpdateExchangeIPO)LoadControl("Update.ascx");
            ControlHolder.Controls.Add(updateEx);
            break;

        case "10":
            OrderList orderlist_ctrl = (OrderList)LoadControl("OrderList.ascx");
            ControlHolder.Controls.Add(orderlist_ctrl);
            break;

        case "11":
            UPIReport upireport_ctrl = (UPIReport)LoadControl("Report.ascx");
            ControlHolder.Controls.Add(upireport_ctrl);
            break;

        case "12":
            IPOSettingsOffline offlinesetting_ctrl = (IPOSettingsOffline)LoadControl("Settings.ascx");
            ControlHolder.Controls.Add(offlinesetting_ctrl);
            break;

        case "13":
            OfflineOrders offlinelist_ctrl = (OfflineOrders)LoadControl("Orders.ascx");
            ControlHolder.Controls.Add(offlinelist_ctrl);
            break;

        case "14":
            IPOAllotment ipoallotment_ctrl = (IPOAllotment)LoadControl("Allotment.ascx");
            ControlHolder.Controls.Add(ipoallotment_ctrl);
            break;

        case "15":
            ExchangePage ExchangePage_ctrl = (ExchangePage)LoadControl("ExchangePage.ascx");
            ControlHolder.Controls.Add(ExchangePage_ctrl);
            break;

        case "16":
            ViewIssue ViewIssue_ctrl = (ViewIssue)LoadControl("ViewIssue.ascx");
            ControlHolder.Controls.Add(ViewIssue_ctrl);
            break;

        case "17":
            ViewIPO ViewIPO_ctrl = (ViewIPO)LoadControl("ViewIPO.ascx");
            ControlHolder.Controls.Add(ViewIPO_ctrl);
            break;

        default:
            // Optionally show a default control or message
            ControlHolder.Controls.Add(new LiteralControl("<p>Select a valid option</p>"));
            break;
    }
}


3. How It Works

  • Get the Page Identifier: You can use QueryString , a DropDownList , or any parameter to determine which control to load.
  • Switch-Case Logic: Each case corresponds to a specific .ascx control. The control is loaded dynamically and added to the placeholder.
  • Type Casting: (Dashboard)LoadControl("Dashboard.ascx") casts the loaded control to the correct class so you can access its public properties or methods.
  • Adding to Placeholder: ControlHolder.Controls.Add(dashboared_ctrl) adds the control to the page dynamically at runtime.

4. Advantages of this Approach

  • Single ASPX page for multiple views: Reduces code duplication.
  • Dynamic content loading: Only loads the control needed based on user interaction.
  • Maintainability: Adding new controls is simple: just add a new case in the switch.
  • Encapsulation: Each user control handles its own UI and logic.

5. Optional Enhancements

  • Use QueryString or Session for persistence.
  • Default control: Show a welcome dashboard if no PageID is provided.
  • Error handling: Wrap LoadControl in try-catch to prevent runtime errors.
  • Refactor using Dictionary : For large numbers of controls, you can use a dictionary mapping instead of long switch-case.

Example URL to Load Controls
Default.aspx?PageID=1   -> Loads Dashboard.ascx
Default.aspx?PageID=2   -> Loads NewClientmaster.ascx
Default.aspx?PageID=10  -> Loads OrderList.ascx


Conclusion
An effective technique to dynamically load several user controls into a single ASPX page is to utilize switch-case with LoadControl in ASP.NET Web Forms. This makes your program modular and maintainable.

HostForLIFE ASP.NET Core 9.0 Hosting

European Best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.