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.
-
1You have a placeholder, on click, you add new control button on that placeholder.Aristos– Aristos2013-03-02 11:17:46 +00:00Commented Mar 2, 2013 at 11:17
-
1You can go through this link or shahed-kazi.blogspot.in/2009/07/…शेखर– शेखर2013-03-02 11:19:47 +00:00Commented Mar 2, 2013 at 11:19
-
please explain your ans.Arwinder Singh– Arwinder Singh2013-03-02 11:21:11 +00:00Commented Mar 2, 2013 at 11:21
Add a comment
|
2 Answers
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 :)
7 Comments
Arwinder Singh
where i should use this code in .aspx.cs file
Avishek
@user2106115 : you can write it in your existing button's click event
Arwinder Singh
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.
Avishek
@user2106115 : show some efforts from your end too..
Eamon Nerbonne
@user2106115: you should upvote useful answers, and mark the answer that best solves your question as the accepted answer.
|
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?