Printing All My Pages
-
My asp .net project has 18 pages. A user can fill out information on each of these pages. The information is stored in a SQL Server database. The administrator would like to click one button and automatically have all 18 pages print. How can this be done? Thanks in advance
-
My asp .net project has 18 pages. A user can fill out information on each of these pages. The information is stored in a SQL Server database. The administrator would like to click one button and automatically have all 18 pages print. How can this be done? Thanks in advance
Hi there. I am guessing a nicely formatted report that incorporates data from each of your 18 pages would probably be more desirable than a raw printout of each of 18 pages? or not? I would look to make an administrative reporting .aspx page that lays out the report data in a desirable format. Perhaps use Crystal Reports or SQL Reporting Services, or some other report tool to help. But if you are really interested in printing 18 raw .aspx pages, there would be a way to automate that. You could use the System.Net.WebClient class to retrieve html from a given Url, then parse that with a regular expression to retrieve <body> contents. Below is an example - you can see the potential issues with relative image and link locations, but if all pages were within the same application, that probably wouldn't be a problem. This is pretty quick and dirty too - if I were doing this for real, I might want to set up multiple threads to perform the url scraping. Again, all that said, I think I would first look at doing a nice administrative report layout.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Text.RegularExpressions" %><script runat="server">
string ScrapePageForBody(string sUrl)
{
// connect to the given URL, download the
// html, and parse it for the <body> contentsWebClient client = new WebClient(); Byte\[\] buffer = client.DownloadData(sUrl); string sOutput = (new UTF8Encoding()).GetString(buffer); Regex rBody = new Regex(@"<body.\*>(?<content>\[\\s\\S\\w\\W\]\*)</body>", RegexOptions.IgnoreCase); Match m = rBody.Match(sOutput); if (m.Success) return m.Groups\["content"\].Value; else return "";
}
void Page_Load(object o, EventArgs e)
{
Page1Contents.Text = ScrapePageForBody("http://www.yahoo.com");
Page2Contents.Text = ScrapePageForBody("http://www.CodeProject.com");
}</script>
<html>
<head>
<style>
div.sample {border: 1px solid #CCCCCC; background-color: #EFEFEF;
margin-top: 4px; margin-left: 10px; margin-right: 10px; margin-bottom: 20px;
width:100%;
padding: 10px;
}
</style>
</head><body>
<form runat="server"><h2> Page 1: www.yahoo.com</h2> <div class="sample">
-
My asp .net project has 18 pages. A user can fill out information on each of these pages. The information is stored in a SQL Server database. The administrator would like to click one button and automatically have all 18 pages print. How can this be done? Thanks in advance
-
Hi there. I am guessing a nicely formatted report that incorporates data from each of your 18 pages would probably be more desirable than a raw printout of each of 18 pages? or not? I would look to make an administrative reporting .aspx page that lays out the report data in a desirable format. Perhaps use Crystal Reports or SQL Reporting Services, or some other report tool to help. But if you are really interested in printing 18 raw .aspx pages, there would be a way to automate that. You could use the System.Net.WebClient class to retrieve html from a given Url, then parse that with a regular expression to retrieve <body> contents. Below is an example - you can see the potential issues with relative image and link locations, but if all pages were within the same application, that probably wouldn't be a problem. This is pretty quick and dirty too - if I were doing this for real, I might want to set up multiple threads to perform the url scraping. Again, all that said, I think I would first look at doing a nice administrative report layout.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Text.RegularExpressions" %><script runat="server">
string ScrapePageForBody(string sUrl)
{
// connect to the given URL, download the
// html, and parse it for the <body> contentsWebClient client = new WebClient(); Byte\[\] buffer = client.DownloadData(sUrl); string sOutput = (new UTF8Encoding()).GetString(buffer); Regex rBody = new Regex(@"<body.\*>(?<content>\[\\s\\S\\w\\W\]\*)</body>", RegexOptions.IgnoreCase); Match m = rBody.Match(sOutput); if (m.Success) return m.Groups\["content"\].Value; else return "";
}
void Page_Load(object o, EventArgs e)
{
Page1Contents.Text = ScrapePageForBody("http://www.yahoo.com");
Page2Contents.Text = ScrapePageForBody("http://www.CodeProject.com");
}</script>
<html>
<head>
<style>
div.sample {border: 1px solid #CCCCCC; background-color: #EFEFEF;
margin-top: 4px; margin-left: 10px; margin-right: 10px; margin-bottom: 20px;
width:100%;
padding: 10px;
}
</style>
</head><body>
<form runat="server"><h2> Page 1: www.yahoo.com</h2> <div class="sample">
Thanks for the code. My pages are formatted to print nicely from the browser so I think the automated way will work well... I think. The pages get their data from a database and the record in the database retrieved is determined by the value of a variable in the Load event in each page. Will I beable to assign a value to that variable when printing? Also, I'm using VB so I converted your sample code (not the script part), and it seems to work fine. The part I don't understand is where to put the script code into the project. I'm kind of new at asp. When I'm in the form designer and click on the Source tab does it go in there some where? Thanks again for your help!
-
Thanks for the code. My pages are formatted to print nicely from the browser so I think the automated way will work well... I think. The pages get their data from a database and the record in the database retrieved is determined by the value of a variable in the Load event in each page. Will I beable to assign a value to that variable when printing? Also, I'm using VB so I converted your sample code (not the script part), and it seems to work fine. The part I don't understand is where to put the script code into the project. I'm kind of new at asp. When I'm in the form designer and click on the Source tab does it go in there some where? Thanks again for your help!
dptalt wrote:
The pages get their data from a database and the record in the database retrieved is determined by the value of a variable in the Load event in each page. Will I beable to assign a value to that variable when printing?
Well, if you build on the sample I showed you, you can create your own URL strings to incorporate a variable like that as part of the QueryString. That would be one way to pass a variable to your individual pages.
dptalt wrote:
The part I don't understand is where to put the script code into the project. I'm kind of new at asp. When I'm in the form designer and click on the Source tab does it go in there some where?
Okay - this depends on whether your .aspx pages are using code-behind or not. If they are using code-behind, then for each .aspx page you'll have, you'll have a corresponding .vb page that contains the programming code. If you are not using code-behind, then you can put the programming code right on your .aspx page in a
<script runat="server" >
block. -- modified at 11:42 Wednesday 22nd February, 2006 -
dptalt wrote:
The pages get their data from a database and the record in the database retrieved is determined by the value of a variable in the Load event in each page. Will I beable to assign a value to that variable when printing?
Well, if you build on the sample I showed you, you can create your own URL strings to incorporate a variable like that as part of the QueryString. That would be one way to pass a variable to your individual pages.
dptalt wrote:
The part I don't understand is where to put the script code into the project. I'm kind of new at asp. When I'm in the form designer and click on the Source tab does it go in there some where?
Okay - this depends on whether your .aspx pages are using code-behind or not. If they are using code-behind, then for each .aspx page you'll have, you'll have a corresponding .vb page that contains the programming code. If you are not using code-behind, then you can put the programming code right on your .aspx page in a
<script runat="server" >
block. -- modified at 11:42 Wednesday 22nd February, 2006 -
I'm using a code-behind file. If I cut and paste your script example into the code window those squiggly lines appear indicating there are syntax errors. Is there a special place I need to put the script?
Hi there. Your code-behind file is defining a class (and remember, what I had was in C#, not VB). You could copy/paste the ScrapeBodyOfPage function pretty much as is, but you probably already have a Page_Load method in your class. So you would adapt what I wrote to your existing function (most likely). You would also use
imports
statements in your code-behind, rather than the<%@ Import %>
directive that appears in the sample .aspx page I sent you. -
Hi there. Your code-behind file is defining a class (and remember, what I had was in C#, not VB). You could copy/paste the ScrapeBodyOfPage function pretty much as is, but you probably already have a Page_Load method in your class. So you would adapt what I wrote to your existing function (most likely). You would also use
imports
statements in your code-behind, rather than the<%@ Import %>
directive that appears in the sample .aspx page I sent you.Hi. The part I don't understand is where to put the script code into the project. The part that start out with text . When I'm in the form designer and click on the Source tab does it go in there some where? And if so where does it go? Or since I have a code-behind file can I put the script code there instead? And if so do I make a new function for it? Also, where in your example does it do the printing? Thanks for all your help!