【问题标题】:Accessing a dynamitcally added buttoncolumn's (in a Datagrid) click event. C#/ASP.NET访问动态添加的按钮列(在 Datagrid 中)的单击事件。 C#/ASP.NET
【发布时间】:2008-09-29 17:42:37
【问题描述】:

当我动态创建 Datagrid 并添加新的 buttoncolumn 时,如何访问 buttoncolumn_click 事件?

谢谢。

【问题讨论】:

  • 标题有错字:dynamittically->dynamic
  • 您是否需要实际访问该单个按钮的 Click 事件,还是只需在单击该列中的按钮时执行一个操作?
  • 不,只要单击该列中的按钮,我只需要执行一个操作。

标签: c# asp.net


【解决方案1】:
protected void Page_Load(object sender, EventArgs e)
{
  DataGrid dg = new DataGrid();

  dg.GridLines = GridLines.Both;

  dg.Columns.Add(new ButtonColumn {
    CommandName = "add",
    HeaderText = "Event Details",
    Text = "Details",
    ButtonType = ButtonColumnType.PushButton
  });

  dg.DataSource = getDataTable();
  dg.DataBind();

  dg.ItemCommand += new DataGridCommandEventHandler(dg_ItemCommand);

  pnlMain.Controls.Add(dg);
}

protected void dg_ItemCommand(object source, DataGridCommandEventArgs e)
{
  if (e.CommandName == "add")
  {
    throw new Exception("add it!");
  }
}

protected DataTable getDataTable()
{
  // returns your data table
}

【讨论】:

    【解决方案2】:

    MSDN 网站上的这篇文章清楚地解释了如何处理adding a button into a datagrid。您将使用 DataGrid 的命令事件,而不是使用按钮的单击事件。每个按钮都将传递您将设置的特定命令参数。

    本文显示how to use the command event with buttons。在其中您使用 CommandArguments 和 CommandNames。

    【讨论】:

      【解决方案3】:

      这里是我创建数据网格的地方:

      System.Web.UI.WebControls.DataGrid Datagridtest = new System.Web.UI.WebControls.DataGrid();

              Datagridtest.Width = 600;
              Datagridtest.GridLines = GridLines.Both;
              Datagridtest.CellPadding = 1;
      
              ButtonColumn bc = new ButtonColumn();
              bc.CommandName = "add";
              bc.HeaderText = "Event Details";
              bc.Text = "Details";
              bc.ButtonType = System.Web.UI.WebControls.ButtonColumnType.PushButton;
              Datagridtest.Columns.Add(bc);
              PlaceHolder1.Controls.Add(Datagridtest);
      
              Datagridtest.DataSource = dt;
              Datagridtest.DataBind();
      

      这是我尝试使用的事件:

      protected void Datagridtest_ItemCommand(object source, DataGridCommandEventArgs e) { …… }

      认为这可能会有所帮助,因为我似乎根本无法捕捉到事件。

      【讨论】:

        猜你喜欢
        • 2013-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-08
        • 1970-01-01
        • 1970-01-01
        • 2011-05-01
        • 1970-01-01
        相关资源
        最近更新 更多