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

查看:114
本文介绍了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.

下面是从我的code一些作品:
构建按钮(在自己的功能)。

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查询中有多少行取回。对于每一行,我将添加删除和更改按钮。
我在程序中调用它建立的结果作为表的方法。
在这种方法中,我检查当前用户是管理用户,如果他是,我会打电话给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?

推荐答案

您必须放置的的code在的Page_Load page_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网页编程

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

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