Working interchangeably with vbCrLf in a TextBox and <br> in a Label

J

Jim in Arizona

On one page I have a datalist where a message is bound to a label.
On another page I have the datalist which allows for the insert of new
message as well as being able to edit those messages. The editing
(EditItemTemplate) has a textbox that loads the message into its text
property.
The problem I'm running into is working with vbCrLf and <br> interchangeably
between the textbox on the admin page and the label on the general message
page. I want line breaks to be visible in the label but I also want these
visible in the textbox.

In the EditItemTemplate, I can do something like this when they save the
edited message:

Dim txtMngrMessageEdit As New TextBox
txtMngrMessageEdit = e.Item.FindControl("txtMngrMessageEdit")
txtMngrMessageEdit.Text = Replace(txtMngrMessageEdit.Text, vbCrLf, "<br>")

This way, when the label on the message page displays the message, the line
breaks are produced correctly. However, if the manager goes to edit the
message again, the message now shows <br> within the message and all the
text on a single line within the textbox.

I was thinking about doing the reverse when they click the Edit button:

Dim txtMngrMessageEdit As New TextBox
txtMngrMessageEdit = e.Item.FindControl("txtMngrMessageEdit")
txtMngrMessageEdit.Text = Replace(txtMngrMessageEdit.Text, "<br>", vbCrLf)

This returns an Error "Object reference not set to an instance of an object"
within a pop up box.

I could replace the Label control with a textbox control on the message page
but then I wouldn't be able to show longer messages in their entirety like I
could with a label control that adjusts its height according to the size of
the message. If I replace the label with a textbox and set a static height
on the text box with textmode="multiline", it would look unprofessional (for
this application) and take up more page space (vertically) than is
necessary.

What's the best way to work around this? Is there a way to set a textbox's
height to the vertical size of the lines of text within it? If I could do
that, then my problem would be solved. Or, if there's a way to fix the Label
control to properly display vbCrLf so I don't have to manually force in <br>
tags from a textbox control.

TIA,
Jim
 
J

Jim in Arizona

Jim in Arizona said:
On one page I have a datalist where a message is bound to a label.
On another page I have the datalist which allows for the insert of new
message as well as being able to edit those messages. The editing
(EditItemTemplate) has a textbox that loads the message into its text
property.
The problem I'm running into is working with vbCrLf and <br>
interchangeably between the textbox on the admin page and the label on the
general message page. I want line breaks to be visible in the label but I
also want these visible in the textbox.

In the EditItemTemplate, I can do something like this when they save the
edited message:

Dim txtMngrMessageEdit As New TextBox
txtMngrMessageEdit = e.Item.FindControl("txtMngrMessageEdit")
txtMngrMessageEdit.Text = Replace(txtMngrMessageEdit.Text, vbCrLf, "<br>")

This way, when the label on the message page displays the message, the
line breaks are produced correctly. However, if the manager goes to edit
the message again, the message now shows <br> within the message and all
the text on a single line within the textbox.

I was thinking about doing the reverse when they click the Edit button:

Dim txtMngrMessageEdit As New TextBox
txtMngrMessageEdit = e.Item.FindControl("txtMngrMessageEdit")
txtMngrMessageEdit.Text = Replace(txtMngrMessageEdit.Text, "<br>", vbCrLf)

This returns an Error "Object reference not set to an instance of an
object" within a pop up box.

I could replace the Label control with a textbox control on the message
page but then I wouldn't be able to show longer messages in their entirety
like I could with a label control that adjusts its height according to the
size of the message. If I replace the label with a textbox and set a
static height on the text box with textmode="multiline", it would look
unprofessional (for this application) and take up more page space
(vertically) than is necessary.

What's the best way to work around this? Is there a way to set a textbox's
height to the vertical size of the lines of text within it? If I could do
that, then my problem would be solved. Or, if there's a way to fix the
Label control to properly display vbCrLf so I don't have to manually force
in <br> tags from a textbox control.

TIA,
Jim

My thanks to Aaron Bertrand, SQL Server MVP, for assisting me with finding
an answer to my problem from the microsoft.public.sqlserver.programming
group.

I was able to resolve this issue by using the Replace function within the
Text property of the Label itself.

<asp:Label ID="lblManagerMsg" runat="server" Font-Names="Tahoma"
Text='<%#REPLACE(Eval("message"),vbCrLf,"<br>") %>' />
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top