print checkbox selected

How can I print in my above Javascript only those records which checkbox status is selected..

right now it just shows print preview page with all elements

Thanks!!


aspx:
                <asp:CheckBox ID="CheckBoxPrintProvider" runat="server" Text="Print this Provider" />
            <asp:HyperLink ID="HyperLink1"  runat="server" Text="Click to See a Map" NavigateUrl='<%# "http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=" + Server.UrlEncode(Eval("Address").ToString) + "," + Server.UrlEncode(Eval("City").ToString) + "," + Server.UrlEncode(Eval("State").ToString) %>'   />                                             
            </td></tr>
            </table>
            <br />
        </ItemTemplate>


 <asp:Button ID="btnPrint" runat="server" Text="Print Selected Providers" />


Javascript:
//Generating Pop-up Print Preview page
function getPrint(print_area) {

  boxes = document.getElementsByTagName('input');
   
   for(var i=0; i < boxes.length; i++)
    
         { 
   
     if (boxes[i].getAttribute('type') == 'checkbox' && boxes[i].checked)
          {

             //Creating new page
             var pp = window.open();
             //Adding HTML opening tag with <HEAD> … </HEAD> portion 
             pp.document.writeln('<HTML><HEAD><title>Print Preview</title>')
             pp.document.writeln('<LINK href=Styles.css type="text/css" rel="stylesheet">')
             pp.document.writeln('<LINK href=PrintStyle.css ' +
                                        'type="text/css" rel="stylesheet" media="print">')
             pp.document.writeln('<base target="_self"></HEAD>')

             //Adding Body Tag
             pp.document.writeln('<body MS_POSITIONING="GridLayout" bottomMargin="0"');
             pp.document.writeln(' leftMargin="0" topMargin="0" rightMargin="0">');
             //Adding form Tag
             pp.document.writeln('<form method="post">');

             //Creating two buttons Print and Close within a HTML table
             pp.document.writeln('<TABLE width=100%><TR><TD></TD></TR><TR><TD align=right>');
             pp.document.writeln('<INPUT ID="PRINT" type="button" value="Print" ');
             pp.document.writeln('onclick="javascript:location.reload(true);window.print();">');
             pp.document.writeln('<INPUT ID="CLOSE" type="button" ' +
                                        'value="Close" onclick="window.close();">');
             pp.document.writeln('</TD></TR><TR><TD></TD></TR></TABLE>');

             //Writing print area of the calling page
             pp.document.writeln(document.getElementById(print_area).innerHTML);
            // pp.document.writeln(GetCheckedValues(print_area));

             //Ending Tag of </form>, </body> and </HTML>
             pp.document.writeln('</form></body></HTML>');

         }     
    }
}

Open in new window

LVL 9
TonyRebaAsked:
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.

TonyRebaAuthor Commented:
hmm I am not really get it...

Can you explain me what is wrong in my code?
0
nmarunCommented:
I won't be able to explain your code, because I'm don't see the entire html of the grid control you have there. But I'll give you a snippet of something that worked on my box.

Arun

// aspx of the GridView
<asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="False" Width="400px">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="PrintBox" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="FirstName" HeaderText="First Name" />
        <asp:BoundField DataField="LastName" HeaderText="Last Name" />
    </Columns>
</asp:GridView>
<br/>
<asp:Button ID="Click" runat="server" OnClientClick="return openWindow();" Text="Click" />

// javascript function
<script language="javascript" type="text/javascript">
    function openWindow() {
        var pp = window.open();
        //Adding HTML opening tag with <HEAD> … </HEAD> portion 
        pp.document.writeln('<HTML><HEAD><title>Print Preview</title></HEAD>')

        //Adding Body Tag
        pp.document.writeln('<body>');
        var gridViewCtlId = '<%=gvTest.ClientID%>';
        var tableElement = document.getElementById(gridViewCtlId);
        for (var i = 1; i < tableElement.rows.length; i++) {
            var rowElem = tableElement.rows[i];
            var printBoxName = rowElem.cells[0].childNodes[0].name;
            var printBox = document.getElementsByName(printBoxName);
            if (printBox[0].checked) {
                for (var x = 1; x < rowElem.cells.length; x++) {
                    pp.document.writeln(rowElem.cells[x].childNodes[0].data);
                    pp.document.writeln(' ');
                }
                pp.document.writeln('<br/>');
            }
        }
        pp.document.writeln('</body></html>');
    }

</script>

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
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

TonyRebaAuthor Commented:
actually this is a different control....
is an asp.net 3.5 listview,, how can I do the same on that control?

<asp:ListView ID="ListView1" runat="server"   DataSourceID="AccessDataSource2" 
        Visible="False"  >

         <EmptyDataTemplate>
         <span></span>
        </EmptyDataTemplate>
    <GroupTemplate>
            <tr ID="itemPlaceholderContainer" runat="server">
                <td ID="itemPlaceholder" runat="server">
                </td>
            </tr>
        </GroupTemplate>
 
  <ItemTemplate>
        <br />
        <table id="TableItemTemplate"  runat= "server" class="itemTemplateClass">
     
 <tr>
    <td >
      <b>
            <asp:Label ID="Label1" runat="server" Text='<%# Eval("Last_Name") %>' />
            ,
             <asp:Label ID="Label2" runat="server" 
             Text='<%# Eval("First_Name") %>' />
             
               <asp:Label ID="Label3" runat="server" 
             Text='<%# Eval("Prof_Designation") %>' />
             </b>
             </td>
             </tr>
             <tr>
             <td>
            <asp:Label ID="Label4" runat="server" Text='<%# Eval("Specialty") %>' />
            <asp:Label ID="Label5" runat="server" 
            Text='<%# Eval("[Specialty2]") %>' />
            <br/>
            <asp:Label ID="Label6" runat="server" Text='<%# Eval("Address") %>' />
            <br/>
            <asp:Label ID="Label7" runat="server" Text='<%# Eval("Address2") %>' />
           <br/>
            <asp:Label ID="Label8" runat="server" Text='<%# Eval("City") %>' />
            ,
            <asp:Label ID="Label9" runat="server" Text='<%# Eval("State") %>' />
            ,
            <asp:Label ID="Label10" runat="server" Text='<%# Eval("Zip") %>' />
           <br/>
            <asp:Label ID="Label11" runat="server" Text='<%# Eval("Phone") %>' />
            <br/>
            <asp:Label ID="Label12" runat="server" Text='<%# Eval("Fax") %>' />  
            <br />
            <asp:CheckBox ID="CheckBoxPrintProvider" runat="server" Text="Print this Provider" />
            <asp:HyperLink ID="HyperLink1"  runat="server" Text="Click to See a Map" NavigateUrl='<%# "http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=" + Server.UrlEncode(Eval("Address").ToString) + "," + Server.UrlEncode(Eval("City").ToString) + "," + Server.UrlEncode(Eval("State").ToString) %>'   />                                             
            </td></tr>
            </table>
            <br />
        </ItemTemplate>

      <AlternatingItemTemplate>
      <table id="TableAlternativeItemTemplate"  runat= "server" class="alternateitemTemplateClass"   >
  <tr>
    <td >
      <b>
            <asp:Label ID="Label1" runat="server" Text='<%# Eval("Last_Name") %>' />
            ,
             <asp:Label ID="Label2" runat="server" 
             Text='<%# Eval("First_Name") %>' />
             
               <asp:Label ID="Label3" runat="server" 
             Text='<%# Eval("Prof_Designation") %>' />
             </b>
             </td>
             </tr>
             <tr>
             <td>
            <asp:Label ID="Label4" runat="server" Text='<%# Eval("Specialty") %>' />
            <asp:Label ID="Label5" runat="server" 
            Text='<%# Eval("[Specialty2]") %>' />
            <br/>
            <asp:Label ID="Label6" runat="server" Text='<%# Eval("Address") %>' />
            <br/>
            <asp:Label ID="Label7" runat="server" Text='<%# Eval("Address2") %>' />
           <br/>
            <asp:Label ID="Label8" runat="server" Text='<%# Eval("City") %>' />
            ,
            <asp:Label ID="Label9" runat="server" Text='<%# Eval("State") %>' />
            ,
            <asp:Label ID="Label10" runat="server" Text='<%# Eval("Zip") %>' />
           <br/>
            <asp:Label ID="Label11" runat="server" Text='<%# Eval("Phone") %>' />
            <br/>
            <asp:Label ID="Label12" runat="server" Text='<%# Eval("Fax") %>' />
            
            <br />
            <asp:CheckBox ID="CheckBoxPrintProvider2" runat="server" Text="Print this Provider" />
            <asp:HyperLink ID="HyperLink2"  runat="server" Text="Click to See a Map" NavigateUrl='<%# "http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=" + Server.UrlEncode(Eval("Address").ToString) + "," + Server.UrlEncode(Eval("City").ToString) + "," + Server.UrlEncode(Eval("State").ToString) %>'   />
            </td>
            </tr>
            </table>
       </AlternatingItemTemplate>
   
   <LayoutTemplate>

   
             <table id="Table1"cellpadding="0" cellspacing="0" style="border-right: #000000 thin solid; border-top: #000000 thin solid;
                                    border-left: #000000 thin solid; border-bottom: #000000 thin solid; background-color: #FFFFFF; border-width: 1px;"
                                    width="100%" align="center" class="NoDisplay">
        <tr>
            <td class="style2">
                    <b>Matching Providers </b>
                   </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
                             Sort By: 
                                 <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Sort" CommandArgument="Last_Name">Name</asp:LinkButton>&nbsp;Sort 
                By:   
                                 <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Sort" CommandArgument="City">City</asp:LinkButton>
                <br />
            </td>
            <td>
                        <%--<asp:Button ID="PrintButton" runat="server" Text="Print Selected Providers"  />--%>
                        <asp:Button ID="btnPrint" runat="server" Text="Print Selected Providers" />
                   &nbsp;
                   <asp:CheckBox ID="chkSelectAll" runat="server" Text=" Select All For Printing" AutoPostBack="true" OnClick="selectAll(this)" />
                                    </td>
        </tr>
    </table>
    
                        <table ID="groupPlaceholderContainer" runat="server" border="1" 
                            style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
                            <tr ID="groupPlaceholder" runat="server">
                            </tr>
                        </table>
                                                
                    </td>
                </tr>
                <tr id="Tr2" runat="server">
                    <td id="Td2" runat="server" 
                        style="text-align: center;background-color: #5D7B9D;font-family: Verdana, Arial, Helvetica, sans-serif;color: #FFFFFF">
                        <br />
                        <asp:DataPager ID="DataPager1" runat="server" PageSize="12">
                            <Fields>
                                <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                                    ShowLastPageButton="True" />
                            </Fields>
                        </asp:DataPager>
                    </td>
                </tr>
            </table>
             
        </LayoutTemplate>
        
    </asp:ListView>

Open in new window

0
nmarunCommented:
That should be fine. All you need to do is to modify the indexes in the javascript function. I'm afraid I won't be able to give you the actual JS function as I don't have the datasource needed to populate the listbview.

Arun
0
TonyRebaAuthor Commented:
ok testing now
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.