Dchris
Dchris

Reputation: 3047

asp.net literal.text using multiple lines of html code

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>";

Upvotes: 1

Views: 6877

Answers (2)

Dave
Dave

Reputation: 4414

For a small number of lines of code, I generally use StringBuilder. 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 System.IO.File, 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 static class 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();

Upvotes: 2

Oded
Oded

Reputation: 498924

Use a verbatim string literal (one that starts with @):

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

Upvotes: 2

Related Questions