为动态创建的按钮创建唯一的事件处理程序

时间:2013-09-18 21:34:20

标签: c# asp.net

我想动态地向我的页面添加按钮。它取决于SELECT语句的结果数。我

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            for (int i = 0; i < Query.length; i++)
            {
                 Button btn = new Button();
                 btn.ID = "Button" + i;
                 btn.Click += new EventHandler(btn_Click());
                 btn.Text = i.ToString();
                 pagingPanel.Controls.Add(btn);
           }
        }
    }

但我希望每个按钮都有自己的自定义事件处理程序。如果我单击一个按钮,我希望它与我单击另一个按钮的结果不同。我想做一些这样的事情,我可以传递一个aditional参数:

protected void btn_Click(object sender, EventArgs e, string test)
{
    System.Diagnostics.Debug.WriteLine(test);
}

也许我不知道要通过哪些对象?或许我接近这个错误的方式。

如何达到预期效果?

4 个答案:

答案 0 :(得分:2)

试试这个:

protected void Page_Load(object sender, EventArgs e)
{
        // you do not use !IsPostBack here
        //count of func must be equal with 'Query.Length'
        string[,] arr ={
                         {"func1","hello world"},
                         {"func2","Hello ASP.NET"}
                     };
        for (int i = 0; i < Query.Length; i++)//I assume length is 2
        {
             Button btn = new Button();
             btn.ID = arr[i, 0];
             btn.CommandArgument = arr[i, 1];
             btn.Click += new EventHandler(btn_Click);
             btn.Text = i.ToString();
             pagingPanel.Controls.Add(btn);
       }
}
protected void btn_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    System.Reflection.MethodInfo methodInfo = typeof(_Default2).GetMethod(btn.ID); //_Default2 is class name of code behind
    if (methodInfo != null)
    {
        object[] parameters = new object[] { btn.CommandArgument};
        methodInfo.Invoke(this,parameters);
    }
}
public void func1(object args)
{
    string test = args.ToString();
    Response.Write(test);
}
public void func2(object args)
{
    string test = args.ToString();
    Response.Write(test);
}

答案 1 :(得分:1)

首先 - 你必须创建按钮,无论它是否回发。按钮单击是回发的原因,如果您没有按钮 - 将点击什么?

其次,添加到您的页面类:

Dictionary<Button, ButtonInfo> fButtonLookup = new Dictionary<Button, ButtonInfo>();

然后,在哪里创建按钮:

fButtonLookup.Clear();
for (int i = 0; i < Query.length; i++)
{
  Button btn = new Button();
  fButtonLookup.Add(btn, new ButtonInfo() { whatever information about this button you want to keep});
  ...
}

然后,在您的按钮中点击:

protected void btn_Click(object sender, EventArgs e)
{
  Button btn = (Button)sender;
  if (fButtonLookup.ContainsKey(btn))
  {
    ButtonInfo info = fButtonLookup[btn];
    // do waht you need with button information
  }
}

答案 2 :(得分:0)

您可以检查sender以查看点击了哪个按钮:

protected void btn_Click(object sender, EventArgs e)
{
    if (sender == btnOne)
        performBtnOne("foo");
    else if (sender == btnTwo)
        performButtonTwo("bar");
}

答案 3 :(得分:0)

扩展其中的...86

protected void btn_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;  //Now you have an instantiated version of the button pressed.
    switch (btn.Name)
    {
        case "foo":
            performBtnOne();
            break;
        case "bar":
            performBtnTwo();
            break;
        default:
            performUnexpectedButton();
            break;
    }
}