ASP C#Gridview确认删除并提示用户发送短信

时间:2017-11-16 12:55:17

标签: c# asp.net .net gridview

我正在使用ASP C#VS 2015.我设法使用来自MySQL的数据生成gridview。更新和删除命令显示为链接,我设法将它们正确地连接到事件。

现在我想添加一项功能,在删除之前添加确认信息以及删除信息(文本框)?

我如何在.NET中执行此操作?

我精通PHP和jQuery,但仍处于.NET的学习曲线中。 提前谢谢。

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="id"
            OnRowDataBound="OnRowDataBound" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit"
            OnRowUpdating="OnRowUpdating" EmptyDataText="No records has been added." OnRowDeleting="OnRowDeleting" OnRowDeleted="OnRowDeleted">
   <Columns>
       <asp:TemplateField HeaderText="Id" ItemStyle-Width="20">
           <ItemTemplate>
               <asp:Label ID="lblId" runat="server" Text='<%# Eval("id") %>'></asp:Label>
           </ItemTemplate>
           <ItemStyle Width="20px"></ItemStyle>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="Email">
           <ItemTemplate>
               <asp:Label ID="lblEmail" runat="server" Text='<%# Eval("email") %>'></asp:Label>
           </ItemTemplate>
       </asp:TemplateField>
       <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" DeleteText="Reject" headertext="Controls"></asp:CommandField>
   </Columns>
</asp:GridView>

1 个答案:

答案 0 :(得分:1)

试试这个

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string item = e.Row.Cells[0].Text;
        foreach (Button button in e.Row.Cells[2].Controls.OfType<Button>())
        {
            if (button.CommandName == "Delete")
            {
                button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + item + "?')){ return false; };";
            }
        }
    }
}