0

我动态创建了一个 gridview 并添加了 AutoGenerateEditButton = true; 属性,我看到它在将表格加载到gridview时将编辑链接添加到所有字段。但是当我单击编辑按钮时,除了回发之外,什么都没有发生。我究竟做错了什么?

        GridView gridData = new GridView();
        gridData.ID = "test";
        gridData.AutoGenerateEditButton = true;
        gridData.RowEditing += (sender, e) => grid_RowEditing(tbl, e, sender);
        gridData.DataSource = tbl;
        gridData.DataBind();

 protected void grid_RowEditing(DataTable tbl, GridViewEditEventArgs e, object sender)
    {
        ((GridView)sender).EditIndex = e.NewEditIndex;
        // call your databinding method here
        ((GridView)sender).DataSource = tbl ;
        ((GridView)sender).DataBind();
    }
4

2 回答 2

0

我的猜测是,因为您通过重新创建每个回发来覆盖网格,所以点击被忽略了。

建议的方法是静态创建不可见的网格:

<asp:GridView .... Visible="false" />

然后在需要时让它可见。这种方法(与动态创建相反)将使您免于许多问题,很可能包括您面临的问题。

编辑:如果你真的,真的想遵循你的路线,那么我的猜测是你只是没有指定你的数据键名,所以编辑点击不携带点击行的信息。

gridData.DataKeyNames = new string[] { "ID" };

(如果您的主键列名称是ID

于 2013-08-26T17:59:56.823 回答
0

a <p id="p" runat="server">在客户端添加并在此控件中添加了该网格。这是我的代码。我使用 SqlDataSource 而不是 Datatable,你可以使用它的任何一个。希望它对你有用..

    protected void gridview()
    {               
        GridView gridData = new GridView();
        gridData.ID = "test";
        gridData.AutoGenerateEditButton = true;

        gridData.RowEditing += (sender, e) => grid_RowEditing(SqlDataSource1, e, sender);
        gridData.DataSource = SqlDataSource1;
        gridData.DataBind();
        p.Controls.Add(gridData);
    }

    protected void grid_RowEditing(SqlDataSource tbl, GridViewEditEventArgs e, object sender)
    {
        ((GridView)sender).EditIndex = e.NewEditIndex;
        // call your databinding method here
        ((GridView)sender).DataSource = tbl;
        ((GridView)sender).DataBind();
    }

2)我也试过datatable了,它的工作原理。这是我的代码。

    protected void gridview()
    {

        DataTable dt = new DataTable();            
        dt.Columns.Add("id");
        dt.Columns.Add("name");         
        for (int i = 0; i < 100; i++)
        {               
            dt.Rows.Add(i.ToString(),"v"+i.ToString());
        }

        GridView gridData = new GridView();
        gridData.ID = "test";
        gridData.AutoGenerateEditButton = true;
        gridData.RowEditing += (sender, e) => grid_RowEditing(dt, e, sender);
        gridData.DataSource = dt;
        gridData.DataBind();
        p.Controls.Add(gridData);
    }

    protected void grid_RowEditing(DataTable tbl, GridViewEditEventArgs e, object sender)
    {
        ((GridView)sender).EditIndex = e.NewEditIndex;
        // call your databinding method here
        ((GridView)sender).DataSource = tbl;
        ((GridView)sender).DataBind();
    }
于 2013-08-26T19:07:35.920 回答