asp:CommandField ButtonType =“Link”ShowEditButton =“true”ShowDeleteButton =“true”OnRowDeleting无法正常工作

时间:2014-10-16 03:14:27

标签: c# asp.net

对不起, 我关注this tutorial 在该教程中,使用Link的命令字段.. 我想使用Button而不是LinkBut​​ton .. draft 我将我的代码更改为喜欢这个:

<asp:CommandField ButtonType="Button" ShowEditButton="true" ShowDeleteButton="true"
                            HeaderText="Action" ItemStyle-Width="150" />

这样的代码背后:

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != GridViewPaging.EditIndex)
            {
                (e.Row.Cells[3].Controls[2] as Button).Attributes["onclick"] = "return confirm('Do you want to delete this row?');";
            }
        }

然后我的OnRowDeleting方法无效...

protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int sales_so_id = Convert.ToInt32(GridViewPaging.DataKeys[e.RowIndex].Values[0]);

            //delete operation
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand("DeleteSO", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@sales_so_id", SqlDbType.Int));
            cmd.Parameters["@sales_so_id"].Value = sales_so_id;
            cmd.ExecuteNonQuery();
            con.Close();
            Getdata();
        }

我该怎么办? 当我使用LinkBut​​ton时,一切都很好.. 但是,现在代码背后的OnRowDeleting无效.. 请帮忙。

1 个答案:

答案 0 :(得分:0)

最后,我使用this tutorial

我以前的onDataBound是这样的:

if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != GridViewPaging.EditIndex)
            {
                (e.Row.Cells[3].Controls[2] as Button).Attributes["onclick"] = "return confirm('Do you want to delete this row?');";
            }

我将其更改为:

if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string item = e.Row.Cells[0].Text;
            foreach (Button button in e.Row.Cells[3].Controls.OfType<Button>())
            {
                if (button.CommandName == "Delete")
                {
                    button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + item + "?')){ return false; };";
                }
            }
        }

现在它正在运作.. 谢谢