如何在Web表单中创建动态按钮 [英] how to create a dynamic button in a web form
本文介绍了如何在Web表单中创建动态按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家上午好,
我在c#.net网络应用程序中创建一个益智游戏24按钮(5 * 5)矩阵。那么如何创建动态按钮以及如何在c#.net中创建动态点击事件。
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....
推荐答案
下面的示例代码将在运行时添加一个按钮网页。修改它以满足您的要求。
Below sample code will add a button on runtime in you web page. Modify it to meet your requirements.
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:
protected void bt_click(object sender, EventArgs e)
{
Button btn = sender as Button;
//Add your functionality here
}
在您的页面添加面板..
Add panel to your page..
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");
}
}
protected void Page_Load(object sender,EventArgs e)
{
表tbl =新表();
phl.Controls.Add(tbl);
数组array = new Array [25];
随机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);
}
}
}
}
}
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);
}
}
}
}
}
这篇关于如何在Web表单中创建动态按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文