Solved

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

Posted on 2004-03-31
16
1,187 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
ID: 10726599
try to replace vbCrLf with
Environment.NewLine
or
ControlChars.NewLine.
0
 
LVL 7

Expert Comment

by:dante469
ID: 10726763
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
ID: 10726855
Are you certain you have TextMode="MultiLine" on the destination ???

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

Have Fun,
Dante
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:FL6
ID: 10727021
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
ID: 10727059
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
ID: 10727116
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
ID: 10727216
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
 
LVL 7

Expert Comment

by:dante469
ID: 10727361
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
ID: 10727644
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
ID: 10728030
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
ID: 10728078
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
ID: 10734651
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
ID: 10735361
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
ID: 10784283
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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