Link to home
Start Free TrialLog in
Avatar of martinjamesd
martinjamesd

asked on

Print contents of textbox in asp.net vb.net

I have a vb.net web page that contains a multiline textbox with hundreds of lines. I would like to be able to print the contents of this textbox. What is the best way to go about this?
Avatar of crescendo
crescendo

You can print portions of a page by putting a DIV around the required part and using Javascript to find and print it. See the link below./

http://javascript.about.com/library/scripts/blprintready.htm
Or you could also use the findcontrol as demonstrated here:

http://odetocode.com/Articles/116.aspx

excerpt:

One more tip before we move to the last section. Suppose we added a second button at the bottom of the form with the repeater. When the user clicks this button we want to loop the repeater collecting the contents of each TextBox control from every row. You can accomplish this task with the following code.

private void Button2_Click(object sender, System.EventArgs e)
{
   foreach(RepeaterItem item in Repeater1.Items)
   {
      TextBox b = item.FindControl("TextBox1") as TextBox;
      Response.Write(b.Text + "<br>");                      
   }
}

Regards,

Aeros
private void Page_Load(object sender, System.EventArgs e)
{
   MyUserControl u = FindControl("MyUserControl1") as MyUserControl;
   TextBox b = u.FindControl("TextBox1") as TextBox;
   if(b != null)
   {
      Response.Write("Found TextBox1 on Page_Load<br>");
   }                           
}
Aeros

All that does is to display the contents of the textbox on the web page. He already has that. I think that what he what he wants is to send the contents of the textbox to the printer without disturbing the page layout.
Oh I see no buttons, etc?
otherwise you could always just make the text string invisible on the page and print it from there
Avatar of martinjamesd

ASKER

What I need to do is print the entire contents of the textbox. I have 33 rows showing be design but there are multiples of that number in most cases in the textbox. Here is what I am doing and there maybe a better way overall to do it:

User selects file using "file field" control.
File is uploaded to web server
File is parsed and displayed in textbox for viewing.
User needs option to print out contents of textbox.

Maybe there is an easier way such as saving the file back to the server after parsing and then printing?
Add a label and a button to your page. When the button is clicked, copy the text into the label. Use the stuff in my first post's link to cause the label's contents to be printed.

Or, since you have the file on the server, create a page to display and print the contents of the file, and add a button to open that page in a new window.

See my posts in the question below for further information:

https://www.experts-exchange.com/questions/21085514/Saving-and-printing-a-File-from-a-Client-machine.html
Are you going to be using and DBMS?
I use SQL 2000 for pretty much everything except this.
If you wrote it to a db field, you could then bind it to the a datagrid and then use javascript simaler to crescendo's that would print whatever cell you wanted.  You really wouldn't have to do this you could always bind the text to the datagrid without the DB insert, though perhaps crescendos first solution is the easiest
Or you could just print from a stream

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDrawingPrintingPrintDocumentClassTopic.asp

Just trying to show you all your options, sorry for such rapid posting
Does your last example work with ASP or just standard Windows.Forms? It looks like the simplest option if so.
The class documentation doesn't specify that its not for webforms so I assumed it would be fine.  I have never used this myself so I'm not 100% sure honestly martinjamesd.  Since it is in the CLR and there is no mention that it doesn't work I think it will.  Does anyone have a definate answer on using system.drawing.printing for webforms?

Regards
I can't see that System.Drawing.Printing can work for ASP.NET. After all, it can only print to a local printer, i.e. on the server.
it couldn't default to the local machines printer & settings?  I mean there has to be some built in print mechanism right?
nishikanth:

That's a windows forms article, not asp.net. It won't work.
There's no way to print from an ASP.NET server direct to a remote PC's printer, unless that PC happens to be not so remote and has shared it's printer with the server, that is, they are on the same local area network.

As far as a normal Internet-based PC is concerned there's no mechanism to print to the PC. After all, the server and the PC could be completely different platforms, PC, Unix, mainframe, etc, so there's no way of building a "standard" way of sending print information that will be recognised by every possible configuration.

The only thing you can do is to use the window.print() method in client-side script. This prints the current window (or portion of it, see above). Since most people don't want to see all the buttons and textboxes on their printout, you have to mess around to get a reasonable printable document. You can either send the text in a new window, and print that, or use some clever Javascript to print a selected portion of the page. Both methods are detailed in my posts above.
ASKER CERTIFIED SOLUTION
Avatar of crescendo
crescendo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I produced a tested and working solution that did exactly what he asked for...
None of these examples worked in my particular situation. I ended up creating a text file. Uploading it to the web server and allowing the operator to open it in wordpad via a link.