0

Somebody help me please.. i have make a research on how to retrieve query string using java script but it seems not success. I need to open window print view for a form (different web form) when user click on button that is placed in grid view. Every row in grid view have its own print button as its have different id as the form data are retrieve from id(primary key in database which refer to trip id. Below is my code for java script. The print window able to open but the form data is not retrieve.

 <script type="text/javascript">
   function printExternal() {
   var data='<%=this.Request.QueryString["tripid"]%>';
   var printWindow = window.open('PrintBusinessTrip.aspx?tripid=' + data, 'Print', 'left=200, top=200, width=950, height=500, toolbar=0, resizable=0');
   printWindow.addEventListener('load', function () {
   printWindow.print();
   printWindow.close();
   }, true);
   }
 </script>

Here is the button,

<asp:Button ID="sendbtn" runat="server"  OnClientClick="printExternal();"     PostBackUrl='<%#"~/PrintBusinessTrip.aspx?tripid="+Eval("tripid")%>'  Text="Print" />

It only success when I hard code the id like this,

   var printWindow = window.open('PrintBusinessTrip.aspx?tripid=102' + data, 'Print', 'left=200, top=200, width=950, height=500, toolbar=0, resizable=0');

I really do appreciate any help.. because i am new to java script and asp.net. Sorry for my English.

GridView where the button placed

print view

aniys
  • 1
  • 1
  • 1
  • 4
  • 4
    Possible duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Vinod Louis Apr 05 '17 at 06:29
  • Your code seems to work well in my new WebForm project. Please, can you post the complete code which doesn't work? – Omar Muscatello Apr 05 '17 at 06:37
  • actually the code works well but the output not display what exactly i want.. i have add the image for the print view as you can see from the url there is no query string id read. Is my javascript code is correct as for retrieve the query string? – aniys Apr 05 '17 at 07:01

1 Answers1

0

The problem is in the printExternal Javascript function. You shouldn't get the tripid using the query-string because it doesn't change for every row of your table.

Instead, you could pass the tripid as parameter of the printExternal function from every button.

<asp:Button ID="sendbtn" runat="server" OnClientClick='<%# Eval("tripid", "printExternal({0})") %>' Text="Print" />

or simply

<button type="button" onclick="printExternal(<%# Eval("tripid") %>);">Print</button>

The Javascript function should look like this:

function printExternal(tripid) {
    var printWindow = window.open('PrintBusinessTrip.aspx?tripid=' + tripid, 'Print', 'left=200, top=200, width=950, height=500, toolbar=0, resizable=0');
    printWindow.addEventListener('load', function () {
        printWindow.print();
        printWindow.close();
    }, true);
}
Omar Muscatello
  • 1,256
  • 14
  • 25
  • May i ask one question? If you could help. Why print window is not working on IE. But for Chrome and Firefox just fine.. Is it because i used add Eventlistener? – aniys Apr 06 '17 at 05:55