删除CommandField中的确认消息?

时间:2013-10-28 12:10:18

标签: c# asp.net gridview

我正在尝试确认消息,同时点击删除删除按钮 GridView 。如果我符合,则只会在 GridView 中删除该行。

* .ASPX

<Columns>

    <asp:CommandField ButtonType="Button" ShowDeleteButton="true" />

</Columns>

* .ASPX.CS

protected void grdPersTable_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Button buttonCommandField = e.Row.Cells[0].Controls[0] as Button;
        buttonCommandField.Attributes["onClick"] = 
               string.Format("return confirm('Are you want delete ')");
    }
}

protected void grdPersTable_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    Label lbl0 = (Label)grdPersTable.Rows[e.RowIndex].FindControl("lblId");
    txtId.Text = lbl0.Text;
    obj.DeleteV(Convert.ToInt32(txtId.Text));
    grdPersTable.DataSource = obj.GetTableValues();
    grdPersTable.DataBind();        
    lblMessage.Text = "Deleted successfully !";
}

4 个答案:

答案 0 :(得分:6)

我得到了回答朋友

<asp:TemplateField>
      <ItemTemplate>
            <asp:Button ID="deletebtn" runat="server" CommandName="Delete" 
             Text="Delete" OnClientClick="return confirm('Are you sure?');" />
      </ItemTemplate>
</asp:TemplateField>

我将 CommandField 更改为 TemplateField

谢谢!

答案 1 :(得分:1)

更改rowdatabound事件,如下所示。

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        ((Button)e.Row.Cells[0].Controls[0]).OnClientClick = "return confirm('Are you sure you want to delete?');"; 
    }
}

答案 2 :(得分:1)

只需在onclientclick事件上调用javascript函数并要求确认。如果返回true,则可以调用服务器端代码进行删除。

以下是解释代码

<asp:LinkButton ID="lbDelete" runat="server" OnClick="lbDelete_Click" OnClientClick="return fnConfirm();"> Delete</asp:LinkButton>

以下是javascript函数:

<script type="text/javascript">
function fnConfirm() {
    if (confirm("The item will be deleted. Are you sure want to continue?") == true)
        return true;
    else
        return false;
}
</script>

您可以在以下链接中查看包含源代码的详细文章

http://www.dotnetpickles.com/2013/03/how-to-show-confirm-message-while.html

由于

答案 3 :(得分:0)

Microsoft 的文档显示了如何使用 ASP 数据源的 DeleteCommand 参数执行此操作的示例:

https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.sqldatasource.deletecommand?view=netframework-4.8

它使用 DataSource 类的“OnDeleting”参数和事件。