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

Discussion in 'ASP .Net' started by Jim in Arizona, Jun 12, 2008.

  1. 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
    Jim in Arizona, Jun 12, 2008
    #1
    1. Advertising

  2. Re: Working interchangeably with vbCrLf in a TextBox and <br> in a Label [RESOLVED]

    "Jim in Arizona" <> wrote in message
    news:...
    > 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>") %>' />
    Jim in Arizona, Jun 13, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hermit Dave
    Replies:
    0
    Views:
    393
    Hermit Dave
    Sep 21, 2004
  2. CSharpner
    Replies:
    0
    Views:
    1,019
    CSharpner
    Apr 9, 2007
  3. Evertjan.
    Replies:
    2
    Views:
    181
    Techy
    Nov 23, 2003
  4. techy techno
    Replies:
    1
    Views:
    109
    Evertjan.
    Nov 23, 2003
  5. Knut Krueger
    Replies:
    8
    Views:
    265
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page