DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   ASP.NET (http://www.daniweb.com/forums/forum18.html)
-   -   how to print the contents of text box (http://www.daniweb.com/forums/thread161217.html)

sancti Dec 8th, 2008 1:48 am
how to print the contents of text box
 
hi all,
I am developing a web application. I have loaded some block of text into the text box. Now i want to print only the contents of the text box.Can anybody help me regarding since i am new to asp.net

thanks in advance

4advanced Dec 8th, 2008 4:56 am
Re: how to print the contents of text box
 
Print? I guess you mean print on the screen?

Did you check the HttpContext.Current.Response object?

Regards,

sancti Dec 8th, 2008 5:02 am
Re: how to print the contents of text box
 
hi,
no i have not used HttpContext.current......
If i use that, will it be possible to print the contents of text box in the sameway as i entered into the text box. Can u please explain me briefly how to use that..
thanks

4advanced Dec 8th, 2008 5:53 am
Re: how to print the contents of text box
 
Hi :=)

Do you want to change the screen during typeing the text in the textbox?
You have to notice that the scripts you write (in the code-behind-page or in the page itself) are serverside executed. Which means that you have to implement some kind of redirection. The redirection has been standarized within the textbox by means of the autopostback option. This, in combination with the TextChange() event is a worse combination because after each keypress the page will be posted back.....
The LostFocus() event gives a much better performance on this but the page still have to make a roundtrip to the server in able to execute the scripts.....

What exactly are you trying to acchieve?

Regards,
Richard
The Netherlands

sancti Dec 8th, 2008 6:02 am
Re: how to print the contents of text box
 
hi Richard,
Thank you for your reply.Actually i have loaded some block of text into the text box with particular line at particular position. Now i want to print that contents of text box by pressing the print button.Can u please help me to solve this
thanks

4advanced Dec 8th, 2008 6:13 am
Re: how to print the contents of text box
 
This code should manage your printing with asp.net. It's a piece of javascript;)

<script type="text/javascript">
<!--
function printTextBox_Text(elementId)
{
 var textboxText = document.getElementById(elementId);
 var windowUrl = 'about:blank';
 var windowName = 'Print' + new Date().getTime();
 var printWindow = window.open(windowUrl, windowName, 'left=50000,top=50000,width=0,height=0');

 printWindow.document.write(textboxText .value);
 printWindow.document.close();
 printWindow.focus();
 printWindow.print();
 printWindow.close();
}
// -->
</script>

4advanced Dec 8th, 2008 8:19 am
Re: how to print the contents of text box
 
I've worked it out to an example for you....

Place 1 textbox control on your page, make it multiline and insert the below stated code in the code-behind page.

Regards,
Richard
The Netherlands
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sPrintScript As System.Text.StringBuilder = New System.Text.StringBuilder
        sPrintScript.Append("<script type='text/javascript'>").AppendLine("")
        sPrintScript.Append("<!--").AppendLine("")
        sPrintScript.Append("function printText(elementId)").AppendLine("")
        sPrintScript.Append("{").AppendLine("")
        sPrintScript.Append("var printContent = document.getElementById(elementId);").AppendLine("")
        sPrintScript.Append("var windowUrl = 'about:blank';").AppendLine("")
        sPrintScript.Append("var windowName = 'Print' + new Date().getTime();").AppendLine("")
        sPrintScript.Append("var sTextToPrint = printContent.innerHTML;").AppendLine("")
        sPrintScript.Append("var printWindow = window.open(windowUrl, windowName, 'left=50000,top=50000,width=0,height=0');")
        sPrintScript.AppendLine("")
        sPrintScript.Append("printWindow.document.write(sTextToPrint);").AppendLine("")
        sPrintScript.Append("printWindow.document.close();").AppendLine("")
        sPrintScript.Append("printWindow.focus();").AppendLine("")
        sPrintScript.Append("printWindow.print();").AppendLine("")
        sPrintScript.Append("printWindow.close();").AppendLine("").Append("}").AppendLine("")
        sPrintScript.Append("// -->").AppendLine("")
        sPrintScript.Append("</script>")
        If Not ClientScript.IsClientScriptBlockRegistered("printText") Then _
        ClientScript.RegisterClientScriptBlock(Me.GetType(), "printText", sPrintScript.ToString)
        Button1.Attributes.Add("onclick", String.Format("return printText('{0}');", Me.TextBox1.ClientID))
    End Sub

serkan sendur Dec 8th, 2008 12:02 pm
Re: how to print the contents of text box
 
the solution is simple, just open a new window with the content of that textbox using javascript.

4advanced Dec 10th, 2008 6:12 am
Re: how to print the contents of text box
 
hmm... did you read my last post serkansendur? :P

sancti Dec 10th, 2008 6:24 am
Re: how to print the contents of text box
 
hi richard,
I think the code u have written is VB.NET, Can u please send the code in C#.

thanks


All times are GMT -4. The time now is 6:26 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC