将参数添加到新的事件处理程序

时间:2013-08-26 17:04:46

标签: c# asp.net gridview

我正在创建一个动态网格视图,还有一部分我为编辑创建了一个新的事件处理程序。我还创建了一个用于进行实际编辑的新方法,但我需要将一个数据表名称作为参数之一传递给我,以便重新绑定它。我无法弄清楚在哪里添加该参数:

        GridView gridData = new GridView();
        gridData.ID = "test";
        gridData.AutoGenerateEditButton = true;
        gridData.RowEditing += new GridViewEditEventHandler(grid_RowEditing);
        gridData.DataSource = tbl;
        gridData.DataBind();


protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        ((GridView)sender).EditIndex = e.NewEditIndex;

        // I don't know how to pass the datasource name to this method, or if its even possible, because i won't ever know the actual gridview name because its dynamically created
        //((GridView)sender).DataSource =  ;
        ((GridView)sender).DataBind();

1 个答案:

答案 0 :(得分:0)

如果您不需要取消挂钩事件处理程序,最简单的方法是使用lambda表达式

string dataSourceName = ...;
gridData.RowEditing += (sender, e) => grid_RowEditing(gridData, dataSourceName, e);
        gridData.DataSource = tbl;
        gridData.DataBind();


void grid_RowEditing(GridView gridData, string dataSourceName, GridViewEventArgs e) {
  ...
}