简体   繁体   English

asp.net动态按钮与事件处理程序

[英]asp.net dynamically button with event handler

I have here a small problem with a dynamically generated buttons and their event handler in asp.net. 我在这里有一个小问题,动态生成按钮及其在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. 我的Page_Load是空的。

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. 问题是我一开始不知道从sql查询中得到多少行。 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. 在这个方法中,我检查当前用户是否是AdminUser,如果是,我将调用buildAdminButtons函数。 Here, I create the buttons in a new column, for every row. 在这里,我为每一行创建一个新列中的按钮。 How could I get this in OnLoad? 我怎么能在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. 我在每个按钮上添加一个唯一的ID,我在开始时就不知道。 How could I handle this? 我怎么能处理这个?

You must have to place that code in page_load or page_init event. 您必须将该代码放在page_loadpage_init事件中。

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? 阅读MSDN文章 - 如何:以编程方式将控件添加到ASP.NET网页?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM