具有事件处理程序的 asp.net 动态按钮
我在 ASP.NET 中动态生成的按钮及其事件处理程序遇到了一个小问题。我为特殊用户生成了一个带有附加按钮的灵活表格。这些按钮将动态生成,效果很好。但我无法让事件处理程序工作。
以下是我的代码中的一些片段: 构建按钮(在自己的函数中)。
…
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);
…
我
void test(object sender, EventArgs e)
{
// Do some stuff
}
的 Page_Load
是空的。
但是,如果我单击按钮,程序将不会跳转到测试。出了什么问题?
编辑!!! 问题是我一开始不知道从 sql 查询中得到了多少行。对于每一行,我都会添加一个删除和更改按钮。 我在程序中调用一个方法,将结果构建为表格。 在此方法中,我检查当前用户是否是 AdminUser,如果是,我将调用 buildAdminButtons 函数。 在这里,我在新列中为每一行创建按钮。我怎样才能在 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);
}
我为每个按钮添加了一个唯一的 ID,但我一开始并不知道。我该如何处理这个问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将该代码放置在
page_load
或page_init
事件中。阅读 MSDN 文章 - 如何:以编程方式向 ASP.NET 网页添加控件?< /a>
You must have to place that code in
page_load
orpage_init
event.Read MSDN article - How to: Add Controls to an ASP.NET Web Page Programmatically?