Solved

Why am I losing formatting (carriage returns) in ASP.NET web textbox control?

Posted on 2004-03-31
16
1,150 Views
Last Modified: 2012-06-21
I am maintaining a web site using ASP.NET.  I have a Web text box control on one form used for data entry that is multi-line with scroll bars displayed a large amount of text, 6-7 pages worth, with formatting such as carriage returns so it looks decent.  I have to take the contents of this textbox, along with some of the other fields on the data entry form, and transfer it to a textbox on another form used to Print.  The only difference on the print page is all the text must be displayed on one page, so no scroll bars or multiline for the text box so it'll expand to fit the text. It does but I lose all my formatting! Tried label and same thing - all the carriage returns within the text disappear so it looks awful. Why am I losing my formatting and how do I preserve it from one web page to another?  Found one article that said to use Server.HTMLEncode but that didn't help. Also tried replacing vbcrlf with html <BR> as per another article, that didn't work either.

I'm expected to have this working today so - help!
0
Comment
Question by:FL6
  • 8
  • 5
16 Comments
 
LVL 28

Expert Comment

by:iboutchkine
Comment Utility
try to replace vbCrLf with
Environment.NewLine
or
ControlChars.NewLine.
0
 
LVL 7

Expert Comment

by:dante469
Comment Utility
Do not really understand why you are loosing all the formatting, however PLS try this workaround using the session...

On the calling form (main) do a

       session("tb")= tbSource  ' tbSource is the source textbox

Other form (child) please try

       tbDest.text = ctype(TextBox,session("tb").text

Please let me know if that resolves...  Have 2 other tricks up sleeve but hoping this one works...

Have Fun,
dante
0
 
LVL 7

Expert Comment

by:dante469
Comment Utility
Are you certain you have TextMode="MultiLine" on the destination ???

Did not have any problems in my test environment...

Have Fun,
Dante
0
 

Author Comment

by:FL6
Comment Utility
Follow up on this one ...
Feedback on answers and clarification on question for you...

To all of you - should have said a label not text box...(text box won't expand automatically to be as big as the text in it, I don't think...so for simplicity sake let's stick with label)

On the answers...

iboutchkine - did the replace for Environment.NewLine...this did add carriage returns but way too many. It seems like it's counting each space as a vbCrLf, as I get one word per line when I do that replacement - so I wonder what character is really the cr?

dante649 - was interrupted, still must try this which I'm about to do know. Will it work OK for a label?  Going to assume so if I change type from textbox to label, since label has a text property and we will see.  Now that I have clarified source form is multiline text box and print form, where formatting doesn't display, has label (to accommodate all text being displayed on page no matter how much text there is) let me know if that changes anything. Need to figure out what the character is for the crlf - as per my other response, vbCrLf doesn't seem to really be the carriage returns....

and thanks to both of you for suggestions!
0
 

Author Comment

by:FL6
Comment Utility
Hey Dante,

thanks for testing that - sorry on the mistake in my message! no not a multi line textbox...label...was looking at code-behind page and it was sitting there inbetween 10 other text boxes so misread it - wasn't looking at GUI...the joy of looking at someone else's code! :)

multi-line text box probably work OK but doesn't accommodate as much text as I want on the page...label does but then I lose the formatting...

0
 
LVL 7

Expert Comment

by:dante469
Comment Utility
No such thing as a muti line label :(....

Would you PLS explain why the mult-line textbox doesn't allow as much text...

Thank you,
Dante
0
 

Author Comment

by:FL6
Comment Utility
Hi Dante,

It's not that the textbox won't allow the text...it's that you have to set the number of rows to a specific size. ie. show 10 rows on screen. If the text is longer than 10 rows, then you have to scroll - you can't see all of the text at one time. Label expands itself so that all text is seen on the page at one time. They're using this other form as a 'Print' option, the contents of the form is sent to the printer.  Thus, if there's 15 rows worth of text and only 10 rows shown then when you print you lose that 5 rows of text.  With the label, all text is always shown no matter how many lines which is good but need the formatting!

Now, I guess I could dynamically look at the length of text and set the .rows property of the text box each time - but was hoping I could simply replace whatever character is a carriage return in a text box with whatever character is equivalent for label...and then I'm set.

Hope that clarifies things...thanks!

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Expert Comment

by:dante469
Comment Utility
Believe the following will meet your need :)...

Have Fun,
Dante


How about something like this --->

Put this in your .aspx where you want the contents of the SourceTB displayed....

    <p id="MultiText" runat="server"></p>

The IDE should add the following ref to codebehind but if not please add to codebehind--->

  Protected WithEvents MultiText As System.Web.UI.HtmlControls.HtmlGenericControl

Use the following code in codebehind to make the copy...

'tb1 = Source TextBox
'MultiText is a <P> control on the .aspx form
        Dim sTB() As String = Split(tb1.Text.ToString, Chr(13))
        Dim sL As String
        Dim sMulti As String
        For Each sL In sTB
            sMulti += sL & "<br>"
        Next
        MultiText.InnerHtml = sMulti
0
 

Author Comment

by:FL6
Comment Utility
Hi Dante,

Again - you're going to be sick of me! sorry... :)

Another wrinkle that comes into play - I also have to be able to toggle between this view in which you see everything (Show All) and another view which is the standard scrolling listbox.  They've accommodated this by using 2 different controls & setting visible on and off dynamically as per Show/Hide toggle buttons. Reason being the users just have to have the functionality to print either part of or all of this field, as they see fit.

Didn't mention this before as both label and textbox have 'Visible' property - I don't believe you can do that with <P>... or can you? Or do something like that?

By the way, the replacing of chars vbcrlf with Environment.Newline did work in that I got some carriage returns in a label - but the problem was there was way too many of them! I got a newline for each word ...
0
 
LVL 7

Expert Comment

by:dante469
Comment Utility
You can set the visibility of a <p> tag

            MultiText.Style.Add("display", "none")      'Invisible

            MultiText.Style.Remove("display")           'Visible
                      or
            MultiText.Style.Add("display", "")             'Visible


Other thing you can do is place the <P> tag inside a <div></div> and set the visibility of the div

Have tried everything to make a label work w/o success...  Setting the row on a textbox still leaves the scrollbars...

Trust this meets your requirements...

Have Fun,
Dante
0
 
LVL 7

Expert Comment

by:dante469
Comment Utility
The other thing to make the <P> not visible simply don't fill...

Suggest something like the following to make generic....

place a hidden checkbox on the screen lets say called cbPrint and create following sub....

BTW.. I am not against bonus points :)....

Have Fun,
Dante

sub IsPrint()
if cbPrint.checked = true then
   tb1.Visible=False     'tb1 is source
   MultiText.Style.Remove("display")           'Visible
        Dim sTB() As String = Split(tb1.Text.ToString, Chr(13))
        Dim sL As String
        Dim sMulti As String
        For Each sL In sTB
            sMulti += sL & "<br>"
        Next
        MultiText.InnerHtml = sMulti
else
   tb1.visible=true
   MultiText.Style.Add("display", "none")      'Invisible
End if

end sub
0
 

Author Comment

by:FL6
Comment Utility
Hi Dante,

First thanks for all the help! I did do as you said, which did add some carriage returns but unfortunately not all of them so the formatting still wasn't quite right.  So, I did what I should of done sooner, and stepped thru each character in the string - turned out to be ascii character 10 that the text was using instead of 13. So, replaced the chr(13) in your code with chr(10) and everything worked wonderfully. I was curious, so I also put the same reformatted text in the label and it worked there as well. So, I now know I can use either label or a generic control both work just fine as long as I replace chr(10) with <BR>.  Thought you would be interested in the final solution since you gave me so much help...

Now, I'm new to this site so I do have a question - what is proper etiquette for points?  I don't mind giving you lots of points at all...but curious how it's supposed to work - that is, if someone helps you but it's not quite fixed then what to do with the points? Are you not supposed to give all points, or does it matter? Asking this not just for this question, but in general - hope you don't mind.  You mentioned extra points can I do that?  And, if one offers one suggestion but it doesn't quite solve the problem (although it may assist you) and someone else offers lots of suggestion, does it let you split the points how you like so  they reflect that effort?
0
 
LVL 7

Accepted Solution

by:
dante469 earned 500 total points
Comment Utility
FL6,

Glad you have a working solution...  Who would have thought chr(10)???

Also glad my efforts provided assistance !!!

As to etiquette for points...  Don't have a firm answer, however feel that it is upto you to award based on value received and most likely level of input from comment provider.  Splitting points is certainly acceptable if you received value from more then one person.  Also awarding bonus points (we all like) are at your discretion and again believe that is a personal decision based on value received and most likely level of input from comment provider...

At anyrate...  It has been a pleasure working with you and thank you for the concern :)...

Have Fun,
Dante
0
 
LVL 7

Expert Comment

by:dante469
Comment Utility
FL6,

Just want to make sure that I am not missing something or have dropped the ball...

It is my understanding that this issue has been resolved and you are getting ready to close :)...

Have Fun,
Dante
0

Featured Post

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

Join & Write a Comment

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

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

10 Experts available now in Live!

Get 1:1 Help Now