Solved

how do I print a web page

Posted on 2015-01-04
7
138 Views
Last Modified: 2015-01-28
have the following code  two lines give error
downloaded this code from web appears to to straight forward but!!!!!!!
  Protected Sub PrintCurrentPage(ByVal sender As Object, ByVal e As EventArgs)
        CustomersGridView.PagerSettings.Visible = False
        CustomersGridView.DataBind()
        Dim sw As New StringWriter()
        Dim hw As New HtmlTextWriter(sw)

        CustomersGridView.RenderControl(hw)     'this line errors says customergridview must be inside of form tag
                                                                               'web code obviously has form tag see below
        Dim gridHTML As String = sw.ToString().Replace("""", "'").Replace(System.Environment.NewLine, "")
        Dim sb As New StringBuilder()
        sb.Append("<script type = 'text/javascript'>")
        sb.Append("window.onload = new function(){")
        sb.Append("var printWin = window.open('', '', 'left=0")
        sb.Append(",top=0,width=1000,height=600,status=0');")
        sb.Append("printWin.document.write(""")
        sb.Append(gridHTML)
        sb.Append(""");")
        sb.Append("printWin.document.close();")
        sb.Append("printWin.focus();")
        sb.Append("printWin.print();")
        sb.Append("printWin.close();};")

        sb.Append("</script>")                 'this line gives error  Statement cannot appear within a method body. End of                                                            'method assumed.

        ClientScript.RegisterStartupScript(Me.GetType(), "GridPrint", sb.ToString())
        CustomersGridView.PagerSettings.Visible = True
        CustomersGridView.DataBind()
    End Sub

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="head1" runat="server">
    <title> TIME SHEET </title>      
           <link rel="stylesheet" type="text/css" href="wages.css"/>            
</head>

<body>
   
  <form id="form1" runat="server">
   <h1>Monthly Hours Sheet</h1>
    <div id="div4">
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
    </div>

    <div id="div1">

    <asp:Label
    ID="Labelex1"
    runat="server"      
    Text="Time is entered on a 24 hour clock, use 15 min increments. It is simple, you come at 8:00, 4 hours later which is 12.">    
    </asp:Label>
     <br />

     <asp:Label
    ID="Labelex2"
    runat="server"        
    Text="You go to lunch for an hour, it is now 13; work 4 more hours it is 17:00. You've been here 9 hours and worked 8.">    
    </asp:Label>
       </div>
     
     <div id="div2">
     <asp:Label
        ID="Label1"
        runat="server"  
        Text="Enter Your Soc. Sec Number ">
        </asp:Label>    

     <asp:TextBox
        ID="TextBoxSocSec"
        Text=""
        AutoPostBack="False"                  
        runat="server">
        </asp:TextBox>

         <asp:Button
        ID="Button1"  
        runat="server"  
             
        Text="Submit Soc. Sec #"        
        OnClick= "btnSubmit_Click"/>
        <br />
          <asp:Label ID="Labelname" runat="server" Text="Name "></asp:Label>  
                 <asp:TextBox ID="TextBoxName" runat="server" ReadOnly="True"></asp:TextBox>  

         <asp:Label ID="LabelSymCode" runat="server" Text="Symitar Code "></asp:Label>
                 <asp:TextBox ID="TextBoxCode" runat="server" ReadOnly="True"></asp:TextBox>

         <asp:Label ID="LabelTD" runat="server" Text="Todays Date "></asp:Label>
                 <asp:TextBox ID="TextBoxNow" runat="server" ReadOnly="True"></asp:TextBox>
          <br />  
       
     </div>
      <asp:Label ID="Labelex3" runat="server" Text="You will see listing of Accumulated hours and used hours as of the first of month. "></asp:Label>
        <asp:Label ID="Labelex4" runat="server" Text="All calculations and recalculations are done from First of Month Numbers. "></asp:Label>
        <br />
         
         <asp:Label ID="LabelPtoAccum" runat="server" Text="Pto Accummulated     "></asp:Label>  
                 <asp:TextBox ID="TextPtoAccum" runat="server" ReadOnly="True"></asp:TextBox>  

         <asp:Label ID="LabelPtoFM" runat="server" Text="Pto 1st of Month     "></asp:Label>
                 <asp:TextBox ID="TextPtoFM" runat="server" ReadOnly="True"></asp:TextBox>

         <asp:Label ID="LabelPtoUsed" runat="server" Text="Pto Currently Claimed     "></asp:Label>
                 <asp:TextBox ID="TextPtoUsed" runat="server" ReadOnly="True"></asp:TextBox>
          <br />  
          <asp:Label ID="LabelBorrowAccum" runat="server" Text="Pto Allow  Borrow  "></asp:Label>  
                 <asp:TextBox ID="TextBorrow" runat="server" ReadOnly="True"></asp:TextBox>

         <asp:Label ID="LabelBorrowFM" runat="server" Text="Pto Borrowed 1st of Month  "></asp:Label>
                 <asp:TextBox ID="TextBorrowFM" runat="server" ReadOnly="True"></asp:TextBox>

         <asp:Label ID="LabelBorrowUsed" runat="server" Text="PTO Borrowed  "></asp:Label>  
                 <asp:TextBox ID="TextBorrowUsed" runat="server" ReadOnly="True"></asp:TextBox>    
     
        <br />

         <asp:Label ID="LabelSickAccum" runat="server" Text="Sick Accummulated  "></asp:Label>  
                  <asp:TextBox ID="TextSickAccum" runat="server" ReadOnly="True"></asp:TextBox>  

         <asp:Label ID="LabelSickFM" runat="server" Text="Sick 1st of Month  "></asp:Label>
                 <asp:TextBox ID="TextSickFM" runat="server" ReadOnly="True"></asp:TextBox>

         <asp:Label ID="LabelSickUsed" runat="server" Text="Sick Currently Claimed  "></asp:Label>  
                 <asp:TextBox ID="TextSickUsed" runat="server" ReadOnly="True"></asp:TextBox>
        <br />    
       
     
     

    <div id="div5">
       
  <asp:gridview id="CustomersGridView"  
       autogeneratecolumns="false"
        autogenerateeditbutton="false"  
       
               
        runat="server">  
        <columns>
       
          <asp:boundfield datafield="WORKDATE"
            readonly="true"      
            headertext="DATE"/>
           
            <asp:boundfield datafield="DAYOFWEEK"
            readonly="true"      
            headertext="DAY IN WEEK"/>

             <asp:TemplateField HeaderText="Authorize">        
               <ItemTemplate>          
                  <asp:TextBox ID="tbauthorizedcode" runat="server" Width="80%"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>

           <asp:TemplateField HeaderText="IN">          
             <ItemTemplate>          
             <asp:DropDownList ID="ddlin" runat="server"  >                                      
                 </asp:DropDownList>  
                   </ItemTemplate>                            
             </asp:TemplateField>    

        <asp:TemplateField HeaderText="OUT">
           <ItemTemplate>          
             <asp:DropDownList ID="ddlout" runat="server"  >              
                 </asp:DropDownList>  
           </ItemTemplate>          
        </asp:TemplateField>

        <asp:TemplateField HeaderText="LUNCH">
           <ItemTemplate>          
             <asp:DropDownList ID="ddllunch" runat="server"  >              
             </asp:DropDownList>  
           </ItemTemplate>          
        </asp:TemplateField>

         <asp:TemplateField HeaderText="LUNCH IN">
           <ItemTemplate>                
             <asp:DropDownList ID="ddllunchin" runat="server"  >              
             </asp:DropDownList>    
            </ItemTemplate>        
         </asp:TemplateField>

           <asp:TemplateField HeaderText="Pto Detail">
             <ItemTemplate>
                <asp:DropDownList ID="ddlptodetail" runat="server">                
                </asp:DropDownList>              
             </ItemTemplate>
           </asp:TemplateField>

            <asp:TemplateField HeaderText="Pto PAID">
               <ItemTemplate>          
                   <asp:TextBox ID="tbptopaid" runat="server" Width="80%"></asp:TextBox>
               </ItemTemplate>  
            </asp:TemplateField>

           

           <asp:TemplateField HeaderText="COMP Pto">          
            <ItemTemplate>          
               <asp:DropDownList ID="ddlcomppto" runat="server">                
               </asp:DropDownList>              
            </ItemTemplate>
           </asp:TemplateField>

             <asp:TemplateField HeaderText="COMP PAID">        
               <ItemTemplate>          
                  <asp:TextBox ID="tbcomppaid" runat="server" Width="80%"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>

           

         </columns>
      </asp:gridview>
       
 </div>
  <div id="div6">        
        <br />
           
     <asp:label id="Message"
        forecolor="Red"
        runat="server"/>
         <br />
  <asp:Button ID="BTNUPDATE" runat="server" Text="UPDATE" Onclick="btnUpdate_Click"/>  
        <br />
      <asp:Button ID="Button2" runat="server" Text="Button" Onclick="btnprint_Click" />
    </div>    
  </form>  
</body>
</html>
0
Comment
Question by:javagair
  • 4
  • 3
7 Comments
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 total points
ID: 40530904
The easiest way to print the page is to add a javascript button
<input type="button" value="Print" onclick="window.print();">

Open in new window


To open in a new windows with a print friendly version use
<input type="button" value="Print" onclick="window.open('YourPrintPage.aspx')" />

Open in new window

and add the following to the end of YourPrintPage
<script language="javascript">
window.print();
</script>

Open in new window


For more detail on this have a look at this tutorial
http://www.beansoftware.com/ASP.NET-Tutorials/Printing-Reporting.aspx
0
 

Author Comment

by:javagair
ID: 40531752
using windows print I get everything and prints on two pages
the windows.open prints all of the page except the gridview which is the only thing I wanted to print.

suggestions?

gair
0
 
LVL 23

Expert Comment

by:Michael74
ID: 40532373
I see what you are doing now. The author of original code recommend the following to your issue.

 The problem is that the .net compiler thinks that the control is not added to the form and hence it throws this error even if your GridView control is inside a form with runat = “server”

To resolve this tell the compiler that the control is rendered explicitly by overriding the VerifyRenderingInServerForm event.

http://www.aspsnippets.com/Articles/Exception---Control-GridView1-of-type-GridView-must-be-placed-inside-a-form-tag-with-runatserver.aspx
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:javagair
ID: 40533794
1. compiler will not allow overrides in function line.
2. nothing in any of these articles tells me how to call the verifyrenderinginserverform function and when or where to call it.

thanks
gair
0
 
LVL 23

Expert Comment

by:Michael74
ID: 40534350
Controls that are required to be inside <form runat=server> tags can call this method before they render so that an error message is shown if they are placed outside the tags.
http://msdn.microsoft.com/en-us/library/system.web.ui.page.verifyrenderinginserverform(v=vs.110).aspx

It gets placed on the code behind for the page
 public partial class MyPage : System.Web.UI.Page
 {
    public override void VerifyRenderingInServerForm(Control control)
    {         
    }
}

Open in new window

0
 

Author Comment

by:javagair
ID: 40576416
changed the entire idea for printing added this code which is simple and prints perfectly
do not print anything but panels and things work wonderfully

<script type="text/javascript">

    function PrintGridData() {
        var prtPanel1 = document.getElementById('<%=panel1.id %>');
        prtPanel1.border = 0;
        var prtwin = window.open('', 'PrintPanel1', 'left=100,top=100,width=1000,height=1000,tollbar=0,scrollbars=1,status=0,resizable=1');
        prtwin.document.write(prtPanel1.outerHTML);
        prtwin.document.close();
        prtwin.focus();
        prtwin.print();
        prtwin.close();
    }
    </script>
0
 

Author Closing Comment

by:javagair
ID: 40576418
I am accepting because it got me to thinking of what type of java script I would need to make the page print
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now