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

Print? I guess you mean print on the screen?

Did you check the HttpContext.Current.Response object?

Regards,

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

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

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

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>

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

the solution is simple, just open a new window with the content of that textbox using javascript.

hmm... did you read my last post serkansendur? :P

hi richard,
I think the code u have written is VB.NET, Can u please send the code in C#.

thanks

hmm... did you read my last post serkansendur? :P

yes i did, but isnt it too long? you are going to scare him and he will quit programming soon.

from the conversiontool ;)

http://www.developerfusion.com/tools/convert/vb-to-csharp/

protected void // ERROR: Handles clauses are not supported in C# Page_Load(object sender, System.EventArgs e) 
{ 
    System.Text.StringBuilder sPrintScript = 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<b></b>: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 (!ClientScript.IsClientScriptBlockRegistered("printText")) ClientScript.RegisterClientScriptBlock(this.GetType(), "printText", sPrintScript.ToString); 
    Button1.Attributes.Add("onclick", string.Format("return printText('{0}');", this.TextBox1.ClientID)); 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.