Print asp.net Page with window.print

Discussion in 'ASP .Net' started by Ben, Feb 13, 2007.

  1. Ben

    Ben Guest

    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

    *** Sent via Developersdex http://www.developersdex.com ***
    Ben, Feb 13, 2007
    #1
    1. Advertising

  2. Ben

    Aidy Guest

    You can set the onLoad event of the BODY tag

    <body onLoad="window.print();">

    "Ben" <> wrote in message
    news:%...
    >
    >
    > 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
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    Aidy, Feb 13, 2007
    #2
    1. Advertising

  3. Ben

    Ben Guest

    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


    *** Sent via Developersdex http://www.developersdex.com ***
    Ben, Feb 13, 2007
    #3
  4. 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

    --
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    Short urls & more: http://ittyurl.net




    "Ben" wrote:

    > 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
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Feb 13, 2007
    #4
  5. Ben

    Ben Guest

    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



    *** Sent via Developersdex http://www.developersdex.com ***
    Ben, Feb 13, 2007
    #5
  6. Ben

    Ben Guest

    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

    *** Sent via Developersdex http://www.developersdex.com ***
    Ben, Feb 13, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGFuaWVsIEZvc3Rlcg==?=

    Window.Print in an ASP.net web page

    =?Utf-8?B?RGFuaWVsIEZvc3Rlcg==?=, Nov 1, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    3,326
    Kevin Spencer
    Nov 1, 2004
  2. Bad_Kid
    Replies:
    3
    Views:
    3,933
    Shawn H. Mesiatowsky
    Apr 7, 2005
  3. keto
    Replies:
    0
    Views:
    903
  4. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    337
    David Cournapeau
    Dec 30, 2008
  5. Pudlik, Szymon
    Replies:
    2
    Views:
    117
    Pudlik, Szymon
    Aug 24, 2005
Loading...

Share This Page