在gridview rowdeleting事件中编写用于确认删除的javascript

时间:2011-08-15 05:42:19

标签: .net asp.net visual-studio gridview delete-row

我正在从gridview中删除一行,我使用了gridview的默认删除命令字段。单击时,将触发gridview行删除命令,并删除所选行。一切都好到现在为止。

但在删除行之前,我需要向用户设置确认消息。单击“确定”按钮时,该行应该被删除,否则不会(单击“取消”按钮)。

我的代码为;

return confirm('Are you sure to delete?');

但如果有一个linkbutton(而不是命令字段),这可以正常工作,因为我可以轻松地在linkbutton的OnClick事件上编写,并且可以在Gridview RowDataBound事件中添加属性。

如何对命令的字段删除按钮起作用?请指导!

谢谢!

3 个答案:

答案 0 :(得分:2)

以下是您可以使用的代码....

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        foreach (DataControlField dcf in GridView1.Columns)
        {

            if (dcf.ToString() == "CommandField")
            {
                if (((CommandField)dcf).ShowDeleteButton == true)
                {
                    e.Row.Cells[GridView1.Columns.IndexOf(dcf)].Attributes.Add("onclick", "return confirm('Are you sure you want to delete?');");
                }
            }
        }
    }
}

答案 1 :(得分:2)

本文介绍了如何完全按照您的需要进行操作:

http://www.codeproject.com/KB/webforms/Gridview_Delete_confirmLS.aspx

以下是您需要执行此操作的代码:

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // loop all data rows
        foreach (DataControlFieldCell cell in e.Row.Cells)
        {
           // check all cells in one row
           foreach (Control control in cell.Controls)
           {
                // Must use LinkButton here instead of ImageButton
                // if you are having Links (not images) as the command button.
                ImageButton button = control as ImageButton;

                if (button != null && button.CommandName == "Delete")
                    // Add delete confirmation
                    button.OnClientClick = "return confirm('Are you sure you want to delete this record?');";
            }
        }
    }
}

答案 2 :(得分:0)

试试这个。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        foreach (DataControlFieldCell cell in e.Row.Cells)
        {
            foreach (Control control in cell.Controls)
            {
                // Choose between Button, ImageButton and LinkButton.
                // as my ButtonType="Button". 
                Button button = control as Button;
                if (button != null && button.CommandName == "Delete")
                {
                    string script = "if(confirm('Are you sure to delete?')) __doPostBack('{0}','{1}${2}'); else return false;";
                    string clickEvent = String.Format(
                        script,
                        GridView1.ClientID,
                        button.CommandName,
                        button.CommandArgument);
                    button.Attributes.Add("onclick", clickEvent);                                 
                    break;
                }
            }
        }
    }
}

比我原先预料的要脏得多。最好使用asp:TemplateField :)

请注意我的ButtonType="Button"