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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:29:52+00:00 2026-05-23T10:29:52+00:00

public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {

  • 0
public partial class Default2 : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        GenerateButtons generate = new GenerateButtons();
        generate.Generate5Controls(PlaceHolder1);

    }


}


class GenerateButtons
{
    PlaceHolder placeHolder;
   public  void Generate5Controls(PlaceHolder placeH)
    {
        placeHolder = placeH;
        for (int i = 0; i < 5; i++)
        {
            Button newBtn = new Button();
            newBtn.Click += btn_Click;
            newBtn.Text = "PageLoadButton Created. Number: "+i;
            placeHolder.Controls.Add(newBtn);
        }
    }
    public void btn_Click(object sender, EventArgs e)
    {
        Button newBTN = new Button();
        newBTN.Text = "A New Button was added by the button event btn_click";
        newBTN.Click += btn2_Click;
        placeHolder.Controls.Add(newBTN);

    }
    public void btn2_Click(object sender, EventArgs e)
    {
        Button newBTN = new Button();
        newBTN.Text = "A New Button was added by the button event btn2_click";
        placeHolder.Controls.Add(newBTN);
    }
}

I want the events btn_click & btn2_click to fire every post back.. When i click the button that was programmatically created it disappears after each postback and its event doesnt fire (btn2_click). I know i could generate the button at the postback.. But I dont want to do that!! I want to know how I could update the state of the placeholder… so that the only button will appear and the 5 buttons generated in Generate5Controls(PlaceHolder placeH) to disappear.

I could use a bool Viewstate to prevent this generate.Generate5Controls(PlaceHolder1); from being execute..

But the question is how do I make the programmatically generated button to appear!?

  • 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-23T10:29:53+00:00Added an answer on May 23, 2026 at 10:29 am

    You should generate controls on every PostBack or you can generate controls once, save in session and add generated controls from session on page_load event.

    protected void Page_Load(object sender, EventArgs e)
        {
           if(Session["GeneratedButtons"] == null)
           {
              GenerateButtons generate = new GenerateButtons();
              generate.Generate5Controls(PlaceHolder1);
           }
           else
           {
               List<Control> generatedControls = Session["GeneratedButtons"] as List<Control>;
               foreach(Control oneControl in generatedControls)
               {
                   PlaceHolder1.Controls.Add(oneControl);
               }
           }
        }
    
    class GenerateButtons
    {
        PlaceHolder placeHolder;
       public  void Generate5Controls(PlaceHolder placeH)
        {
            placeHolder = placeH;
            List<Control> generatedControls = new List<Control>();
            for (int i = 0; i < 5; i++)
            {
                Button newBtn = new Button();
                newBtn.Click += btn_Click;
                newBtn.Text = "PageLoadButton Created. Number: "+i;
                placeHolder.Controls.Add(newBtn);
                AddControlToSession(newBtn);
            }
        }
        public void btn_Click(object sender, EventArgs e)
        {
            Button newBTN = new Button();
            newBTN.Text = "A New Button was added by the button event btn_click";
            newBTN.Click += btn2_Click;
            placeHolder.Controls.Add(newBTN);
            AddControlToSession(newBtn);
        }
    
        public void btn2_Click(object sender, EventArgs e)
        {
            Button newBTN = new Button();
            newBTN.Text = "A New Button was added by the button event btn2_click";
            placeHolder.Controls.Add(newBTN);
            AddControlToSession(newBtn);
        }
    
        private void AddControlToSession(Control ctrl)
        {
            List<Control> generatedControls = Session["GeneratedButtons"] as List<Control>;
            if(generatedControls == null)
            {
                generatedControls = new List<Control>();
            }
            generatedControls.Add(ctrl);
            Session["GeneratedButtons"] = generatedControls;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following master.cs code: public partial class Default : System.Web.UI.MasterPage { protected void
I have the following code: public partial class queryTerm : System.Web.UI.UserControl { private static
Given this class public partial class Default : Page { private IRepository repo; ...
I have a page that is setup like this public partial class _Default :
Here is my master page code behind: namespace mysite.MasterPages { public partial class Main
Please take a look at this: protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { int
I have a web project like: namespace Web { public partial class _Default :
Consider this source: public partial class FormTest : Form { private Test test {
say I have this control: public partial class bloc999 : UserControl { bloc999Data mainBlock
You should be able to create a generic form: public partial class MyGenericForm<T> :

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.