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

LVL 6
M3mph15Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.