0

我想动态地向我的页面添加按钮。这将取决于 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);
           }
        }
    }

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

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

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

我怎样才能达到预期的效果?

4

4 回答 4

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);
}
于 2013-09-19T02:33:12.070 回答
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
  }
}
于 2013-09-19T03:02:39.563 回答
0

您可以检查一下sender单击了哪个按钮:

protected void btn_Click(object sender, EventArgs e)
{
    if (sender == btnOne)
        performBtnOne("foo");
    else if (sender == btnTwo)
        performButtonTwo("bar");
}
于 2013-09-18T21:38:35.380 回答
0

扩展 itsme86...

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;
    }
}
于 2013-09-18T21:47:59.327 回答