【问题标题】:Create buttons dynamically in asp.net page and fire click event to all buttons在 asp.net 页面中动态创建按钮并向所有按钮触发点击事件
【发布时间】:2014-05-14 08:03:16
【问题描述】:

我想在 asp.net 页面中动态创建按钮并编写代码。通过以下代码动态创建的按钮。

List<string> category = new List<string>();
            category.Add("AAA");
            category.Add("BBB");
            category.Add("CCC");
            category.Add("DDD");
            category.Add("EEE");
            for (int i = 0; i < category.Count; i++)
            {
                TableRow tr = new TableRow();

                TableCell cl = new TableCell();
                TableCell cl2 = new TableCell();
                Button button = new Button();
                button.ID = "raid" + i;
                button.Text = category[i];
                button.Click +=button_Click;

 private void button_Click(object sender, EventArgs e)
        {
            Response.Write(sender.ToString());
        }

现在我想为所有按钮添加点击事件,并想知道哪个按钮点击事件被触发了。 任何的想法? Response.Write(sender.ToString());或 Response.Write(e.ToString());返回公共属性。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您可以使用 CommandArgument 属性。

    button.ID = "raid" + i;
    button.Text = category[i];
    button.Click +=button_Click;
    button.CommandArgument +=category[i];
    
    
    private void button_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        string category = btn.CommandArgument;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-11-13
      • 2012-08-18
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多