Need to prompt client with Print Dialog from IE; need some format to print web form content

Have you ever been on a website, and it will have a link or button to "Print Something", and when pressed, the Windows print dialog will automatically appear?  Well that is what I need to do.  I am guessing it must be some Javascript or something in .NET to do this.  So here are my questions.

1. How do I show the user a print dialog box from IE when they click on a link or button on my ASP.NET page?

2. The content I want to print is just a long set of instructions in an ASP.NET text box.  What would be the best product/format to put this in that can be printed? (i.e. notepad, some .NET tool, Microsoft Word, Adobe, a custom report, etc.)

Thanks!


Thank you,

0
atconway
12/21/2007 2:24:48 PM
asp.net.web-forms 93655 articles. 5 followers. Follow

12 Replies
939 Views

Similar Articles

[PageSpeed] 40

 You can use the javascript window.print method.


Website Design Darlington - http://mdssolutions.co.uk
http://lessthandot.com - Experts, Information, Ideas & Knowledge
http://aspnetlibrary.com - An online resource for professional ASP.NET developers


Please remember to click "Mark as Answer" on this post if it helped you

0
ca8msm
12/21/2007 2:34:17 PM

Try to add the below code at page_load event

Button1.Attributes.Add("onclick","window.print();return false;");

Where Button1 is the id of the button when being clicked it prints the window.

as for the second question, You can format the text in Word document, read it on page_load and write it to the window.

HC 


Haissam Abdul Malak
MCAD.NET
| Blog |
0
Haissam
12/21/2007 2:44:25 PM

Hi,

Do the following :

 Step 1: Add a class called PrintHelper to your project :

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.IO;

using System.Text;

using System.Web.SessionState;

public class PrintHelper

{

public PrintHelper()

{

}public static void PrintWebControl(Control ctrl)

{

PrintWebControl(ctrl,
string.Empty);

}

public static void PrintWebControl(Control ctrl, string Script)

{

StringWriter stringWrite = new StringWriter();

System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);

if (ctrl is WebControl)

{

Unit w = new Unit(100, UnitType.Percentage);((WebControl)ctrl).Width = w;

}

Page pg = new Page();

pg.EnableEventValidation = false;if (Script != string.Empty)

{

pg.RegisterStartupScript(
"PrintJavaScript", Script);

}

HtmlForm frm = new HtmlForm();

pg.Controls.Add(frm);

frm.Attributes.Add("runat", "server");

frm.Controls.Add(ctrl);

pg.DesignerInitialize();

pg.RenderControl(htmlWrite);

string strHTML = stringWrite.ToString();

HttpContext.Current.Response.Clear();

HttpContext.Current.Response.Write(strHTML);

HttpContext.Current.Response.Write("<script>window.print();</script>");

HttpContext.Current.Response.End();

}

}

Step 2:

Add a button and on its click event call the PrintWebControl passing the Textbox.

protected void Button1_Click(object sender, EventArgs e)

{

PrintHelper.PrintWebControl(TextBox1);

}

 

HTH,
Suprotim Agarwal

-----
http://www.dotnetcurry.com
-----


ASP.NET Grid With Excel Capabilities | 27 GridView Tips & Tricks | Hosting your site? | SQL Server Tips
0
Suprotim
12/21/2007 2:53:51 PM

I guess that would require that the client has to have Microsoft Word installed or is it the Web Server that would need Word installed?  Also, that would require the Interop .dlls correct for Word to interact with it?

Also, I am not putting everything together because the description was a bit too vague.  How specifically do I hook up the content to the button or link?  Right now the content is just a part of the entire page.  I guess I need to port just the content to its own page and then add the attributes to the button?

I am going to need a little more detail please to connect all of the dots.

 Thanks!


Thank you,

0
atconway
12/21/2007 2:55:35 PM

Hi,

Did you try out the code just posted. Any issues with that?

 

HTH,
Suprotim Agarwal

-----
http://www.dotnetcurry.com
-----


ASP.NET Grid With Excel Capabilities | 27 GridView Tips & Tricks | Hosting your site? | SQL Server Tips
0
Suprotim
12/21/2007 3:27:28 PM

What is the value of the 'PrintJavaScript' string; otherwords, what is the JavaScript script for 'PrintJavaScript' for this:

pg.RegisterStartupScript("PrintJavaScript", Script);

 


Thank you,

0
atconway
12/21/2007 4:20:27 PM

 Hi,

The Page.RegisterStartupScript adds a client side script block to the response. It take two parameters, a key and a script. The "PrintJavaScript" that you see is a unique key that identifies a script block. You can call it "actonway" if you want :)

One note of caution:

Page.RegisterStartupScript is an obsolete method. The new way to call it would be as follows :

ClientScript.RegisterStartupScript(this.GetType(), "SomeKeyName", Script);

http://msdn2.microsoft.com/en-us/library/asz8zsxy.aspx

 

HTH,
Suprotim Agarwal

-----
http://www.dotnetcurry.com
-----


ASP.NET Grid With Excel Capabilities | 27 GridView Tips & Tricks | Hosting your site? | SQL Server Tips
0
Suprotim
12/21/2007 4:37:44 PM

Thank you for the explination. Also, I noticed that and had updated the line to this:

pg.ClientScript.RegisterStartupScript(GetType(System.Web.UI.Page), "PrintJavaScript", Script) 'ASP.NET 2.0

This code is AWSOME by the way!!!!!  Thank you.  I am almost there.

I have 1 more very important issue with the code.  Right now once I click the button, the new page with the text box instructions is displayed. The problem is I do NOT want the user to have to hit back on the browser to get back to the previous page; in fact the user will get a page has expired message, and it is a mess.

Is there any way to pop up the new page a s separate page, or even hidden so the main page stays active in the background.  This way once the Print method is complete, the user will be on the original page.  If yes, how is the code you gave me modified?

Thanks!!!


Thank you,

0
atconway
12/21/2007 4:43:33 PM

Hi,

On Page1.aspx, use this code to pop up Page2:

  protected void btn1_Click(object sender, EventArgs e)
    {
Session["ctrl"] = TextBox1;
ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>window.open('Page2.aspx','PrintMe','height=300px,width=300px,scrollbars=1');</script>");
}


On Page2.aspx, in the Page_Load(), use this script

  protected void Page_Load(object sender, EventArgs e)
    {
        Control ctrl = (Control)Session["ctrl"];
        PrintHelper.PrintWebControl(ctrl);       
    }

 

HTH,
Suprotim Agarwal

-----
http://www.dotnetcurry.com
-----


ASP.NET Grid With Excel Capabilities | 27 GridView Tips & Tricks | Hosting your site? | SQL Server Tips
0
Suprotim
12/21/2007 5:37:41 PM

Great, almost there!  Is there any way I can close that 2nd window that pops up with the text box, once the user select 'Print' from the print dialog?  I guess I would need a way to capture that 'Print' event somehow.  Do you know what the code would be or how to do that?


Thank you,

0
atconway
12/21/2007 6:41:20 PM

Hi,

Add this to the pageload 

Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "<script language=javascript>self.close();</script>");

HTH,
Suprotim Agarwal

-----
http://www.dotnetcurry.com
-----


ASP.NET Grid With Excel Capabilities | 27 GridView Tips & Tricks | Hosting your site? | SQL Server Tips
0
Suprotim
12/22/2007 5:03:23 AM

That did not work for me, but mabye I have it in the wrong spot.  I have it in the PageLoad() of the .aspx page that pops up with the text box control.  Any suggesstions?

 Also, I am running into another issue:  The text box contains text that spans more than one sheet of paper when printed out, but only Page 1 is being printed.  I did check and 'All' pages is selected by default in the print dialog.  Any ideas on this?

Again, thanks for all of your help.


Thank you,

0
atconway
12/28/2007 5:44:03 PM
Reply:

Similar Artilces:

Need Windows Form to Retrieve Information From Web Form
I have a cash and carry web site that customers can order products, the next day come in to the company to pay and pickup. I would like to create a windows application for the company to do two things: 1. download all the orders from the day before 2. upload price changes, product changes, category changes I've search the internet and forums and the only thing I can find that I think would actually link these two together would be a web service. Can you please tell me if I am heading in the right direction or if there is another method to link the two applications, if there are any pitf...

Web Form vs. Web Content Form
If you don't mind, I would like to know what is the difference between those two items, when I go to add a new item I see both of those and I don't know what the difference is.  A content form is for use with a masterpage. A web form is a standalone web page. If you select web content form, you will then have to specify a masterpage for it. and what is a master page used for?  Hi, a master page is used to give a constant look and theme to a site, without having to duplicate code. So basically you can put your header, menu, nav and footer on a master page. Then a...

How to format a web form for printing?
Hi all, Is there a way to quickly 'see' (i.e. preview) the effect of printing a form without actually printing it out? IF yes, How?Thanks Tony  File | Print Preview? Are you talking about a web form? When you load it in the browser the best chance to see a preview is what I said above, although both Firefox and IE have trouble rendering accurate print previews. You can manipulate this outcome by creating a print only stylesheet:http://www.google.co.uk/search?q=print+stylesheet Hi TonyCheung1888, You can put some controls into control div,then preview it. ...

Print web form content
Hi, how to design print page with content of webform in the webform controls are images and lables textboxes ,gridview(gridview with textboxes and add button) and datalist how to get control values and design the print page.   you can design a page with your static HTML and use window.open to open it, creat a button and call window.print() on the client side click of the button.<asp:Button runat='server' id='btn' OnClientClick="window.print()" />SHASHANK BHIDECAPGEMINI INDIA"THE ROAD TO SUCCESS IS ALWAYS UNDER CONSTRUCTION MARK MY RESPONSE...

Open web page in IE and populate fields on the form from my web form
To the dot net Knights.  Many humble thanks in advance from a dot net surf. We have an vb application that opens multiple instances of IE object, each to different web pages.  It then takes information from the current record in the app, populates fields on each of the web pages, and them submits the forms.  We are then able to look at the resulting web pages and quickly get the info we're looking for.This prevents us from having to manually navigate to each web site, enter the same information and then wait for the page to open.  If there is ...

Print Web Form Contents On Submission
Is there a way to print (from the server) the contents of a web form when it is submitted? Thanks, Andrew You can access all the submitted web form fields from the Request object. You can loop through the field collection, create a document (Text/XML) on the fly with all these values and then send it to printer. Hey! It compiles! Ship it! Cheers, Venugopal Mallarapu. Visit : My Blog...

printing in certain format the web forms
Hi Everyone, I want to print something like    First Name : John Doe  < field coming from database>                    Supervisor: smith  < field coming from database>    and address at the bottom half of the page. I want this directly to be printed to the printer and there are at least 100 names or more that will be coming from the database and I need them to be printed on seperate page so one name, supervisor and address combination can only occur...

How to Make Web Forms from other Web Forms
In putting together an ASP.NET application for a biz application, we have come to the point where there are three different types of basic forms which have, for the most part, been perfected. Now its is a matter of cloning those forms for various uses and modifying or adding code to suit the particular need. Copy and paste works OK, but there is a lot of changing of certain words, etc. Is there a way in ASP.NET to take these basic forms, turn them into classes or templates, create forms from them, then add or overwrite code? ? (Anybody who is a Vis Fox Pro programmer would know imme...

Web forms and mobile web forms
Hey everyone, I am looking to develope a web site that depending on the device connecting to it, it will display a certain page. In this case im looking to create a web and wap version of the same project. I have the Web forms version finished using ASP.NET and C# and I am wondering how to create the mobile web form and incorporate it in the project. In the project folder, can I just add a mobile web??? But what about displaying device specific pages?? Cheers -Dudach Yes, you can add mobile web forms and controls to your Web Application project. No problem. For device/br...

Need Advice: Automated Form Fill-up and Submission of Web Forms
Hi! I am new to asking help in community forums and also new to c# network programming. I hope someone can point me to the right direction in starting what I want to do.I want to make a web application that would automatically fill-up application forms from other websites and automatically submit them. For instance, I want to automatically log-in in all my web accounts on different web pages through a button click in my asp.net page.  I am new to network programming but my initial research tells me to use the http and web service classes to accomplish this. Technically, i would get and ...

Need HELP with adding a System.Windows.Forms control to web form!
Howdy folks. I'm in need of some help. I wanted to add a nifty little Control I made for a Windows Form to an ASP.NET page. Now I went and saw Microsoft's quickstart tutorial, and followed the example... You know, compile it to .dll... blah, blah, blah, then use the <OBJECT> tag to insert it into the page. Well, I did all that... the control downloads... but I can't see a thing! :'( For example, here's some code ~ using System; using System.Windows.Forms; using System.Drawing; using System.Reflection; [assembly:AssemblyVersion("1.0.0.0")] ...

For interrupting.web form Resized by position of the div : if div can't dispaly all (such as only top part , then web form heithten the heght),resize the web form
Web Form Resized by position of the div : if div can't dispaly all (such as only top part , then web form heithten the heght),resize the web form ! Because of my pool English, if you can't see , I will explain more ! ThanksGambler is the real life I guess you mean when form grows it also grow parent div element. This is normal. If you want div to stay fixed size and rather clip the form and maybe show a scrollbar, look at overflow property in CSS.Thanks------------------------------------------------------------This posting is provided "AS IS" with no warranties, and confers no ...

Include web form inside web form
I tried to include an aspx file in a asp.net web form. However, all the web controls (textbox, button) in the included page are not displayed when called. <Form id="Form1" method="post" runat="server"> <%   Response.writeFile("subPage.aspx"); %> <asp:button id="btnPrev" runat="server" text="Previous"></asp:button> </Form> There are some <asp:textbox> controls in the subPage.aspx file, the file itself works fine, but when it is included in the main page, all the controls are not displayed. Please assist. Thanks   u should u...

Web Form embedded in another Web Form
Hi,  I have programmed a Web Form which acts like a scientific calculator. I would like to embedd this calculator in the upper left hand corner of another accounting Web Form. I don't want to host the calculator on the same server as the accounting page. I.e. I want them to have distinct URLs. Classically, I would do this using frames, but is there a better way using ASP? Is there some sort of Panel which can display the webpage at a specific URL which I can just drag onto my accounting Web Form? Thanks,  P1000   Frames are the only way to embed a webform inside ano...

Web resources about - Need to prompt client with Print Dialog from IE; need some format to print web form content - asp.net.web-forms

Help:Contents - Wikipedia, the free encyclopedia
Templates are special pages that contain boilerplate text intended to be displayed on more than one page in Wikipedia. This Tip of the day box ...

Portal:Contents/Society and social sciences - Wikipedia, the free encyclopedia
A society is a group of people who form a semi-closed system . At its simplest, the term society refers to a large group of people sharing their ...

Discuss the content of the comment, not the character of the commenter: A note from our chief moderator ...
Rob Ashton, the chief comment moderator for Fairfax Media&rsquo;s five metropolitan news websites, discusses the most-commented stories of the ...

Poll: What content would you like to hear on other Beats radio stations?
... just more music with Beats 2, 3, and beyond. Sports/Talk Radio/Podcasts… Apple already could have a bit of a head start with talk radio content ...

How Anomaly Shanghai Built Its Own Content Platform in China
... At the start of the films, the Anomaly logo flashes on the screen. The films are part of the agency's intellectual property play a content ...

Medium: An Ideal Content Marketing Platform?
Content marketing is only useful when it reaches the right audience. While there are many small steps to success , network selection could be ...

How to Make Money Sharing Sponsored Content Without Annoying Your Followers
It's widely accepted that sponsored content is a part of social media now. Not only do we see ads inserted into our streams by Facebook, Instagram, ...

New Google Glass Reportedly Comes With Foldable Arms, Larger Content Viewer
... the next iteration of Google Glass comes equipped with a slew of new features, including foldable arms and a larger screen for viewing content. ...

Splatoon's content updates will end soon, but it won't be abandoned
While I was sad to hear Splatoon is about to make its final big DLC push, I have to look back on the post-launch support fondly. It has been ...

Razer brings Ouya gaming content to its Forge TV set-top box
After quietly acquiring Ouya earlier this year, Razer's now folding some of those assets into its own gaming platform. Enter the Cortex Game ...

Resources last updated: 1/1/2016 10:17:40 PM