Print asp.net Page with window.print

  • Thread starter Thread starter Ben
  • Start date Start date
B

Ben

Hi All,
I have a form which requires input, has conditional server side
validations and print functionality. There is a print button which does
client side validation & then custom validation, if all passes, prints
the form with the following code:

Response.Write("<script language='javascript'>");
Response.Write("window.print()");
Response.Write("</script>");

My problem is that this causes the form to lose its style classes as
this is written at the top before head or any other html tags are
rendered.I do have media="print" for the style sheets.

Is there a better way to do this ?I have to get this fixed by end of the
day today, so any help is greatly appreciated.

Thanks,
Ben
 
I dont think that would work.

Let me explain a sample scenario :

Step 1: Form loads with empty textboxes etc..
Step 2: User enters 8 & hits print- some client side validation go off
Step 3: User enters 9 and hits print - some server side validation goes
off
Step 4: All input is valid and now the form shud open print dialogue
box.


So I dont need the window.print to be called everytime but only after
all client and server validations are done, hence the code behind
approach.


Please help.

Thanks,
Ben
 
Why don't you put the code with the window.print call at the bottom of the
page with a defer="defer" attribute in the script tag?
Peter
 
I tried adding the defere="true" attribute in my response.write like so:

protected void PrintPage()
{
this.rfv1.Validate();
this.rfv2.Validate();
if ((this.rfv1.IsValid) && (this.rfv2.IsValid) ||
(this.rfv3.IsValid))
{
Response.Write("<script defer = 'true'
language='javascript'>");
Response.Write("window.print();");
Response.Write("</script>");
}
}

but it does the same thing. I also tried having an anchor with
onclick=window.print and doing "document.all['ref1'].click();" in the
response.write but that also doesnt help.

Thanks for all your inputs.
Ben
 
Ok. Fixed .

I did Page.RegisterClientScript instead and that fixed the problem.

String s= "<script language='javascript'>";
scriptText += "window.print();";
scriptText += "</script>";
Page.RegisterClientScriptBlock("Print", s);

Thanks all for your help, much appreciated.
Ben
 
Back
Top