提示一个删除弹出窗口以确认删除- gridviewasp.net

Prompt a delete pop up window to confirm the delete - GridView ASP.NET

本文关键字:删除 窗口 确认 net gridviewasp 一个 提示      更新时间:2023-09-26

当用户点击命令栏中的删除按钮时,我需要提示一个窗口并询问:"您确定要删除Joe Smith吗?"

我写在客户端,但我需要行名也显示。

<asp:CommandField ButtonType="Button" ShowDeleteButton="True" ShowEditButton="True"
        HeaderText="Action" />
    <asp:TemplateField ShowHeader="False">
        <ItemTemplate>
            <asp:Button ID="DeleteButton" runat="server"
                CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this event?');"
                AlternateText="Delete" />               
        </ItemTemplate>

这是我的删除行代码。在这种方法中是否有代码提示?或者我需要做RowDataBound。

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    try
    {
        string connectionString = System.Configuration.ConfigurationManager.AppSettings["DBConnection"];
        SqlConnection connDel = new SqlConnection(connectionString);
        int User_ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
        SqlCommand cmd = new SqlCommand("DELETE FROM DoNotPay_Email_List WHERE User_ID=" + User_ID + "", connDel);
        connDel.Open();
        int temp = cmd.ExecuteNonQuery();
        /* if (temp == 1)
         {
             lblMessage.Text = "Record deleted successfully";
        }*/
        BindGrid();
        cmd.Dispose();
        connDel.Close();
    }
    catch (SqlException ex)
    {
        lblMessage.Text = (ex.Message);
    }
}


您不希望在Row_Deleting中这样做,因为这是在服务器端运行;如果采用这种方法,则需要对每个删除操作进行多次回发。

你需要在RowDataBound事件中设置OnClientClick属性,在那里你可以访问绑定的行值,并且可以连接你的消息以包含你想要的任何数据。

编辑:看一下第3步中的RowDataBound代码,它将向您展示如何获取对按钮的引用、检索行数据和自定义提示文本。 http://msdn.microsoft.com/en-us/library/bb428868.aspx