Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hello good morning,
i m creating a puzzle game 24 button(5*5) matrix in c# .net web application. so how to create dynamic button and how to create a dynamic click event in c# .net....
Posted

Below sample code will add a button on runtime in you web page. Modify it to meet your requirements.

C#
private void AddButton(int id)
{
//Initialize button and set its properties (especially ID)
Button bt = new Button();
bt.ID = "btn" + id.ToString();
bt.Text = "Text"+id.ToString();
//Attach click event
bt.Click += new EventHandler(bt_click);
//Change below line (this) with the container in which you want your button to appear.
this.Controls.Add(bt);
}


Click event handler will be:
C#
protected void bt_click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    //Add your functionality here
}
 
Share this answer
 
Comments
Member 11754700 10-Jun-15 2:48am    
i am using this code it's work successfully but now next how to use for loop for a 1 to 24 button if a button is in 1 to 25 so swap other wise not swap .......
Member 11754700 10-Jun-15 2:48am    
thank you
Member 11754700 10-Jun-15 3:21am    
hello i am my solution it's work but still not swap button. so what can i do....?
Abhipal Singh 10-Jun-15 4:04am    
I did not get you. What do you exactly mean by swaping? and swap button with what? can you explain the requirement a bit more clearly.
Member 11754700 10-Jun-15 6:04am    
i am create a puzzle game 24 button swap random button one location to other location (ex. i m select button one is swap with button 2) so now i m create a dynamic button and also create dynamic click event but button was not swap so what can i do ...
Add panel to your page..


C#
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Table tb = new Table();

        for (int i = 0; i < 5; i++)
        {
            TableRow tr = new TableRow();

            for (int j = 0; j < 5; j++)
            {
                TableCell tc = new TableCell();
                Button bt = new Button();
                bt.Height = 10;
                bt.Width = 10;
                tc.Controls.Add(bt);
                bt.Click += bt_Click;
                tr.Cells.Add(tc);

            }

            tb.Rows.Add(tr);
        }

      Panel1  .Controls.Add(tb);
    }

    void bt_Click(object sender, EventArgs e)
    {
        Response.Write("hello");
    }
}
 
Share this answer
 
protected void Page_Load(object sender, EventArgs e)
{

Table tbl = new Table();
phl.Controls.Add(tbl);
Array array = new Array[25];
Random rndm = new Random();
int n = rndm.Next();
string[] a = new string[25];


for (int i = 0; i < 5; i++)
{

TableRow tbr = new TableRow();
for (int j = 0; j < 5; j++)
{
TableCell tbcl = new TableCell();
Button bt = new Button();
ListItem ls = new ListItem();
ls.Text.Contains(ls.Text);
bt.Text = ls.Text;
ListBox lb = new ListBox();
lb.Items.Add(ls);
ls.Text = rndm.Next(1, 25).ToString();

tbcl.Controls.Add(bt);

tbr.Cells.Add(tbcl);

}


}
}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900