Solved

Print contents of textbox in asp.net vb.net

Posted on 2004-08-12
26
3,752 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
  • 10
  • 8
  • 4
  • +1
26 Comments
 
LVL 9

Expert Comment

by:crescendo
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 9

Expert Comment

by:crescendo
Comment Utility
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
Comment Utility
Oh I see no buttons, etc?
0
 
LVL 17

Expert Comment

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

Author Comment

by:martinjamesd
Comment Utility
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
Comment Utility
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
Comment Utility
Are you going to be using and DBMS?
0
 

Author Comment

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

Expert Comment

by:AerosSaga
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 9

Expert Comment

by:crescendo
Comment Utility
nishikanth:

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

Expert Comment

by:crescendo
Comment Utility
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 250 total points
Comment Utility
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
Comment Utility
I produced a tested and working solution that did exactly what he asked for...
0
 

Author Comment

by:martinjamesd
Comment Utility
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 Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now