tags:

views:

545

answers:

2

Edit: Updated for more clarity.

I have a an unordered list of items, that I need to localize. Rather than wrapping the text of each list item - which, over the course of hundreds of <li>s, would probably kill me - I decided to wrap the <ul> in a literal. This way, I could throw the "text" attribute in a resource file for localization.

However, asp.net thinks that each line break in my code should be replaced with a <br />. Is there a property or a way around this that I'm just brainfarting on, besides replacing each \n in the text with nothing? I tried Mode="PassThrough" and that didn't seem to make a difference.

I've temporarily made all <br />s inside of <ul>s display:none in my css, but it feels hackish. I'd rather it not render the <br />s at all.

Code:

<asp:Literal id="literal" runat="server">
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        ...
        <li>Item 50</li>
    </ul>
</asp:Literal>

What it gives me:

<ul>
<br />
    <li>Item 1</li>
    <br />
    <li>Item 2</li>
    <br />
    ...
    <br />
    <li>Item 50</li>
    <br />
</ul>
A: 

How about using a PlaceHolder control, instead?

You can add the text programmatically to a PlaceHolder control like this:

this.MyPlaceHolderControl.Controls.Add(new LiteralControl(GetLocalizedResource()));

Note that this uses LiteralControl, not Literal, which should take no action on your content.

Does that work for you?

bdukes
Or a Panel if you need a wrap between the controls or nothing at all (there no need of the literal in the sample)
Eduardo Molteni
I need to directly access a sort of "text" attribute that I can put in a resource file.
Jack Lawson
Can you explain more? What "text" do you need?
Eduardo Molteni
The ul and it's contents is the "text" in the literal; it's the ul and it's contents that I need to be able to change via resource file. I can wrap each individual li's contents in a literal, but I'm trying to avoid that if possible.
Jack Lawson
+4  A: 

<asp:Literal Mode="PassThrough"></asp:Literal>

From MSDN:

PassThrough: The contents of the control are not modified.

Encode: The contents of the control are converted to an HTML-encoded string.

Transform: Unsupported markup-language elements are removed from the contents of the control. If the Literal control is rendered on a browser that supports HTML or XHTML, the control's contents are not modified.

Aric TenEyck
I tried passthrough and it didn't appear to make any difference.
Jack Lawson
On my machine, with a simple page with a literal on it and litTest.Text = "<ul>\n<li>test 1</li>\n<li>test 2</li>\n</ul>\n\n"; in the Page_Load, I don't see <br /> tags inserted in either Passthrough or Transform (the default) mode.Do you maybe have a string.replace("\n", "<br />") somewhere?
Aric TenEyck