This shows a pop-up on a link button clicked inside a first grid and then open a second grid in a modal window of Bootstrap. With Bootstrap it is easy to design responsive sites. In this I’ve used the jQuery library and Bootstrap.

Background

I encountered an opportunity from the problem to show one grid view on a link button click in modal pop-up programmatically, so I wrote this article to help other developers.

Initial chamber

Step 1

Open your Visual Studio and create an empty website, provide a suitable name such as GridViewPopup.

Step 2

In Solution Explorer you get your empty website, then add web forms.

For Web Form

GridViewPopup (your empty website). Right-click and select Add New Item -> Web Form. Name it GridviewPopup.aspx.

Design chamber

Step 3

Open the GridviewPopup.aspx file and write some code for the design of the application.

First add the jQuery plugin in your head section. Here I used jquery-1.10.2.js plugin for demonstration purposes.

How to add in your page

In the head section of your web page:

  1. <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
  2. Add bootstrap CSS plugin
  3. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"/>
Then add the following plugin for optional theme:
  1. <!-- Optional theme -->
  2. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"/>
After that I added the Bootstrap plugin to work with bootstrap as in the following:
  1. <!-- Optional theme -->
  2. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"/>
  3. After this I’ve added Bootstrap plugin for working with bootstrap-
  4. <!-- Latest compiled and minified JavaScript -->
  5. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  6. Add control on your page and your page looks like below-
  7. div style="background-color:mediumaquamarine;">
  8. <asp:GridView ID="GridView1" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound" runat="server">
  9. <Columns>
  10. <asp:BoundField DataField="Product_Name" HeaderText="Product_Name" />
  11. <asp:BoundField DataField="Quantity" HeaderText="Quantity" />
  12. <asp:BoundField DataField="Price" HeaderText="Price" />
  13. <asp:TemplateField HeaderText="Show related">
  14. <ItemTemplate>
  15. <asp:LinkButton ID="lnkdetail" href="#myModal" data-toggle="modal" runat="server">Show Data</asp:LinkButton>
  16. </ItemTemplate>
  17. </asp:TemplateField>
  18. </Columns>
  19. </asp:GridView>
  20. </div>
Now the design looks as in the following:

design

Now add a GridView to be shown in a modal popup:

  1. <!-- this is bootstrp modal popup -->
  2. <div id="myModal" class="modal fade">
  3. <div class="modal-dialog">
  4. <div class="modal-content">
  5. <div class="modal-header">
  6. <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
  7. <h4 class="modal-title">Welcome in detail page</h4>
  8. </div>
  9. <div class="modal-body" style="overflow-y: scroll; max-height: 85%; margin-top: 50px; margin-bottom: 50px;">
  10. <asp:Label ID="lblmessage" runat="server" ClientIDMode="Static"></asp:Label>
  11. <asp:GridView ID="GridView2" runat="server"></asp:GridView>
  12. </div>
  13. <div class="modal-footer">
  14. <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
  15. </div>
  16. </div>
  17. </div>
  18. </div>
Here I have taken a dive to show a GridView in a modal pop-up and that ID is myModal and in the link button URL I've called this ID to show it in a modal pop-up.
  1. <asp:LinkButton ID="lnkdetail" href="#myModal" data-toggle="modal" runat="server">Show Data</asp:LinkButton>
Now your page looks as in the following:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridviewPopup.aspx.cs" Inherits="GridviewPopup" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
  7. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"/>
  8. <!-- Optional theme -->
  9. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"/>
  10. <!-- Latest compiled and minified JavaScript -->
  11. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  12. </head>
  13. <body>
  14. <form id="form1" runat="server">
  15. <center>
  16. <div style="background-color:mediumaquamarine;">
  17. <asp:GridView ID="GridView1" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound" runat="server">
  18. <Columns>
  19. <asp:BoundField DataField="Product_Name" HeaderText="Product_Name" />
  20. <asp:BoundField DataField="Quantity" HeaderText="Quantity" />
  21. <asp:BoundField DataField="Price" HeaderText="Price" />
  22. <asp:TemplateField HeaderText="Show related">
  23. <ItemTemplate>
  24. <asp:LinkButton ID="lnkdetail" href="#myModal" data-toggle="modal" runat="server">Show Data</asp:LinkButton>
  25. </ItemTemplate>
  26. </asp:TemplateField>
  27. </Columns>
  28. </asp:GridView>
  29. </div>
  30. <!-- this is bootstrp modal popup -->
  31. <div id="myModal" class="modal fade">
  32. <div class="modal-dialog">
  33. <div class="modal-content">
  34. <div class="modal-header">
  35. <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
  36. <h4 class="modal-title">Welcome in detail page</h4>
  37. </div>
  38. <div class="modal-body" style="overflow-y: scroll; max-height: 85%; margin-top: 50px; margin-bottom: 50px;">
  39. <asp:Label ID="lblmessage" runat="server" ClientIDMode="Static"></asp:Label>
  40. <asp:GridView ID="GridView2" runat="server"></asp:GridView>
  41. </div>
  42. <div class="modal-footer">
  43. <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
  44. </div>
  45. </div>
  46. </div>
  47. </div>
  48. </center>
  49. <!-- end -->
  50. </form>
  51. </body>
  52. </html>
On code behind page

Adding namespaces to the page:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
Here I created a static data table for binding the Grid. Now the page looks as in:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. public partial class GridviewPopup : System.Web.UI.Page
  9. {
  10. protected void Page_Load(object sender, EventArgs e)
  11. {
  12. if (!IsPostBack)
  13. {
  14. BindGridView1();
  15. }
  16. }
  17. protected void BindGridView1() {
  18. DataSet ds = new DataSet();
  19. DataTable dt;
  20. DataRow dr;
  21. DataColumn pName;
  22. DataColumn pQty;
  23. DataColumn pPrice;
  24. int i = 0;
  25. dt = new DataTable();
  26. pName = new DataColumn("Product_Name", Type.GetType("System.String"));
  27. pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
  28. pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
  29. dt.Columns.Add(pName);
  30. dt.Columns.Add(pQty);
  31. dt.Columns.Add(pPrice);
  32. dr = dt.NewRow();
  33. dr["Product_Name"] = "Product 1";
  34. dr["Quantity"] = 2;
  35. dr["Price"] = 200;
  36. dt.Rows.Add(dr);
  37. dr = dt.NewRow();
  38. dr["Product_Name"] = "Product 2";
  39. dr["Quantity"] = 5;
  40. dr["Price"] = 480;
  41. dt.Rows.Add(dr);
  42. dr = dt.NewRow();
  43. dr["Product_Name"] = "Product 3";
  44. dr["Quantity"] = 8;
  45. dr["Price"] = 100;
  46. dt.Rows.Add(dr);
  47. dr = dt.NewRow();
  48. dr["Product_Name"] = "Product 4";
  49. dr["Quantity"] = 2;
  50. dr["Price"] = 500;
  51. dt.Rows.Add(dr);
  52. ds.Tables.Add(dt);
  53. GridView1.DataSource = ds.Tables[0];
  54. GridView1.DataBind();
  55. }
  56. //for pop-up gridview
  57. protected void BindGrid2() {
  58. DataSet ds = new DataSet();
  59. DataTable dt;
  60. DataRow dr;
  61. DataColumn pName;
  62. DataColumn pQty;
  63. DataColumn pPrice;
  64. int i = 0;
  65. dt = new DataTable();
  66. pName = new DataColumn("Product_Name", Type.GetType("System.String"));
  67. pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
  68. pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
  69. dt.Columns.Add(pName);
  70. dt.Columns.Add(pQty);
  71. dt.Columns.Add(pPrice);
  72. dr = dt.NewRow();
  73. dr["Product_Name"] = "FMCG";
  74. dr["Quantity"] = 2;
  75. dr["Price"] = 200;
  76. dt.Rows.Add(dr);
  77. dr = dt.NewRow();
  78. dr["Product_Name"] = "Cold Drink";
  79. dr["Quantity"] = 5;
  80. dr["Price"] = 480;
  81. dt.Rows.Add(dr);
  82. dr = dt.NewRow();
  83. dr["Product_Name"] = "Biscuits";
  84. dr["Quantity"] = 8;
  85. dr["Price"] = 100;
  86. dt.Rows.Add(dr);
  87. dr = dt.NewRow();
  88. dr["Product_Name"] = "Mixture";
  89. dr["Quantity"] = 2;
  90. dr["Price"] = 500;
  91. dt.Rows.Add(dr);
  92. ds.Tables.Add(dt);
  93. GridView2.DataSource = ds.Tables[0];
  94. GridView2.DataBind();
  95. }
  96. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  97. {
  98. if (e.Row.RowType == DataControlRowType.DataRow)
  99. {
  100. string username = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Product_Name"));
  101. LinkButton lnkbtnresult = (LinkButton)e.Row.FindControl("lnkdetail");
  102. BindGrid2();
  103. }
  104. }
  105. }
Now the output looks as in:

gridview
Figure 1: On initial loading

On link button click
Figure 2: On link button click

I hope you liked this. Have a good day. Thank you for reading.