Introduction
This blog demonstrate how to open a GridView in a modal pop-up on another GridView link button clicked using bootstrap. This shows a pop-up on link button clicked inside first grid and then open second grid in a modal-popup of bootstrap. Bootstrap is easy to design responsive sites. In this I’ve used jQuery library and Bootstrap.
Background
I got an opportunity from a problem to show one gridview on linkbutton click in modal pop-up programmatically, so wrote this blog for helping some other developer.
Add library in your design page inside the head section. You can also download this in your code file and add in your solution as well:
- <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
- <!-- Optional theme -->
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
- <!-- Latest compiled and minified JavaScript -->
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:GridView ID="GridView1" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound" runat="server">
- <Columns>
- <asp:BoundField DataField="Product_Name" HeaderText="Product_Name" />
- <asp:BoundField DataField="Quantity" HeaderText="Quantity" />
- <asp:BoundField DataField="Price" HeaderText="Price" />
- <asp:TemplateField HeaderText="Show related">
- <ItemTemplate>
- <asp:LinkButton ID="lnkdelete" href="#myModal" data-toggle="modal" runat="server">Show Data</asp:LinkButton>
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- </asp:GridView>
- </div>
- <!-- this is bootstrp modal popup -->
- <div id="myModal" class="modal fade">
- <div class="modal-dialog">
- <div class="modal-content">
- <div class="modal-header">
- <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
- <h4 class="modal-title">Article for C# Corner</h4>
- </div>
- <div class="modal-body" style="overflow-y: scroll; max-height: 85%; margin-top: 50px; margin-bottom: 50px;">
- <asp:Label ID="lblmessage" runat="server" ClientIDMode="Static"></asp:Label>
- <asp:GridView ID="GridView2" runat="server"></asp:GridView>
- </div>
- <div class="modal-footer">
- <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
- </div>
- </div>
- </div>
- </div>
- <!-- end -->
- </form>
- </body>
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- //then write below code-
- protected void BindGridView1() {
- DataSet ds = new DataSet();
- DataTable dt;
- DataRow dr;
- DataColumn pName;
- DataColumn pQty;
- DataColumn pPrice;
- int i = 0;
- dt = new DataTable();
- pName = new DataColumn("Product_Name", Type.GetType("System.String"));
- pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
- pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
- dt.Columns.Add(pName);
- dt.Columns.Add(pQty);
- dt.Columns.Add(pPrice);
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 1";
- dr["Quantity"] = 2;
- dr["Price"] = 200;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 2";
- dr["Quantity"] = 5;
- dr["Price"] = 480;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 3";
- dr["Quantity"] = 8;
- dr["Price"] = 100;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 4";
- dr["Quantity"] = 2;
- dr["Price"] = 500;
- dt.Rows.Add(dr);
- ds.Tables.Add(dt);
- GridView1.DataSource = ds.Tables[0];
- GridView1.DataBind();
- }
- protected void BindGrid2() {
- DataSet ds = new DataSet();
- DataTable dt;
- DataRow dr;
- DataColumn pName;
- DataColumn pQty;
- DataColumn pPrice;
- int i = 0;
- dt = new DataTable();
- pName = new DataColumn("Product_Name", Type.GetType("System.String"));
- pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
- pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
- dt.Columns.Add(pName);
- dt.Columns.Add(pQty);
- dt.Columns.Add(pPrice);
- dr = dt.NewRow();
- dr["Product_Name"] = "FMCG";
- dr["Quantity"] = 2;
- dr["Price"] = 200;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["Product_Name"] = "Cold Drink";
- dr["Quantity"] = 5;
- dr["Price"] = 480;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["Product_Name"] = "Biscuits";
- dr["Quantity"] = 8;
- dr["Price"] = 100;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["Product_Name"] = "Mixture";
- dr["Quantity"] = 2;
- dr["Price"] = 500;
- dt.Rows.Add(dr);
- ds.Tables.Add(dt);
- GridView2.DataSource = ds.Tables[0];
- GridView2.DataBind();
- }
- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
- if (e.Row.RowType == DataControlRowType.DataRow) {
- string username = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Product_Name"));
- LinkButton lnkbtnresult = (LinkButton) e.Row.FindControl("lnkdelete");
- BindGrid2();
- }
- }
Design Page-
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridviewPopup.aspx.cs" Inherits="GridviewPopup" %>
- <!DOCTYPE html>
- <html
- xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
- <!-- Optional theme -->
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
- <!-- Latest compiled and minified JavaScript -->
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:GridView ID="GridView1" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound" runat="server">
- <Columns>
- <asp:BoundField DataField="Product_Name" HeaderText="Product_Name" />
- <asp:BoundField DataField="Quantity" HeaderText="Quantity" />
- <asp:BoundField DataField="Price" HeaderText="Price" />
- <asp:TemplateField HeaderText="Show related">
- <ItemTemplate>
- <asp:LinkButton ID="lnkdelete" href="#myModal" data-toggle="modal" runat="server">Show Data</asp:LinkButton>
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- </asp:GridView>
- </div>
- <!-- this is bootstrp modal popup -->
- <div id="myModal" class="modal fade">
- <div class="modal-dialog">
- <div class="modal-content">
- <div class="modal-header">
- <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
- <h4 class="modal-title">Article for C# Corner</h4>
- </div>
- <div class="modal-body" style="overflow-y: scroll; max-height: 85%; margin-top: 50px; margin-bottom: 50px;">
- <asp:Label ID="lblmessage" runat="server" ClientIDMode="Static"></asp:Label>
- <asp:GridView ID="GridView2" runat="server"></asp:GridView>
- </div>
- <div class="modal-footer">
- <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
- </div>
- </div>
- </div>
- </div>
- <!-- end -->
- </form>
- </body>
- </html>
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class GridviewPopup: System.Web.UI.Page {
- protected void Page_Load(object sender, EventArgs e) {
- if (!IsPostBack) {
- BindGridView1();
- }
- }
- protected void BindGridView1() {
- DataSet ds = new DataSet();
- DataTable dt;
- DataRow dr;
- DataColumn pName;
- DataColumn pQty;
- DataColumn pPrice;
- int i = 0;
- dt = new DataTable();
- pName = new DataColumn("Product_Name", Type.GetType("System.String"));
- pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
- pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
- dt.Columns.Add(pName);
- dt.Columns.Add(pQty);
- dt.Columns.Add(pPrice);
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 1";
- dr["Quantity"] = 2;
- dr["Price"] = 200;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 2";
- dr["Quantity"] = 5;
- dr["Price"] = 480;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 3";
- dr["Quantity"] = 8;
- dr["Price"] = 100;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["Product_Name"] = "Product 4";
- dr["Quantity"] = 2;
- dr["Price"] = 500;
- dt.Rows.Add(dr);
- ds.Tables.Add(dt);
- GridView1.DataSource = ds.Tables[0];
- GridView1.DataBind();
- }
- protected void BindGrid2() {
- DataSet ds = new DataSet();
- DataTable dt;
- DataRow dr;
- DataColumn pName;
- DataColumn pQty;
- DataColumn pPrice;
- int i = 0;
- dt = new DataTable();
- pName = new DataColumn("Product_Name", Type.GetType("System.String"));
- pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
- pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
- dt.Columns.Add(pName);
- dt.Columns.Add(pQty);
- dt.Columns.Add(pPrice);
- dr = dt.NewRow();
- dr["Product_Name"] = "FMCG";
- dr["Quantity"] = 2;
- dr["Price"] = 200;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["Product_Name"] = "Cold Drink";
- dr["Quantity"] = 5;
- dr["Price"] = 480;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["Product_Name"] = "Biscuits";
- dr["Quantity"] = 8;
- dr["Price"] = 100;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr["Product_Name"] = "Mixture";
- dr["Quantity"] = 2;
- dr["Price"] = 500;
- dt.Rows.Add(dr);
- ds.Tables.Add(dt);
- GridView2.DataSource = ds.Tables[0];
- GridView2.DataBind();
- }
- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
- if (e.Row.RowType == DataControlRowType.DataRow) {
- string username = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Product_Name"));
- LinkButton lnkbtnresult = (LinkButton) e.Row.FindControl("lnkdelete");
- BindGrid2();
- }
- }
- }

Figure 1: Output 1

Figure 2: Output 2
Important points:
- You have learned here how to show the modal pop-up using BootStrap and ASP.NET.
- You have learned how to show other grid inside a grid using link-button.

kalai romiPosted Feb 26, 2020, 2:47 AM
Thanks a lot...
pankaj kumarPosted Apr 9, 2018, 10:16 AM
When i select the single row then show the last row only.......its wrong
Dev BhagatPosted Mar 2, 2016, 1:09 AM
Hi. I have a question. RowDataBound will not bind second Grid with unique records.... we need second grid, based on first grid's unique rowId's. Pop up will appear that make sense, but not with unique records. As per default behavior of RowDatabound, last row records will be shown every time.... no matter you click on any row of first grid between "lnkbtnresult in rowdatabound event.... where are you using this.?
Upendra Pratap ShahiPosted Jun 26, 2015, 7:45 AM
Thanks Narasimha Reddy Chennupalli
Narasimha Reddy ChennupalliPosted Jun 26, 2015, 7:42 AM
Good one..