Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Print contents of textbox in asp.net vb.net

Posted on 2004-08-12
26
Medium Priority
?
3,903 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
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

972 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