• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 474
  • Last Modified:

Javascript error

Hi all,

I have a page with a gridview on it that shows some search results. Below the gridview is a radio button list for the print options and a button. What i want to do is print the gridview but without the radiobuttonlsit and the button.

What i have done is put a Literal Control in the head section of the page and on the click of the button i set the text of the literal control with a javascript statement.
            LitJs.Text = "<script type=""text/javascript"">" & _
                        "var tbl = document.getElementById('<%=tblPrint.ClientID %>');" & _
                        "tbl.style.visibility = ""hidden"";" & _
                        "window.print();" & _
                        "tbl.style.visibility = ""visible"";" & _
                        "<" & _
                        "/" & "script>"
I have the radio button list and the button in a html table called tblPrint (see attached code). So i am trying to hide the table, print the page and then show the table.

Now when i click the button it stops on the line tbl.style.visibility = "hidden"; and gives the error "Microsoft JScript runtime error: Object required"

Thanks in advance
-M3mph15
<table style="width: 100%;" runat="server" id="tblPrint" >
            <tr>
                <td class="style1">
                    &nbsp;
                </td>
                <td class="style2">
                    &nbsp;
                    <asp:RadioButtonList ID="rblPrint" runat="server" Font-Names="Arial" 
                        Font-Size="Small" Width="160px" Height="70px">
                        <asp:ListItem>Print List</asp:ListItem>
                        <asp:ListItem>Print Details</asp:ListItem>
                        <asp:ListItem Value="Print Address Label">Print Address Label</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
                <td>
                    &nbsp;
                    <asp:Button ID="btnPrint" runat="server" Text="Print" Font-Names="Arial" />
                </td>
            </tr>
        </table>

Open in new window

0
M3mph15
Asked:
M3mph15
  • 4
3 Solutions
 
GreymanMSCCommented:
First, put the following in your page header or App_Themes/Styles/Stylesheet.css
    <style type="text/css" media="print"> 
        .ScreenOnly {display: none; visibility:hidden;}        
        .PrintOnly {display: block;}        
    </style> 
    <style type="text/css" media="screen"> 
        .PrintOnly {display: none; visibility:hidden;}        
        .ScreenOnly {display: block;}        
    </style> 

Open in new window

0
 
GreymanMSCCommented:
Then set classes as requires.  If you don't want something to appear in Printer mode, use ScreenOnly.  etc.
       <table style="width: 100%;" runat="server" id="tblPrint" >
            <tr>
                <td>
                    <!--anything here will appear on screen and printer-->  
                </td>
                <td class="ScreenOnly">
                    <!--anything here will not appear on printer-->                   
                    <asp:RadioButtonList ID="rblPrint" runat="server" Font-Names="Arial" 
                        Font-Size="Small" Width="160px" Height="70px">
                        <asp:ListItem>Print List</asp:ListItem>
                        <asp:ListItem>Print Details</asp:ListItem>
                        <asp:ListItem Value="Print Address Label">Print Address Label</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
                <td class="ScreenOnly">
                    <asp:Button ID="btnPrint" runat="server" Text="Print" Font-Names="Arial" />
                </td>
            </tr>
        </table>

Open in new window

0
 
GreymanMSCCommented:
Finally, all you need to do is print.

        LitJs.Text = "<script type=""text/javascript"">window.print();<" & "/" & "script>"

PS: the reason you were getting the error was that you were passing an asp block to something that was being posted directly to the server.  That asp block was not being processed server side.  Instead, you should have tried building the ClientID directly into the string (since it was being created server side.

Since you are filling the code in server side, you don't need to try passing an asp block to the script.

Just just build the ClientID directly into the string.

          "var tbl = document.getElementById('" & Me.tblPrint.ClientID & "');" & _

0
 
M3mph15Author Commented:
Fantastic Solution. Thankyou very much.
0
 
GreymanMSCCommented:
You are welcome.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now