Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Print contents of textbox in asp.net vb.net

Posted on 2004-08-12
26
Medium Priority
?
3,884 Views
Last Modified: 2009-04-19
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?
0
Comment
Question by:martinjamesd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 8
  • 4
  • +1
26 Comments
 
LVL 9

Expert Comment

by:crescendo
ID: 11783138
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
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11783246
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
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11783261
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>");
   }                           
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Expert Comment

by:crescendo
ID: 11783337
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.
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11783351
Oh I see no buttons, etc?
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11783359
otherwise you could always just make the text string invisible on the page and print it from there
0
 

Author Comment

by:martinjamesd
ID: 11783398
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?
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11783459
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:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21085514.html
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11783484
Are you going to be using and DBMS?
0
 

Author Comment

by:martinjamesd
ID: 11783511
I use SQL 2000 for pretty much everything except this.
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11783555
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
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11783579
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11783607
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
0
 

Author Comment

by:martinjamesd
ID: 11785684
Does your last example work with ASP or just standard Windows.Forms? It looks like the simplest option if so.
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11785958
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
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11789239
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.
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11790272
it couldn't default to the local machines printer & settings?  I mean there has to be some built in print mechanism right?
0
 
LVL 8

Expert Comment

by:nishikanth
ID: 11790324
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11791410
nishikanth:

That's a windows forms article, not asp.net. It won't work.
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11791450
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.
0
 
LVL 9

Accepted Solution

by:
crescendo earned 1000 total points
ID: 11791646
OK, I have a fully working solution! The code below uses javascript to print the contents of a multiline textbox, as requested. To test it, create a new ASPX page, and paste the script below in to the HEAD section, then create a textbox called TextBox1, make it multiline, and put the following hyperlink below it:

    <a href="javascript:void(printSpecial('TextBox1'))">Print this Textbox</a>

Browse the page, enter some text, click on the link and voila! It opens in a new window and asks you which printer to use. Cool.

If you change the name of the textbox, just change 'TexBox1' in the hyperlink.




            <script language="JavaScript">
var gAutoPrint = true; // Flag for whether or not to automatically call the print function

function printSpecial(TextBoxName)
{
      if (document.getElementById != null)
      {
            var html = '<HTML>\n<HEAD>\n';

            if (document.getElementsByTagName != null)
            {
                  var headTags = document.getElementsByTagName("head");
                  if (headTags.length > 0)
                        html += headTags[0].innerHTML;
            }
            
            html += '\n</HE' + 'AD>\n<BODY>\n';
            
            var printReadyElem = document.getElementById(TextBoxName);
            
            if (printReadyElem != null)
            {
                        html += printReadyElem.innerHTML.replace(new RegExp("\n", "g"),"<br>");
            }
            else
            {
                  alert("Could not find the printReady section in the HTML");
                  return;
            }
                  
            html += '\n</BO' + 'DY>\n</HT' + 'ML>';
            
            var printWin = window.open("","printSpecial");
            printWin.document.open();
            printWin.document.write(html);
            printWin.document.close();
            if (gAutoPrint)
                  printWin.print();
      }
      else
      {
            alert("Sorry, the print ready feature is only available in modern browsers.");
      }
}

            </script>
0
 
LVL 9

Expert Comment

by:crescendo
ID: 12073242
I produced a tested and working solution that did exactly what he asked for...
0
 

Author Comment

by:martinjamesd
ID: 12199455
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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question