Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
This is the javascript code that I use

JavaScript
function Print() {
    var prntData = document.getElementById('Table1');
    var mywindow = window.open('', 'printer', 'height=400,width=600');
    mywindow.document.write('<html><head><title></title>');
    mywindow.document.write('</head><body style="direction:rtl;"><pre>');
    mywindow.document.write(prntData.innerHTML);
    mywindow.document.write('</body></html>');
    mywindow.document.close();
    mywindow.print();
}


This is my html table
ASP.NET
<table style="width: 100%;" class="MemberTbl"  runat="server" id="Table1">


I noticed that when I created the table myself and print it the code works but when I create it dynamically with a runat=server, on print button click it only brings up an empty white box.

How can a achieve this

Any help is welcome
Posted
Updated 20-Jan-14 2:38am
v2
Comments
TorstenH. 20-Jan-14 8:39am    
I changed Tag "Java" to "JavaScript".
That might attract someone who knows.
Member 10395722 21-Jan-14 1:08am    
I think I selected the wrong one, apologise for that

Hello,
When you create it dynamically with a runat=server, the id of the element will change.
It will be something like this : "containerId_containerId_Table1".
So, in your print function, your first line of code will return nothing.
I hope it's clear.

You can use a library like jQuery to get your HTML element with this method :
Attribute Ends With Selector [name$="value"]
 
Share this answer
 
v2
 
Share this answer
 
v2
if runat=server for an html control,it becomes an asp.net control so use jquery to get the table ID
i.e var prntData = $("#<%=Table1.ClientID%>");
 
Share this answer
 
XML
function Print() {
    var prntData = document.getElementById('Table1');
    var mywindow = window.open('', 'printer', 'height=400,width=600');
    mywindow.document.write('<html><head><title></title>');
    mywindow.document.write('</head><body style="direction:rtl;"><pre>');
    mywindow.document.write(prntData.innerHTML);
    mywindow.document.write('</body></html>');
    mywindow.document.close();
    mywindow.print();
}



I still used this code, but the way I loaded the data into the table is differnt. Instead of creating the rows and cells using HtmlGenericControl I just used document.write
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900