编写javascript以在gridview rowdeleting事件中确认删除

发布于 2024-11-29 08:13:07 字数 387 浏览 0 评论 0原文

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

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

我的代码为;

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

但是,如果有一个链接按钮(而不是命令字段),则效果很好,因为我可以轻松地在链接按钮的 OnClick 事件上编写内容,并且可以在 Gridview RowDataBound 事件中添加属性。

对于命令的字段删除按钮同样如何工作?请指导!

谢谢!

I am deleting a row from gridview for which i have used gridview's default delete command field. On clicking, the gridview row deleting command gets fired and the selected row gets deleted. All good till now.

But before the row gets deleted, i need to set confirmation message to user. On clicking of OK button the row should get deleted else not (on click of cancel button).

I have the code as;

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

But this works fine if there is a linkbutton (instead of command field) as i could easily write on OnClick event of linkbutton and could add the attributes in Gridview RowDataBound event.

How the same would work for command's field delete button? Please guide!

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

×纯※雪 2024-12-06 08:13:07

本文介绍了如何完全满足您的需要:

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?');";
            }
        }
    }
}

This article explains how to do exactly what you need:

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

And here is the code you need to do it:

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?');";
            }
        }
    }
}
怪我入戏太深 2024-12-06 08:13:07

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

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?');");
                }
            }
        }
    }
}

Here is code which you can use....

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?');");
                }
            }
        }
    }
}
莫言歌 2024-12-06 08:13:07

试试这个。

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"

Try this.

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;
                }
            }
        }
    }
}

Much more dirty than I originally anticipated. Better to use a asp:TemplateField :)

Please note that my ButtonType="Button"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文