Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否删除CommandField中的确认消息?_C#_Asp.net_Gridview - Fatal编程技术网

C# 是否删除CommandField中的确认消息?

C# 是否删除CommandField中的确认消息?,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在尝试获取确认消息,同时单击网格视图中的删除按钮。如果Iconform,则在GridView中仅删除该行 *.ASPX <Columns> <asp:CommandField ButtonType="Button" ShowDeleteButton="true" /> </Columns> 更改rowdatabound事件,如下所示 protected void gv_RowDataBound(object sender, GridViewR

我正在尝试获取确认消息,同时单击网格视图中的删除按钮。如果Iconform,则在GridView中仅删除该行

*.ASPX

<Columns>

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

</Columns>

更改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?');"; 
    }
}
我得到了朋友们的回答

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

我将CommandField更改为TemplateField


谢谢

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

下面是解释代码

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

谢谢


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


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

我使用了上面的代码。。我遇到了类似“无法将类型为“System.Web.UI.LiteralControl”的对象强制转换为类型为“System.Web.UI.WebControl.Button.”的错误。请使用此((LinkButton)e.Row.Cells[0]。Controls[0])。OnClientClick=“return confirm('您确定要删除吗?');”;如果您在数据源上调用了deletesql(如果您使用的是SQL),那么您仍然能够使用DeleteCommand功能吗?
<asp:LinkButton ID="lbDelete" runat="server" OnClick="lbDelete_Click" OnClientClick="return fnConfirm();"> Delete</asp:LinkButton>
<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>
<asp:TemplateField HeaderText="DELETE" ShowHeader="False">
                        <ItemTemplate>
                        <span onclick="return confirm('Are you sure to Delete?')">
                            <asp:Button ID="Button1" runat="server" CausesValidation="False"
                                CommandName="Delete" Text="Delete" />
                        </ItemTemplate>
</asp:TemplateField>