访问动态添加的按钮列(在Datagrid中)单击事件。 C#/ ASP.NET
时间:2020-03-06 14:53:12 来源:igfitidea点击:
当我动态创建一个Datagrid并添加一个新的buttoncolumn时,如何访问buttoncolumn_click事件?
谢谢。
解决方案
MSDN网站上的这篇文章清楚地说明了如何向数据网格中添加按钮。代替使用按钮的click事件,我们将使用DataGrid的command事件。每个按钮都会传递我们要设置的特定命令参数。
本文介绍如何通过按钮使用命令事件。在其中使用CommandArguments和CommandNames。
这是我创建数据网格的地方:
System.Web.UI.WebControls.DataGrid Datagridtest =新的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();
这是我尝试使用的事件:
受保护的void Datagridtest_ItemCommand(对象源,DataGridCommandEventArgs e)
{
....
}
这种想法可能会有所帮助,因为我似乎根本无法捕捉到该事件。
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 }