asp.net literal.text usando varias líneas de código html

I am trying to initialize a literal with a big sequence of html code. for example this is working just fine:

Literal1.Text.= "<ul><li>home<li/><ul/>";//just fine

But: When i have more than one lines it doesn't work:

Literal1.Text.= "<ul><li>home
                 </li></ul>";//error

I know a simple solution which is this:

Literal1.Text.= "<ul><li>home"+
                "</li></ul>";//nice but time wasting for many lines

But: When i have 100 lines for example i don't want to spend my time concatenating the strings.Is there any more practical solution?

Specifically my html code is the folowing:

    Literal1.Text.= "<ul id="nav">


<li class="current"><a href="http://www.webdesignerwall.com">Home</a></li>
<li><a href="http://www.ndesign-studio.com">People</a>

    <ul>
        <li><a href="http://www.ndesign-studio.com">Customers</a>
            <ul>
                <li><a href="http://www.ndesign-studio.com/portfolio">View</a></li>
                <li><a href="http://www.ndesign-studio.com/wp-themes">Edit</a></li>
                <li><a href="http://www.ndesign-studio.com/wallpapers">Print</a></li>
                <li><a href="http://www.ndesign-studio.com/tutorials">Delete</a></li>

            </ul>
        </li>
        <li><a href="http://www.webdesignerwall.com">Employees</a>
            <ul>
                <li><a href="http://www.ndesign-studio.com/portfolio">View</a></li>
                <li><a href="http://www.ndesign-studio.com/wp-themes">Edit</a></li>
                <li><a href="http://www.ndesign-studio.com/wallpapers">Print</a></li>
                <li><a href="http://www.ndesign-studio.com/tutorials">Delete</a></li>
            </ul>
        </li>

    </ul>
</li>
<li><a href="#">Quotations</a>
    <ul>
        <li><a href="#">Create</a></li>
        <li><a href="#">Edit</a></li>
        <li><a href="#">View</a></li>



    </ul>
</li>   


<li><a href="#">Invoices</a>
    <ul>
        <li><a href="#">Create</a></li>
        <li><a href="#">Edit</a></li>
        <li><a href="#">View</a></li>



    </ul>
</li>   

<li><a href="#">Receipts</a>
    <ul>
        <li><a href="#">Create</a></li>
        <li><a href="#">Edit</a></li>
        <li><a href="#">View</a></li>


    </ul>
</li>   

<li><a href="#">Statements</a>
    <ul>
        <li><a href="#">Create</a></li>


    </ul>
</li>   

<li><a href="#">About Us</a></li>
<li class="style1"><a href="#">Contact Us</a></li>
</ul>";

preguntado el 09 de marzo de 12 a las 15:03

in addition to oded's answer you may have to replace " con \" [escape character] or ' [single quotes will] -

Althought Oded has the correct answer to your question, I can't help think that maybe you've made a design decision that should be rethought. It miradas like you are coming from the php world where you do a lot of html concatenation in code.. .Net takes a una experiencia diferente Acercarse. -

@dchris I fixed your closing tags. Surely you meant </foo>no, <foo/>... -

@rs:do i have to replace every single " with \";Because i tried it and it didn't work. -

2 Respuestas

For a small number of lines of code, I generally use Constructor de cadenas. That way there are no performance issues with concatenating large immutable strings.

StringBuilder sb = new StringBuilder();

sb.Add("<ul><li>home");
sb.Add("</li></ul>");

For a large number of lines, don't put them in your code at all. Read them in using Sistema.IO.Archivo, so that you can edit your HTML without worrying about doubling your quotes. If you're using the code a lot, read it in once using a clase estática so that you're not causing a lot of extra IO usage. Something like this (untested):

public static class HTMLStringClass
{
    private static string html;

    public static string GetHtmlString()
    {
        if (string.IsNullOrEmpty(html))
            html = File.ReadAllText("path/to/file");
        return html;
    }
}

And then call it by doing this:

Literal1.Text = HtmlStringClass.GetHtmlString();

respondido 09 mar '12, 16:03

You could also use resource strings. :) - mgnoonan

Utilizar literal de cadena literal (one that starts with @):

Literal1.Text = @"<ul><li>home
                 </li></ul>";

respondido 09 mar '12, 15:03

Note that when using a double quotes inside a verbatim string literal, you escape them with another double quote "" instead of a backslash. - David

@Dave - Or, since this is an HTML literal, you can escape attributes using single quotes ('). - Oded

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.