0

I have a button and now i want that when i click this button and then this click creates one more button on the same page. Please tell me in detail how it should be done? Thanks in advance.

3
  • 1
    You have a placeholder, on click, you add new control button on that placeholder. Commented Mar 2, 2013 at 11:17
  • 1
    You can go through this link or shahed-kazi.blogspot.in/2009/07/… Commented Mar 2, 2013 at 11:19
  • please explain your ans. Commented Mar 2, 2013 at 11:21

2 Answers 2

2

This is how to do it.

in aspx

<asp:Button ID="btnMain" Text="Create New Button" runat="server" 
        onclick="btnMain_Click" />

in aspx.cs

private static int count=0;

protected void btnMain_Click(object sender, EventArgs e)
{
    Button btnNew = new Button();
    btnNew.ID = "btnNew_" + count;
    btnNew.Text = "New Button_" + count;
    Form.Controls.Add(btnNew);
    count++;
}

you can set any container control id in place of this to add the button inside that container. You can also set position, height, width etc of the button here. Hope it helps :)

Sign up to request clarification or add additional context in comments.

7 Comments

where i should use this code in .aspx.cs file
@user2106115 : you can write it in your existing button's click event
thanks buddy...it solved my one problem. Now i want to persist the buttons means when i click "btnMain" then "New Button" is created but i want when again i click "btnMain" then one more button is created.
@user2106115 : show some efforts from your end too..
@user2106115: you should upvote useful answers, and mark the answer that best solves your question as the accepted answer.
|
1

You will have to use page_load or page_init event.

protected void Page_Load()// it can be any event button click also
{
  Button ButtonChange = new Button();

  ButtonChange.Text = "Change";
  ButtonChange.ID = "change_" + i.ToString();
  ButtonChange.Font.Size = FontUnit.Point(7);
  ButtonChange.ControlStyle.CssClass = "button";
  ButtonChange.Click += new EventHandler(test);
}

Read MSDN article - How to: Add Controls to an ASP.NET Web Page Programmatically?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.