Monday, March 26, 2012

Populate asp:label with text from db and retain formatting?

Hello,
Im can't seem to figure this out OR find any documentation on if it's possib
le; I need to populate the value of an asp:label with text retrieved from th
e database. However when the text is populated I need to retain carriage ret
urns and indents that the t
ext contains. That way if I want to see this:
Some text with a manual
carriage return.
1. this is one.
2. this is two.
It doesn't look like this:
Some text with a manual carriage return. 1. this is one. 2. this is two.
I've tried enclosing the text in <pre></pre> tags before the text is assigne
d to the label and it works fine except for 2 things. The text is rendered i
n a non-styled font and in the instance that the user entered really long te
xt and didn't manullay hit
a carriage return- which causes the text continue to the right instead of au
tomatically wrapping even though I define a width on my label, the cell the
label is within, etc..
Does anyone know how to get this to work? I must have looked around on the n
et for hours!set the text box Text Mode property to MultiLine.
Av.
"Tony" <anonymous@.discussions.microsoft.com> wrote in message
news:7B9C3796-02D8-4180-AD93-8572746CD951@.microsoft.com...
> Hello,
> Im can't seem to figure this out OR find any documentation on if it's
> possible; I need to populate the value of an asp:label with text retrieved
> from the database. However when the text is populated I need to retain
> carriage returns and indents that the text contains. That way if I want to
> see this:
> Some text with a manual
> carriage return.
> 1. this is one.
> 2. this is two.
> It doesn't look like this:
> Some text with a manual carriage return. 1. this is one. 2. this is two.
> I've tried enclosing the text in <pre></pre> tags before the text is
> assigned to the label and it works fine except for 2 things. The text is
> rendered in a non-styled font and in the instance that the user entered
> really long text and didn't manullay hit a carriage return- which causes
> the text continue to the right instead of automatically wrapping even
> though I define a width on my label, the cell the label is within, etc..
> Does anyone know how to get this to work? I must have looked around on the
> net for hours!
>
TextMode is not a property for a label but a textbox. I have tried using a t
extbox control with this property set to multiline previously however withou
t specifically setting the height of the textbox I end up with vertical scro
llbars- which I cannot have
because this is for a printable report.
Hi, Tony
You might write a function to conver the strin, like:
'Get str from database
str = "Some text with a manual carriage return." & vbCr & vbTab & "1. this i
s one. " & vbNewLine & vbTab & "2. this is two."
str = Replace(str, " ", " ")
str = Replace(str, vbTab, " ") 'De
pend how many spaces do you want for a tab.
str = Replace(str, vbNewLine, "<br/>")
str = Replace(str, vbCr, "<br/>")
str = Replace(str, vbLf, "<br/>")
Label1.Text = str
Hope this helps,
Bin Song, MCP
even better ..use a StringBuilder :)
Swanand Mokashi
Microsoft Certified Solution Developer (.NET)
Microsoft Certified Application Developer (.NET)
http://www.swanandmokashi.com/
http://www.swanandmokashi.com/HomePage/WebServices/
Home of the Stock Quotes, Quote of the day and Horoscope web services
"Bin Song, MCP" <anonymous@.discussions.microsoft.com> wrote in message
news:E9930659-B6E5-47F0-A42E-88BE5B97B7A9@.microsoft.com...
> Hi, Tony
> You might write a function to conver the strin, like:
> 'Get str from database
> str = "Some text with a manual carriage return." & vbCr & vbTab &
"1. this is one. " & vbNewLine & vbTab & "2. this is two."
> str = Replace(str, " ", " ")
> str = Replace(str, vbTab, " ") '
Depend
how many spaces do you want for a tab.
> str = Replace(str, vbNewLine, "<br/>")
> str = Replace(str, vbCr, "<br/>")
> str = Replace(str, vbLf, "<br/>")
> Label1.Text = str
> Hope this helps,
> Bin Song, MCP
>
The problem is that there is no universally accepted definition of how
preformatted ascii text should be rendered on the web. You just have
to munge it yourself. In the case you gave I would suggest something
like:
outputLabel.Text = dbText.Replace(@."\n", "<BR>").Replace(@."\t",
@." ");
There is no "easy way out" that I'm aware of.
Hope that helps
Ian
"Tony" <anonymous@.discussions.microsoft.com> wrote in message news:<7B9C3796-02D8-4180-AD93
-8572746CD951@.microsoft.com>...
> Hello,
> Im can't seem to figure this out OR find any documentation on if it's possible; I
need to populate the value of an asp:label with text retrieved from the database. Ho
wever when the text is populated I need to retain carriage returns and indents that
the
text contains. That way if I want to see this:
> Some text with a manual
> carriage return.
> 1. this is one.
> 2. this is two.
> It doesn't look like this:
> Some text with a manual carriage return. 1. this is one. 2. this is two.
> I've tried enclosing the text in <pre></pre> tags before the text is
assigned to the label and it works fine except for 2 things. The text
is rendered in a non-styled font and in the instance that the user
entered really long text and didn't manullay hit a carriage return-
which causes the text continue to the right instead of automatically
wrapping even though I define a width on my label, the cell the label
is within, etc..
> Does anyone know how to get this to work? I must have looked around on the net for
hours!
AWESOME!! Thanks Ian!! All I had to do is lose the '@.' sign (not sure if it'
s because I use C#) and it works. I cannot tell you how freakin ecstatic I a
m that your solution worked. Thank you!

0 comments:

Post a Comment