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!?

5
  • 1
    "But I dont want to do that!!" -- Unfortunately you sometimes have to do things you don't want to. Commented Jun 26, 2011 at 11:13
  • Do you really know how ASP.NET works? Commented Jun 26, 2011 at 11:16
  • I do.. it goes back with every click button and does fires pageload then the page events.. Problem is that i think i need to regenerate the buttons at the page_load event..and if i dont! the programmatically generated buton dissappears. Is there a way not to generate the button at the page_load event and still get the button appearing? Commented Jun 26, 2011 at 11:18
  • No. You'll have to manually create the button on every request. That's how Web Forms work. Either rewrite your code to do so, or switch to a less convoluted and confusing framework, such as ASP.NET MVC... Commented Jun 26, 2011 at 11:23
  • I used something very convoluted. My idea is to regenerate all the controls.. and control their placement with a bool property (that will be with ViewState inside it). When the button event fires it will set the property to false.. this will lead to the execution of everything including the button the button that i will regenerate at page load (i will add the code), but the 5 buttons will not be placed ... ie. if (DontPlaceControlsToPlaceHolder){ placeHolder.Controls.Add(newBtn);}.. What do you think.. But I still have some overhead cause of regenerating everything..Dont i? Commented Jun 26, 2011 at 11:29

1 Answer 1

1

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Couldnt you use bool instead of stuffing controls into session ... bool property like this: public bool ListAllPosts { get{return ViewState["ListAllTopics"]==null?false:(bool)ViewState["ListAllTopics"];} set { ViewState["ListAllTopics"]=value; } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.