带有事件处理程序的 asp.net 动态按钮 [英] asp.net dynamically button with event handler

查看:32
本文介绍了带有事件处理程序的 asp.net 动态按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个动态生成的按钮及其在 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);
…

还有

void test(object sender, EventArgs e)
{ 
   // Do some stuff       
}

我的Page_Load 是空的.

但是如果我点击按钮,程序不会跳转到测试.怎么了?

But the program won’t jump to test, if I click the button. What’s going wrong?

编辑!!!问题是我一开始不知道从我的 sql 查询中得到了多少行.对于每一行,我将添加一个删除和一个更改按钮.我在我的程序中调用了一个将结果构建为表格的方法.在这个方法中,我检查当前用户是否是 AdminUser,如果是,我将调用 buildAdminButtons 函数.在这里,我在新列中为每一行创建按钮.我怎么能在 OnLoad 中得到这个?

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);
}

我为每个按钮添加了一个唯一的 ID,一开始我不知道.我该如何处理?

I add to every button a unique id, which I don't know at the start. How could I handle this?

推荐答案

您必须将那个代码放在 page_loadpage_init 事件中.

You must have to place that code in page_load or page_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);
}

阅读 MSDN 文章 - 如何:向 ASP.NET 网页添加控件以编程方式?

Read MSDN article - How to: Add Controls to an ASP.NET Web Page Programmatically?

这篇关于带有事件处理程序的 asp.net 动态按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆