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 3319926
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:52:55+00:00 2026-05-17T22:52:55+00:00

I want to create control that will allow user to design his own website

  • 0

I want to create control that will allow user to design his own website structure. I imagined that inside UpdatePanel is control(s) with TextBox ( for page name) and Button ‘add below’. It would looks like:

|   "Questions" |
| [ add below ] |

|     "Tags"    |
| [ add below ] |

|    "Badges"   |
| [ add below ] |

now when user click on button in “Tags” element there should appear new one, between “Tags” and “Badges”, with editable name, so user can name it “Users” for example. It should be done without full postback ( to avoid page blinks).

Now is my problem: I cannot load those controls (at last not all of them) in onInit since they dont exist, but I have to attend to theirs click event so I should attaching event listener what should be done during Init phase. How can I achieve described functionality?

I played around it for too much time and I am confused. I would be grateful for any tips.

  • 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. Editorial Team
    Editorial Team
    2026-05-17T22:52:56+00:00Added an answer on May 17, 2026 at 10:52 pm

    This is a tricky situation because you are dealing with dynamic controls you need to populate them on page init in order to persist viewstate, also events for controls added inside an update panel during button clicks do not seems to get registered until the next postback, so the normal button click event only fires once every other time you click on a newly added control. There may be some other way to work around this than the way I did it. If anyone knows I would like find out.

    My solution is to keep a list of what you have added dynamically, and store it in a session variable (because the viewstate is not loaded during page init). Then on the page init load any controls you have previously added. Then handle the click event during the page load with some custom code instead or the normal click event.

    I have created a sample page to help test this.

    Here is the code for the aspx page (default.aspx):

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!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 id="Head1" runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <p>This only updates on full postbacks: <%= DateTime.Now.ToLongTimeString() %></p>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:PlaceHolder ID="PlaceholderControls" runat="server"></asp:PlaceHolder>
            </ContentTemplate>
        </asp:UpdatePanel>
        </form>
    </body>
    </html>
    

    And here is the code for the code behind page (default.aspx.cs):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class _Default : System.Web.UI.Page
    {
        /// <summary>
        /// this is a list for storing the id's of dynamic controls
        /// I have to put this in the session because the viewstate is not
        /// loaded in the page init where we need to use it
        /// </summary>
        public List<string> DynamicControls
        {
            get
            {
                return (List<string>)Session["DynamicControls"];
            }
            set
            {
                Session["DynamicControls"] = value;
            }
        }
    
        protected void Page_Init(object sender, EventArgs e)
        {
            PlaceholderControls.Controls.Clear();
    
            //add one button to top that will cause a full postback to test persisting values--
            Button btnFullPostback = new Button();
            btnFullPostback.Text = "Cause Full Postback";
            btnFullPostback.ID = "btnFullPostback";
            PlaceholderControls.Controls.Add(btnFullPostback);
    
            PlaceholderControls.Controls.Add(new LiteralControl("<br />"));
    
            PostBackTrigger FullPostbackTrigger = new PostBackTrigger();
            FullPostbackTrigger.ControlID = btnFullPostback.ID;
    
            UpdatePanel1.Triggers.Add(FullPostbackTrigger);
            //-----------------------------------------------------------------------
    
    
    
    
            if (!IsPostBack)
            {
                //add the very first control           
                DynamicControls = new List<string>();
    
                //the DynamicControls list will persist because it is in the session
                //the viewstate is not loaded yet
                DynamicControls.Add(AddControls(NextControl));
    
            }
            else
            {
                //we have to reload all the previously loaded controls so they
                //will have been added to the page before the viewstate loads
                //so their values will be persisted
                for (int i = 0; i < DynamicControls.Count; i++)
                {
                    AddControls(i);
                }
            }
    
    
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!IsPostBack)
            {
                //we have to increment the initial
                //control count here here be cause we cannot persit data in the viewstate during 
                //page init
    
                NextControl++;
    
            }
            else
            {
                HandleAddNextClick();           
    
            }
    
        }
    
        /// <summary>
        /// this function looks to see if the control which caused the postback was one of our 
        /// dynamically added buttons, we have to do this because the update panel seems to interefere
        /// with the event handler registration.
        /// </summary>
        private void HandleAddNextClick()
        {
            //did any of our dynamic controls cause the postback if so then handle the event
            if (Request.Form.AllKeys.Any(key => DynamicControls.Contains(key)))
            {
                DynamicControls.Add(AddControls(NextControl));
                NextControl++;
            }
        }
    
    
    
        protected void btnAddNext_Command(object sender, CommandEventArgs e)
        {
            //this is intentionally left blank we are handling the click in the page load
            //because the event for controls added dynamically in the click does
            //not get registered until after a postback, so we have to handle it 
            //manually.  I think this has something to do with the update panel, as it works 
            //when not using an update panel, there may be some other workaround I am not aware of
    
        }
    
        /// <summary>
        /// variable for holding the number of the next control to be added
        /// </summary>
        public int NextControl
        {
            get
            {
                return  ViewState["NextControl"] == null ? 0 : (int)ViewState["NextControl"];
            }
            set
            {
                ViewState["NextControl"] = value;
            }
        }
    
    
        /// <summary>
        /// this function dynamically adds a text box, and a button to the placeholder
        /// it returns the UniqueID of the button, which is later used to find out if the button
        /// triggered a postback
        /// </summary>
        /// <param name="ControlNumber"></param>
        /// <returns></returns>
        private string AddControls(int ControlNumber)
        {
            //add textbox
            TextBox txtValue = new TextBox();
            txtValue.ID = "txtValue" + ControlNumber;
            PlaceholderControls.Controls.Add(txtValue);
    
            //add button
            Button btnAddNext = new Button();
            btnAddNext.Text = "Add Control " + ControlNumber;
            btnAddNext.ID = "btnAddNext" + ControlNumber;
            int NextControl = ControlNumber + 1;
            btnAddNext.CommandArgument = NextControl.ToString();
    
            btnAddNext.Command += new CommandEventHandler(btnAddNext_Command);
            PlaceholderControls.Controls.Add(btnAddNext);
    
            //add a line break
            PlaceholderControls.Controls.Add(new LiteralControl("<br />"));
    
            return btnAddNext.UniqueID;
    
        }    
    }
    

    Let me know if this code helps you out at all. I tried to add comments with my understanding of what is going on and how it works.

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

Sidebar

Related Questions

I want to create custom control (let's name it DesignPanel ) that will allow
I want to create a user control that will again contain a user control
I want to create a Silverlight 2 control that has two content areas. A
I have a user control and I want to create a property of type
I am trying to create a winForms user control. But I want would like
I am trying to create a wpf control that will display a map image
I need a dropdown list on my page that will allow a user to
I've created a registration form control for a Sitecore site that will create a
I'm creating a login page. I want to create ASP.NET TextBox controls that have
In Silverlight, when you want to create a control dynamically, you must add the

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.