分类 ‘C# 3.0’ 的归档

ASP.NET print HTML report in A4 Paper

work in ASP.net project again. currently working on report module.  I need to print all members notification letter in once.  the best way for web printing of course is generate PDF file, but my company no provide me PDF library, and I can’t find any free PDF library for ASP.net. So, the only method is use the javaScript, Window.print() function to print my report.

I used repeater web control to generate the report and in HTML form.

I need fix all in A4 size, if my repeater generate 4 member notification  letter, then I need exactly print 4 letters in A4 paper.

0

But you may face this problem:

1

when the first page content cannot fix in entire A4 paper, the second page content will move up. or maybe :

2

the first page content too long, and extend to second page.

that’s not really possible to control,  because it depending on content. But I found out the IE8( or IE7) have a function call “shrink-to-fit”that can solve my problem.

I need to explain my situation,

1st, I build management system for a company but not create website for company. So, I can force my client using which browser to operate the system. 2nd, client no require cross browser capability . 3rd, my company no provide any printing library for me.

first of all, you need write all the page content(report content) within the <div> , 210mm X 297mm is A4 paper size.

3

2nd, setting IE:

left click on “Page Setup… (menu item)”

image

change the page size to A4 size.

image

Tick the Enable Shink-to-Fit

image

Done. Now you can print your report nicely. kindly Email me, if you face any problem in ASP.net printing issue.

you can download my solution file here.

C# 3.0 Extension methods

Extension methods is a new features of C# 3.0, it allow developers to add new methods to a existing type(string, int, object and etc) and without change the definition of the original type.

Simple Extension Method Example:

let said we wanted to checking to see whether a int is odd? normally we can using function(make it reusable).

protected void Page_Load(object sender, EventArgs e)
{
 int x=3;
 Response.Write(funcIsOdd(x));
 }

 protected bool funIsOdd(int i)
 {
 bool ret;
 if (i % 2 != 0)
 ret= true;
 else 
ret= false;
 return ret;
}

by using the extension method in C# 3.0, we can instead add a IsOdd () method onto the int class itself. we define a new static class with a static method IsOdd() like below:

public static class Class1
{
    public static bool IsOdd(this int i)
    {
        if (i % 2 != 0)
            return true;
        else
            return false;
    }

Notice the argument list of these function, especially keyword this. Keyword this followed by type tells to compilator that this method is applied to specified type.

after I defined the extension method, when hit the “.” keyword on a int variable, my extension methods will now show up in the IntelliSense drop-downlist:

printsceen1

加关注

Get every new post delivered to your Inbox.