1

Do I need to go through the whole thing (see below) and make it escape in order put this in string? (using c#, vs 2010, console app)

<link rel="stylesheet" href="_layouts/jquery/global.css">
<script type="text/javascript" src="_layouts/jQuery/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="_layouts/jQuery/slides.min.jquery.js"></script>
<script>
    $(function(){
        $('#slides').slides({
            preload: true,
            preloadImage: 'img/loading.gif',
            play: 5000,
            pause: 2500,
            hoverPause: true,
            animationStart: function(current){
                $('.caption').animate({
                    bottom:-50
                },100);
                if (window.console && console.log) {
                    // example return of current slide number
                    console.log('animationStart on slide: ', current);
                };
            },
            animationComplete: function(current){
                $('.caption').animate({
                    bottom:0
                },200);
                if (window.console && console.log) {
                    // example return of current slide number
                    console.log('animationComplete on slide: ', current);
                };
            },
            slidesLoaded: function() {
                $('.caption').animate({
                    bottom:0
                },200);
            }
        });
    });
</script>

    <div id="container">
        <div id="example">
            <img src="img/new-ribbon.png" width="112" height="112" alt="New Ribbon" id="ribbon">
            <div id="slides">
                <div class="slides_container">
3
  • 1
    Try Verbatim string once (@"string") and see if works Commented Jun 29, 2011 at 15:59
  • @Rahul -- that won't escape double quotations. Commented Jun 29, 2011 at 16:00
  • @George, True; I was not sure; just checked and yep it don't work Commented Jun 29, 2011 at 16:01

7 Answers 7

5

If you've got a large amount of text which you want to put in a string, I wouldn't hard-code it into the source code to start with. Either include it in a file you can load from disk, or include it in an embedded resource and load it with Assembly.GetManifestResourceStream. That way it doesn't need escaping, you can use syntax highlighting in your favourite editor, and your C# code stays filled with C# rather than HTML.

Sign up to request clarification or add additional context in comments.

6 Comments

@Jon: Actually, I am appending bunch of div dynamically that's why I want to make sure they are all together.
@Amber: It still sounds like that might be better off in text files than within your C#. You can load the files once and store the contents in a bunch of static readonly variables, of course.
@Jon: Now that I got to think twice, I think I am going to use Resource. I have 3 pieces that will go into a text file. The first piece (above code in my original post, static html), then I need to create bunch of html with c# code, then 3rd piece (static html). Is it possible to create 2 resource file for the static html? if yes, then how would I do that and finally put all of this into a text file.
@Amber: You can embed as many files as you want. Personally I don't typically use ResourceManager etc - I just set the build action for the text file to be "Embedded Resource" and load it with Assembly.GetManifestResourceStream. (Then in your case, wrap it in a StreamReader and call ReadToEnd.)
I added 2 string to resource (string1 and string2). I added this line to the console app StreamReader sr = new StreamReader(GetDiv.htmlResource.strHTML1.ToString()); I get an error "illegal characters in path"
|
3

For such a large body of text, I wouldn't quote it directly in code. I would consider adding this as a linked resource or embedded resource and reading it from there.

Comments

2

I guess the obvious question is, why would you want to put all that in to a string? If you want javascript conditionally included in the page you would be better off to store it in a separate file and then add that script to the page using the client script manager, i.e.

ClientScriptManager cs = Page.ClientScript;
if (!cs.IsClientScriptIncludeRegistered(cstype, csname))
{
    cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl));
}

1 Comment

+1 for adding good info. This is not THE answer to the question, but it is relevant info, and perhaps what the OP should do.
0

Yes you will need to escape everything in order to place this inside a string. One thing you could also use is StringBuilder to build your string one line of code at a time. You will still have to escape characters like '"'. Best option thought would to keep this as an external resource in your application. Have fun.

Comments

0

Yes you right you'll need to escape every quotation mark in the string.

But doing such thing as building the whole page in the code behind is very bad thing. Don't do it.

Comments

0

An alternative way: Put it in the resource file, if something change you don't need to escape it again.

Comments

0

You would have to escape this to have it directly in a C# file. Why not store it an external file and read it into a string instead? Just use a StreamReader or something like that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.