Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 59901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:04:40+00:00 2026-05-10T18:04:40+00:00

I’m having trouble dynamically adding controls inside an update panel with partial postbacks. I’ve

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:04:41+00:00Added an answer on May 10, 2026 at 6:04 pm

    This is, I think, one of the common pitfalls for asp.net 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">     <title>Sample Menu</title> </head> <body>     <form id="form1" runat="server">         <asp:Menu ID="Menu1" runat="server" OnMenuItemClick="Menu1_MenuItemClick">             <Items>                 <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>             </Items>         </asp:Menu>         <br />         <br />         <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>         <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>     </form> </body> </html> 

    and

    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     {         get         {             return ViewState["LastLoaded"] as string;         }         set         {             ViewState["LastLoaded"] = value;         }     }      private void LoadUserControl()     {         string controlPath = LastLoadedControl;          if (!string.IsNullOrEmpty(controlPath))         {             PlaceHolder1.Controls.Clear();             UserControl uc = (UserControl)LoadControl(controlPath);             PlaceHolder1.Controls.Add(uc);         }     }      protected void Page_Load(object sender, EventArgs e)     {         LoadUserControl();     }      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();     } } 

    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:

    • http://aspnet.4guysfromrolla.com/articles/081402-1.aspx
    • http://aspnet.4guysfromrolla.com/articles/082102-1.aspx

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 161k
  • Answers 161k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It seems you have great expectations from a tiny little… May 12, 2026 at 11:54 am
  • Editorial Team
    Editorial Team added an answer In XPath 1.0: You can use /div//text()[not(parent::p)] to capture the… May 12, 2026 at 11:54 am
  • Editorial Team
    Editorial Team added an answer TSVN does not call the command line client. So there… May 12, 2026 at 11:54 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.