Bootstrap is the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first web sites. In this example, I will show how to use Bootstrap Confirm modal in ASP.NET. This is the most common Scenario where we want to confirm before we delete any record. Here, I will demonstrate how call confirm modal from within Gridview link and if user confirms then only record will be deleted.

For the sake of simplicity, I have used a class with some data. You can also use database to populate Gridview.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. namespace BootStrapConfirm
  6. {
  7. [Serializable()]
  8. public class Details
  9. {
  10. public int Id { get; set; }
  11. public string Name { get; set; }
  12. public string City { get; set; }
  13. public List<Details> GetDetails()
  14. {
  15. List<Details> Persons = new List<Details>();
  16. Persons.Add(new Details { Id=1,Name= "Rajeev", City="Bangalore" });
  17. Persons.Add(new Details { Id = 2, Name = "Raj Kumar", City = "Chennai" });
  18. Persons.Add(new Details { Id = 3, Name = "Raj", City = "Mumbai" });
  19. Persons.Add(new Details { Id = 4, Name = "Shiva", City = "Pune" });
  20. Persons.Add(new Details { Id = 5, Name = "Lalit", City = "Ahmedabad" });
  21. return Persons;
  22. }
  23. }
  24. }
Now we will create a webform ConfirmDemo.aspx.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ConfirmDemo.aspx.cs" Inherits="BootStrapConfirm.ConfirmDemo" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Confirm Demo</title>
  6. <script src="js/jquery-1.10.2.min.js"></script>
  7. <script src="js/bootstrap.min.js"></script>
  8. <script src="js/bootstrap-dialog.min.js"></script>
  9. <link href="css/bootstrap-dialog.min.css" rel="stylesheet" />
  10. <link href="css/bootstrap.min.css" rel="stylesheet" />
  11. <style>
  12. .row1 {
  13. padding: 4px 30px 4px 6px;
  14. border-bottom: 1px solid #b3b3b3;
  15. }
  16. .header {
  17. background: #646464;
  18. color: #d1d1d1;
  19. text-align: center;
  20. font-weight: bold;
  21. padding: 4px 30px 4px 6px;
  22. border-bottom: 1px solid #b3b3b3;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <form id="form1" runat="server">
  28. <h2>
  29. Confirm Demo Using Bootstrap Dialog
  30. </h2>
  31. <div class="container">
  32. <asp:GridView ID="grdDemo" runat="server" AutoGenerateColumns="false" RowStyle-CssClass="row1" HeaderStyle-CssClass="header" OnRowCommand="grdDemo_RowCommand" OnRowDeleting="grdDemo_RowDeleting">
  33. <Columns>
  34. <asp:TemplateField HeaderText="Id">
  35. <ItemTemplate>
  36. <asp:Label ID="lblId" runat="server" Text='<%#Eval("Id") %>'></asp:Label>
  37. </ItemTemplate>
  38. </asp:TemplateField>
  39. <asp:TemplateField HeaderText="Name">
  40. <ItemTemplate>
  41. <asp:Label ID="lblName" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
  42. </ItemTemplate>
  43. </asp:TemplateField>
  44. <asp:TemplateField HeaderText="City">
  45. <ItemTemplate>
  46. <asp:Label ID="lblCity" runat="server" Text='<%#Eval("City") %>'></asp:Label>
  47. </ItemTemplate>
  48. </asp:TemplateField>
  49. <asp:TemplateField HeaderText="Action">
  50. <ItemTemplate>
  51. <asp:LinkButton ID="lnkDelete" runat="server"
  52. CssClass=""
  53. OnClientClick='<%# string.Concat("if(!popup(this.id",",",Eval("ID"),",\"",Eval("Name"),"\"))return false; ") %>'
  54. Text="Delete" CommandArgument='<%# Eval("Id") %>' CommandName="Delete" ></asp:LinkButton>
  55. </ItemTemplate>
  56. </asp:TemplateField>
  57. </Columns>
  58. </asp:GridView>
  59. </div>
  60. <script>
  61. function popup(lnk, id, Name) {
  62. BootstrapDialog.confirm({
  63. title: 'WARNING',
  64. message: 'Do You Want To Delete <b>'+Name+'</b>',
  65. type: BootstrapDialog.TYPE_WARNING, // <-- Default value is BootstrapDialog.TYPE_PRIMARY
  66. closable: true, // <-- Default value is false
  67. draggable: true, // <-- Default value is false
  68. btnCancelLabel: 'Cancel', // <-- Default value is 'Cancel',
  69. btnOKLabel: 'Ok', // <-- Default value is 'OK',
  70. btnOKClass: 'btn-warning', // <-- If you didn't specify it, dialog type will be used,
  71. callback: function (result) {
  72. // result will be true if button was click, while it will be false if users close the dialog directly.
  73. if (result) {
  74. javascript: __doPostBack('grdDemo$ctl02$lnkDelete', '');
  75. } else {
  76. BootstrapDialog.closeAll();
  77. }
  78. }
  79. });
  80. }
  81. </script>
  82. </form>
  83. </body>
  84. </html>
Here is the code behind for the same.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. namespace BootStrapConfirm
  8. {
  9. public partial class ConfirmDemo : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. if (!IsPostBack)
  14. gridbind();
  15. }
  16. protected void grdDemo_RowCommand(object sender, GridViewCommandEventArgs e)
  17. {
  18. if (e.CommandName == "Delete")
  19. {
  20. LinkButton lnkView = (LinkButton)e.CommandSource;
  21. string dealId = lnkView.CommandArgument;
  22. List<Details> data = (List<Details>)ViewState["Data"];
  23. data.RemoveAll(d => d.Id == Convert.ToInt32(dealId));
  24. ViewState["Data"] = data;
  25. gridbind();
  26. System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "BootstrapDialog.alert('Record Deleted Successfully.');", true);
  27. //data.
  28. }
  29. }
  30. protected void gridbind()
  31. {
  32. if (ViewState["Data"] != null)
  33. {
  34. }
  35. else
  36. {
  37. Details d = new Details();
  38. ViewState["Data"] = d.GetDetails();
  39. }
  40. grdDemo.DataSource = (List<Details>)ViewState["Data"];
  41. grdDemo.DataBind();
  42. }
  43. protected void grdDemo_RowDeleting(object sender, GridViewDeleteEventArgs e)
  44. {
  45. }
  46. }
  47. }