具有事件处理程序的 asp.net 动态按钮

发布于 2024-12-09 04:41:07 字数 1594 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不在是我 2024-12-16 04:41:07

您必须将代码放置在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);
}

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文