Solved

Print contents of textbox in asp.net vb.net

Posted on 2004-08-12
26
3,776 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What are the Important skill to have as Asp.net Developer 8 43
SSRS ReportViewer report timeout 7 104
Authentication of Web Services 3 54
Entity Framework 7 32
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

17 Experts available now in Live!

Get 1:1 Help Now