asp.net动态按钮与事件处理程序 [英] asp.net dynamically button with event handler
问题描述
这里有一些代码:
构建按钮(在自己的函数中) / p>
...
按钮ButtonChange = new Button();
ButtonChange.Text =更改;
ButtonChange.ID =change_+ i.ToString();
ButtonChange.Font.Size = FontUnit.Point(7);
ButtonChange.ControlStyle.CssClass =button;
ButtonChange.Click + = new EventHandler(test);
...
和
void test(object sender,EventArgs e)
pre>
{
//做一些东西
}
我的
Page_Load
为空。
但是程序不会跳转到测试中,如果我点击按钮。发生什么事?
编辑!!!
问题是,我不知道在开始有多少行我得到从我的sql查询回来。对于每一行,我将添加一个删除和更改按钮。
我在程序中调用一个方法,它将结果构建为一个表。
在这个方法中,我检查当前用户是否是AdminUser,如果他是,我将调用buildAdminButtons函数。
这里,我为每一行创建一个新列中的按钮。如何在OnLoad中获得这个?private void buildAdminButtons(TableRow tempRow,int i)
{
按钮ButtonDelete = new Button();
按钮ButtonChange = new Button();
TableCell change = new TableCell();
TableCell delete = new TableCell();
ButtonChange.Text =更改;
ButtonChange.ID =change_+ i.ToString();
ButtonChange.Font.Size = FontUnit.Point(7);
ButtonChange.ControlStyle.CssClass =button;
ButtonDelete.Text =删除;
ButtonDelete.ID =delete_+ i.ToString();
ButtonDelete.Font.Size = FontUnit.Point(7);
ButtonDelete.ControlStyle.CssClass =button;
change.Controls.Add(ButtonChange);
delete.Controls.Add(ButtonDelete);
tempRow.Cells.Add(更改);
tempRow.Cells.Add(delete);
}
我添加到每个按钮一个唯一的ID,我不知道开始。如何处理?
解决方案您必须将 c> page_load 或
page_init
事件。protected void Page_Load()
{
Button ButtonChange = new Button();
ButtonChange.Text =更改;
ButtonChange.ID =change_+ i.ToString();
ButtonChange.Font.Size = FontUnit.Point(7);
ButtonChange.ControlStyle.CssClass =button;
ButtonChange.Click + = new EventHandler(test);
}
阅读MSDN文章 - 如何以编程方式将控件添加到ASP.NET网页?
I have here a small problem with a dynamically generated buttons and their event handler in asp.net. I generate a flexible table with additional Buttons for special users. The buttons will generate dynamically, which works fine. But I can’t get the event handler to work.
Here are some pieces from my code: Build the button (In an own function).
… 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); …
And
void test(object sender, EventArgs e) { // Do some stuff }
My
Page_Load
is empty.But the program won’t jump to test, if I click the button. What’s going wrong?
Edit!!! The problem is that I don’t know at start how many rows I get from my sql query back. For every row I will add a delete and a change button. I call in my program a method which builds the result as a table. In this method, I check if the current user is an AdminUser and if he is, I will call buildAdminButtons function. Here, I create the buttons in a new column, for every row. How could I get this in OnLoad?
private void buildAdminButtons(TableRow tempRow, int i) { Button ButtonDelete = new Button(); Button ButtonChange = new Button(); TableCell change = new TableCell(); TableCell delete = new TableCell(); ButtonChange.Text = "Change"; ButtonChange.ID = "change_" + i.ToString(); ButtonChange.Font.Size = FontUnit.Point(7); ButtonChange.ControlStyle.CssClass = "button"; ButtonDelete.Text = "Delete"; ButtonDelete.ID = "delete_" + i.ToString(); ButtonDelete.Font.Size = FontUnit.Point(7); ButtonDelete.ControlStyle.CssClass = "button"; change.Controls.Add(ButtonChange); delete.Controls.Add(ButtonDelete); tempRow.Cells.Add(change); tempRow.Cells.Add(delete); }
I add to every button a unique id, which I don't know at the start. How could I handle this?
解决方案You must have to place that code in
page_load
orpage_init
event.protected void Page_Load() { 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?
这篇关于asp.net动态按钮与事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!