Answered by:
Loading nested gridview with jquery for very large data

Question
-
User1487175000 posted
Hi,
I am trying to load the nested gridview with jquery but i don't want to reload the data if its already loaded by the user. because retrieving detail data is time costly and large.
Example: consider country and cities scenario. cities will be loaded with jquery.
I dont want to load the cities name if cities name already loaded.
My
<script type="text/javascript"> $('.view').click(function () { var expand = $(this).attr("src"); // image like + and - to expand and collapse if (expand.includes("plus.png")) { // load details $(this).attr("src", "../images/minus.png"); var ID = $(this).attr("data-id"); //customer id $(this).closest('tr').after('<tr id="bookings"><td colspan="100%"></td></tr>'); // Here i want to check if data is already loaded then i dont want to load again. Currently its loaded everytime $('#bookings td').load('conferenceBookerBookingList.aspx?conferenceBooker=' + ID + ' #bookingList');
// Here i want to show loading cirlce image until data is loaded } else { // hide or collpase the detail row $(this).attr("src", "../images/plus.png"); $('#bookings').remove(); } return false; }); </script>Thanks in advance.
Tuesday, December 4, 2018 11:15 PM
Answers
-
User-893317190 posted
Hi shahid.majeed,
I find your code
$('#bookings td').load('conferenceBookerBookingList.aspx?conferenceBooker=' + ID + ' #bookingList');
Do you want to load an aspx page?
Normally, we use ajax to load json data and create html element in client side.
Below is my code.
#GridView1 table{ width:100% } </style> <script src="../Scripts/jquery-1.9.1.js"></script> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID" DataSourceID="SqlDataSource1"> <Columns> <asp:TemplateField HeaderText="Action"> <ItemTemplate> <asp:HiddenField runat="server" Value='<%#Eval("CategoryId") %>' > </asp:HiddenField> <img src="/images/icons/mini_add.png" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" /> <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" /> </Columns> </asp:GridView> <script> $(function () { //bind event to every image in the gridview $("#GridView1 img").click(function () { //get the tr element the image is in let tr = $(this).closest("tr"); // if the user want to close if ($(this).attr("src") === "/images/icons/clear.png") { //change it to the open icon $(this).attr("src", "/images/icons/mini_add.png") // ensure the nested table is loaded if (tr.next().find("table").length == 1) { //hide the table tr.next().hide(); return; } } //if the user wants to open the nested table if ($(this).attr("src") === "/images/icons/mini_add.png") { // change the img to close icon $(this).attr("src", "/images/icons/clear.png"); // ensure the table is loaded if (tr.next().find("table").length == 1) { //show the table tr.next().show(); return; } //here the table is not loaded //create an loading image var image = "<tr><td colspan='3' style='text-align:center'><img src='/images/icons/reload.png' width='200px' /></td></tr>"; //insert after the tr tr.after(image); //load the table $.ajax({ "url": "/Services/res1.ashx", "data": { id: $(this).siblings("input[type=hidden]").val() }, "dataType": "json", "success": function (data) { //create a table element var table = "<tr ><td colspan='3'><table>"; //loop through the data and insert into the created table $.each(data, function (index, ele) { var content="<tr >" content += "<td style='border:1px solid black'>" + ele.Id + "</td>"; content += "<td style='border:1px solid black'>" + ele.ProductName + "</td>"; content += "<td style='border:1px solid black'>" + ele.Description + "</td>"; content += "<td style='border:1px solid black'>" + ele.ImagePath + "</td>"; content += "<td style='border:1px solid black'>" + ele.UnitPrice + "</td>"; content += "</tr>"; table += content; }) table += "</table></td></tr>"; //remove the loading img tr.next().remove(); //insert the newly created table after the tr tr.after(table); } }) } }) }) </script> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WingtipToysConnectionString2 %>" SelectCommand="SELECT [CategoryName], [Description], [CategoryID] FROM [Categories]"></asp:SqlDataSource> </form>
Code of ashx.
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/json"; Thread.Sleep(3000); SqlConnection con = new SqlConnection(constr); using (SqlCommand com = new SqlCommand("select * from products where CategoryId=@id", con)) { com.Parameters.AddWithValue("id", context.Request["id"]);//get the CategoryId con.Open(); List<Product> list = new List<Product>(); using (SqlDataReader reader=com.ExecuteReader()) { while (reader.Read()) { Product pro = new Product(); pro.Description = reader.GetString(2); pro.Id = reader.GetInt32(0); pro.ProductName = reader.GetString(1); pro.ImagePath = reader.GetString(3); pro.UnitPrice = reader.GetDouble(4); list.Add(pro); } } ; JavaScriptSerializer serializer = new JavaScriptSerializer(); context.Response.Write(serializer.Serialize(list)); con.Close(); con.Dispose(); } } public class Product { public int Id { get; set; } public string ProductName { get; set; } public string Description { get; set; } public string ImagePath { get; set; } public double UnitPrice { get; set; } }
The result.
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 7, 2018 8:11 AM
All replies
-
User-893317190 posted
Hi shahid.majeed,
According to your description and the jquery code your have posted,
It seems you have customers who have many book.And you want to show every customer's book.
If you load every customer's book data through the ajax load method and insert the data into your html,there should be some differences between the dom element before you insert and after you insert, you could find the difference and decide whether to load ajax data according to the difference.
A good way to find the difference is to open F12 develop tool to check the html element.
Or if you could not find the difference or it is hard to deal with , you could create a mark after one specified customer's books are loaded(For example ,you could try $(this).attr("data-haveload","1");) after the customer's data is load, then you could use if else to avoid the second loading.
If you want to show laoding circle image until data is loaded, the load method has a callback function which will be called after the data returns.
Please refer https://api.jquery.com/load/#load-url-data-complete
Then you could write your code.
show element you want to show when the data is loading,
hide element you want to hide when the data is loading
$('#bookings td').load('conferenceBookerBookingList.aspx?conferenceBooker=' + ID + ' #bookingList',function(){ show element you want to show after the data is loaded hide element you want to hide after the data is loaded });If you still have problem,please post your code behind and the aspx page with your jquery code so that we could know more about your problem.
Best regards,
Ackerly Xu
Thursday, December 6, 2018 5:12 AM -
User1487175000 posted
Hi Ackerly Xu,
Thanks for detail answer, Yes you understand exactly what i want to do. I updated my jquery script little bit now i am only loading data once.
I still cant figure out once user load booking for one customer and then click at minus img to hide it and show again when click on the plus image.
Also need to show loading image, because some customer have alot of booking which took some couple of seconds e.g 10, 15 seconds due to large data.
Here is my markup:
Master Page:
<%@ Page Title="" Language="C#" MasterPageFile="~/statistics/Site.master" AutoEventWireup="true" CodeFile="konferensExample.aspx.cs" Inherits="statistics_konferensExample" UICulture="sv-SE" Culture="sv-SE" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server"> <asp:ScriptManager ID="ScriptManager1" runat="server" EnableScriptGlobalization="True"></asp:ScriptManager> <asp:GridView ID="gvReportWithDebit" runat="server" AutoGenerateColumns="False" Width="90%" DataKeyNames="Konferensbokare" AlternatingRowStyle-CssClass="alt" ShowFooter="True" CssClass="mGrid" OnRowDataBound="gvReportWithDebit_RowDataBound"> <Columns> <asp:TemplateField> <ItemTemplate> <img id="expand" src="../images/plus.png" class="view" data-id='<%#Eval("Konferensbokare") %>' data-status="not-loaded"/> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Konferensbokare" HeaderText="Konferensbokare"> <ControlStyle Width="100%" /> <FooterStyle HorizontalAlign="Left" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" CssClass="conferenceBooker"></ItemStyle> </asp:BoundField> <asp:BoundField DataField="Centralen" HeaderText="Centralen"> <ControlStyle Width="100%" /> <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle"></ItemStyle> </asp:BoundField> <asp:BoundField DataField="Odenplan" HeaderText="Odenplan"> <ControlStyle Width="100%" /> <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle"></ItemStyle> </asp:BoundField> <asp:BoundField DataField="Strandvägen" HeaderText="Strandvägen"> <ControlStyle Width="100%" /> <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle"></ItemStyle> </asp:BoundField> <asp:BoundField DataField="Belopp" HeaderText="Totalt"> <ControlStyle Width="100%" /> <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle"></ItemStyle> </asp:BoundField> <asp:BoundField DataField="Debiterat" HeaderText="Debiterat"> <ControlStyle Width="100%" /> <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle"></ItemStyle> </asp:BoundField> </Columns> </asp:GridView> <div> <ajax:ModalPopupExtender ID="ModalPopupExtender1" BackgroundCssClass="ModalPopupBG" runat="server" OkControlID="btnOkay" TargetControlID="lbmsgError" PopupControlID="Panel1" PopupDragHandleControlID="PopupHeader"> </ajax:ModalPopupExtender> <div id="Panel1" style="display: none;" class="popupConfirmation"> <div class="popup_Container"> <div class="popup_Titlebar" id="PopupHeader"> <div class="TitlebarLeft"> <asp:Label ID="lbMessageTitle" runat="server" Text="Error Message"></asp:Label> </div> </div> <div class="popup_Body"> <p> <asp:Label ID="lbMsgError" runat="server" Text=""></asp:Label> </p> </div> <div class="popup_Buttons"> <input id="btnOkay" value="Stänga" type="button" /> </div> </div> </div> </div> <script type="text/javascript"> $('.view').on("click", function () { var expand = $(this).attr("src"); if (expand === "../images/plus.png") { $(this).attr("src", "../images/minus.png"); var ID = encodeURI($(this).attr("data-id")); var loadStatus = $(this).attr("data-status"); if (loadStatus === "not-loaded") { $(this).closest('tr').after('<tr id="bookings"><td colspan="100%" style="padding: 5px;"></td></tr>'); $('#bookings td').load('conferenceBookerBookingList.aspx?conferenceBooker=' + ID + ' #bookingList'); $(this).attr("data-status", "loaded"); } else { alert("need to show the data already loaded"); } } else { // hide or collpase the detail row $(this).attr("src", "../images/plus.png"); // need to hide the data which is loaded } return false; }); </script> </asp:Content>
Detail Page:
<%@ Page Title="" Language="C#" MasterPageFile="~/statistics/Site.master" AutoEventWireup="true" CodeFile="conferenceBookerBookingList.aspx.cs" Inherits="statistics_conferenceBookerBookingList" UICulture="sv-SE" Culture="sv-SE" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server"> <div id="bookingList"> <asp:GridView ID="gvNestedReportWithDebit" runat="server" AutoGenerateColumns="False" Width="100%" GridLines="Horizontal" DataKeyNames="ID,kundID" OnRowDataBound="gvNestedReportWithDebit_RowDataBound"> <Columns> <asp:TemplateField HeaderText="Datum"> <ItemTemplate> <asp:HyperLink ID="btnShowBooking" runat="server" Text='<%# Bind("Datum") %>' Target="_blank"></asp:HyperLink> </ItemTemplate> <ControlStyle Width="100%" /> <FooterStyle HorizontalAlign="Left" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" /> </asp:TemplateField> <asp:BoundField DataField="Kund" HeaderText="Kund"> <ControlStyle Width="100%" /> <FooterStyle HorizontalAlign="Left" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="left" VerticalAlign="Top" /> </asp:BoundField> <asp:BoundField DataField="ProjectLeader" HeaderText="Projektledare"> <ControlStyle Width="100%" /> <FooterStyle HorizontalAlign="Left" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="left" VerticalAlign="Top" /> </asp:BoundField> <asp:BoundField DataField="Centralen" HeaderText="Centralen"> <ControlStyle Width="100%" /> <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" /> </asp:BoundField> <asp:BoundField DataField="Odenplan" HeaderText="Odenplan"> <ControlStyle Width="100%" /> <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" /> </asp:BoundField> <asp:BoundField DataField="Strandvägen" HeaderText="Strandvägen"> <ControlStyle Width="100%" /> <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" /> </asp:BoundField> <asp:TemplateField HeaderText="Debiterat"> <ItemTemplate> <asp:HyperLink ID="btnShowDebit" runat="server" Text='<%# Bind("Debiterat") %>' Target="_blank"></asp:HyperLink> </ItemTemplate> <ControlStyle Width="100%" /> <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" /> </asp:TemplateField> </Columns> </asp:GridView> </div> </asp:Content>
/Shahid
Thursday, December 6, 2018 10:36 AM -
User-893317190 posted
Hi shahid.majeed,
I find your code
$('#bookings td').load('conferenceBookerBookingList.aspx?conferenceBooker=' + ID + ' #bookingList');
Do you want to load an aspx page?
Normally, we use ajax to load json data and create html element in client side.
Below is my code.
#GridView1 table{ width:100% } </style> <script src="../Scripts/jquery-1.9.1.js"></script> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID" DataSourceID="SqlDataSource1"> <Columns> <asp:TemplateField HeaderText="Action"> <ItemTemplate> <asp:HiddenField runat="server" Value='<%#Eval("CategoryId") %>' > </asp:HiddenField> <img src="/images/icons/mini_add.png" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" /> <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" /> </Columns> </asp:GridView> <script> $(function () { //bind event to every image in the gridview $("#GridView1 img").click(function () { //get the tr element the image is in let tr = $(this).closest("tr"); // if the user want to close if ($(this).attr("src") === "/images/icons/clear.png") { //change it to the open icon $(this).attr("src", "/images/icons/mini_add.png") // ensure the nested table is loaded if (tr.next().find("table").length == 1) { //hide the table tr.next().hide(); return; } } //if the user wants to open the nested table if ($(this).attr("src") === "/images/icons/mini_add.png") { // change the img to close icon $(this).attr("src", "/images/icons/clear.png"); // ensure the table is loaded if (tr.next().find("table").length == 1) { //show the table tr.next().show(); return; } //here the table is not loaded //create an loading image var image = "<tr><td colspan='3' style='text-align:center'><img src='/images/icons/reload.png' width='200px' /></td></tr>"; //insert after the tr tr.after(image); //load the table $.ajax({ "url": "/Services/res1.ashx", "data": { id: $(this).siblings("input[type=hidden]").val() }, "dataType": "json", "success": function (data) { //create a table element var table = "<tr ><td colspan='3'><table>"; //loop through the data and insert into the created table $.each(data, function (index, ele) { var content="<tr >" content += "<td style='border:1px solid black'>" + ele.Id + "</td>"; content += "<td style='border:1px solid black'>" + ele.ProductName + "</td>"; content += "<td style='border:1px solid black'>" + ele.Description + "</td>"; content += "<td style='border:1px solid black'>" + ele.ImagePath + "</td>"; content += "<td style='border:1px solid black'>" + ele.UnitPrice + "</td>"; content += "</tr>"; table += content; }) table += "</table></td></tr>"; //remove the loading img tr.next().remove(); //insert the newly created table after the tr tr.after(table); } }) } }) }) </script> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WingtipToysConnectionString2 %>" SelectCommand="SELECT [CategoryName], [Description], [CategoryID] FROM [Categories]"></asp:SqlDataSource> </form>
Code of ashx.
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/json"; Thread.Sleep(3000); SqlConnection con = new SqlConnection(constr); using (SqlCommand com = new SqlCommand("select * from products where CategoryId=@id", con)) { com.Parameters.AddWithValue("id", context.Request["id"]);//get the CategoryId con.Open(); List<Product> list = new List<Product>(); using (SqlDataReader reader=com.ExecuteReader()) { while (reader.Read()) { Product pro = new Product(); pro.Description = reader.GetString(2); pro.Id = reader.GetInt32(0); pro.ProductName = reader.GetString(1); pro.ImagePath = reader.GetString(3); pro.UnitPrice = reader.GetDouble(4); list.Add(pro); } } ; JavaScriptSerializer serializer = new JavaScriptSerializer(); context.Response.Write(serializer.Serialize(list)); con.Close(); con.Dispose(); } } public class Product { public int Id { get; set; } public string ProductName { get; set; } public string Description { get; set; } public string ImagePath { get; set; } public double UnitPrice { get; set; } }
The result.
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 7, 2018 8:11 AM -
User1487175000 posted
Hi Ackerly Xu,
Thanks alot for your time and explanation of my problem. I update my script and its work for all my conditions.
- data should be loaded once.
- load image should be shown until data is loaded.
I tested the script and work fine. During my test for large data, then its took couple of second e.g. 20 seconds then user panic and click twice the image then its took more time and load morethan one table in the row.
Now i am wondering how i can disable all the images so user cant click them twice
Here is my updated script.
<script type="text/javascript"> $('.view').on("click", function () { var expand = $(this).attr("src"); if (expand == "../images/expand.png") { var tr = $(this).closest("tr"); if (tr.next().find("table").length == 1) { $(this).attr("src", "../images/collapse.png"); tr.next().show(); return; } var image = "<tr id='bookings'><td colspan='100%' style='padding: 10px;'><img src='../images/Vanta.gif'/></td></tr>"; tr.after(image); var url = encodeURI($(this).attr("data-id") + "&startDate=" + $('.startDate').val() + "&endDate=" + $('.endDate').val() + "&venue=" + $('.venue').val()); $('#bookings td').load('statistics/conferenceBookerBookingList.aspx?conferenceBooker=' + url + ' #bookingList'); $(this).attr("src", "../images/collapse.png"); } else { var tr = $(this).closest("tr"); $(this).attr("src", "../images/expand.png"); if (tr.next().find("table").length == 1) { tr.next().hide(); } } return false; }); </script>
Thanks a lot.Friday, December 7, 2018 10:11 PM -
User1487175000 posted
I notice more atleast for my script if seems load data overwrite to all table dynamically. What i mean if user click first + image to load the data, after that user click second + image to load the data.
Now after this if user click again first i+ image then it shows the second time loaded table :(.
Here is pictures what i mean table loaded multiple time if user click multiple time at image before data loaded.
When user click - image then one table remove
Friday, December 7, 2018 10:39 PM -
User-893317190 posted
Hi shahid.majeed,
To avoid reloading data,you could try the code below.
//bind event to every image in the gridview $("#GridView1 img").click(function () { //get the tr element the image is in let tr = $(this).closest("tr"); // check whether the data is loading, please look for your own loading image if (tr.next().find("img").attr("src") == "/images/icons/reload.png") { // do what you want to user to do alert("please be patient, the data is loading") return; }
Best regards,
Ackerly Xu
Monday, December 10, 2018 5:45 AM -
User1487175000 posted
Hi,
I did more improvement on my script, had encode problem. Some of the customer have "&" character in their name. Javascript encodeURI method did not encode "&" character. So my server side code things there are more query string parameter. I solved by manually replace & character with %26.
<script type="text/javascript"> $('.view').on("click", function () { var expand = $(this).attr("src"); var tr = $(this).closest("tr"); if (expand == "../images/expand.png") { if (tr.next().find("table").length == 1) { $(this).attr("src", "../images/collapse.png"); tr.next().show(); return; } tr.after("<tr id='bookings'><td colspan='100%' style='padding: 10px;'><img src='../images/Vanta.gif' align='middle'/> <span style='verticla-align: middle; padding-left: 5px; color: red; font-weight: bold;'> Please be patient, the data is loading</span></td></tr>"); var url = encodeURI("conferenceBooker=" + $(this).attr("data-id").replace("&", "%26") + "&startDate=" + $('.startDate').val() + "&endDate=" + $('.endDate').val() + "&venue=" + $('.venue').val()); $.get("test.aspx", url, function (data, status) { if (status == "success") { var resp = "<tr id='bookings'><td colspan='100%' style='padding: 10px;'>" + data + "</td></tr>"; tr.next().remove(); tr.after(resp); } if (status == "error") { tr.next().remove(); tr.after("<tr id='bookings'><td colspan='100%' style='padding: 10px;'>Error occure during loading data</td></tr>"); } }); $(this).attr("src", "../images/collapse.png"); } else { $(this).attr("src", "../images/expand.png"); if (tr.next().find("table").length == 1) { tr.next().hide(); } } return false; }); </script>
Monday, December 10, 2018 11:21 PM -
User1487175000 posted
Hi
Ackerly Xu,Thanks Ackerly i wondering how we can print this type of master and detail structure. Because all detail table are hiden and when i print its only print the master rows.
Is there any way to print full master detail structure?
Thanks in advance.
Thursday, December 13, 2018 9:14 PM -
User-893317190 posted
Hi shahid.majeed,
Printer only print what is shown in the window,so you should make every child page row show before print the page.
Below is my code.Please ensure every child page is loaded before call the function.
$("#Button1").click(function () {// add a button and bind click event $("img").parent().parent().next().show();// $("img").parent().parent() is used to get all the master row,
//if you couldn't get all the parent row through this way,please use another selector $(this).hide(); // .next() get all the child row and show them window.print(); //print the window return false })Best regards,
Ackerly Xu
Friday, December 14, 2018 1:20 AM -
User1487175000 posted
HI,
Windows.print() method will print complete page. However i placed my gridview inside a div with name "divPrint".
I tried to write some script, so first i get the print div element. Then i tried to get table element of my gridview which i currently hardcoded.
after that i simply print the innerHtml. Its open print window with my data. Its shows all the hiden table as well. Eventhough i havent set to show hidden table. its good i dont need to show the hidden elements :).
But problem is that main gridview does not follow the style its simply shows as string without formating.
<script type="text/javascript"> $('.print').click(function () { var parentContent = document.getElementById("divPrint"); //Step1: Need to find Table inside divPrint. var reportTable = document.getElementById("MainContent_gvConferenceBooker"); //Step2: Show all hide rows in the table var WinPrint = window.open('', '', 'letf=0,top=0,width=400,height=400,toolbar=0,scrollbars=0,status=0'); WinPrint.document.write(reportTable.innerHTML); WinPrint.document.close(); WinPrint.focus(); WinPrint.print(); WinPrint.close(); return false; }); </script>
<div id="divPrint" style="vertical-align:top; text-align:center;"> <asp:GridView ID="gvConferenceBooker" runat="server" AutoGenerateColumns="False" Width="90%" GridLines="None" DataKeyNames="Konferensbokare" ShowFooter="True" OnRowDataBound="gvConferenceBooker_RowDataBound" HorizontalAlign="Center"> <Columns> <asp:TemplateField> <ItemTemplate> <img id="expand" src="../images/expand.png" class="view" data-id='<%#Eval("Konferensbokare") %>' data-status="not-loaded" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Konferensbokare" HeaderText="Konferensbokare"> <ControlStyle Width="100%" /> <FooterStyle HorizontalAlign="Left" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" /> </asp:BoundField> <asp:BoundField DataField="Centralen" HeaderText="Centralen"> <ControlStyle Width="100%" /> <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" /> </asp:BoundField> <asp:BoundField DataField="Odenplan" HeaderText="Odenplan"> <ControlStyle Width="100%" /> <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" /> </asp:BoundField> <asp:BoundField DataField="Strandvägen" HeaderText="Strandvägen"> <ControlStyle Width="100%" /> <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" /> </asp:BoundField> <asp:BoundField DataField="Belopp" HeaderText="Totalt"> <ControlStyle Width="100%" /> <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" /> </asp:BoundField> <asp:BoundField DataField="Debiterat" HeaderText="Debiterat"> <ControlStyle Width="100%" /> <FooterStyle HorizontalAlign="Right" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" /> </asp:BoundField> </Columns> </asp:GridView> </div>
Monday, December 17, 2018 11:25 PM -
User-893317190 posted
Hi shahid.majeed,
You should also include your css style in your element to print.
For example , I use bootstrap and want to print the element dvCustomers. I should put bootstrap in the element.
<div id="dvCustomers" class="container" > <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous"> <div class="row " style="width:210mm;"> <asp:Repeater ID="Repeater1" runat="server"> <itemtemplate> <div style="text-align:center;height:16.5mm;" class="col-3"> <asp:Label ID="Label2" runat="server" Font-Size="10px" Font-Names="Palatino Linotype" Text='<%# Bind("Item_Name") %>'></asp:Label> <br /> <asp:Label ID="Label1" runat="server" Font-Size="15px" Font-Names="MRV Code39extMA" Text='<%# Bind("BC") %>'></asp:Label> </div> </itemtemplate> </asp:Repeater> </div>
Best regards,
Ackerly Xu
Tuesday, December 18, 2018 1:12 AM