1

I'm creating a loop in which each line is a pretty long HTML line on the page. I've tried various combinations of @ and """ but I just can't seem to get the hang of it This is what I've got now, but the single quotes are giving me problems on the page, so I want to change all the single quotes to double quotes, just like a normal HTML line would use them for properties in the elements:

sOutput += "<div class='item link-item " + starOrBullet + "'><a href='" + appSet + linkID + "&TabID=" + tabID + "' target=’_blank’>" + linkText + "</a></div>";
variables are:
starOrBullet 
appSet
LinkID
tabID (NOT $TabID=)
linkText

BTW, appSet="http://linktracker.swmed.org:8020/LinkTracker/Default.aspx?LinkID="
Can someone help me here?
2
  • What are the values of your variables? Commented Feb 7, 2019 at 14:25
  • 1
    You can escape dbl quotes in a regular string with \" or in verbatim string literals with "". Example: sOutput += $"<div class=\"item link-item {starOrBullet}\"><a href=\"{appSet}{linkID}\" &TabID=\"{tabID}\" target=\"_blank\">{linkText}</a></div>"; Commented Feb 7, 2019 at 14:35

3 Answers 3

2

You have to escape the double quotes (") with \"

For your case:

sOutput += "<div class=\"item link-item " + starOrBullet + "\"><a href=\"" + appSet + linkID + "&TabID=" + tabID + "\" target=’_blank’>" + linkText + "</a></div>";

If you concat many strings, you should use StringBuilder for performance reasons.

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

Comments

2

You can use a verbatim string and escape a double quote with a double quote. So it will be a double double quote.

tring mystring = @"This is \t a ""verbatim"" string";

You can also make your string shorter by doing the following:

Method 1

string mystring = @"First Line
                    Second Line
                    Third Line";

Method 2

string mystring = "First Line \n" +
                  "Second Line \n" +
                  "Third Line \n";

Method 3

var mystring = String.Join(
               Environment.NewLine,
               "First Line",
               "Second Line",
               "Third Line");

Comments

0

You must make habit to use C# class to generate Html instead concatenation. Please find below code to generate Html using C#.

Check this link for more information https://dejanstojanovic.net/aspnet/2014/june/generating-html-string-in-c/

https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.htmltextwriter

Find below code for your question

protected void Page_Load(object sender, EventArgs e)
        {
            string starOrBullet = "star-link";
            string appSet = "http://linktracker.swmed.org:8020/LinkTracker/Default.aspx?LinkID=";
            string LinkID = "2";
            string tabID = "1";
            string linkText = "linkText_Here";
            string sOutput = string.Empty;

            StringBuilder sbControlHtml = new StringBuilder();

            using (StringWriter stringWriter = new StringWriter())
            {
                using (HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter))
                {
                    //Generate container div control  
                    HtmlGenericControl divControl = new HtmlGenericControl("div");
                    divControl.Attributes.Add("class", string.Format("item link-item {0}",starOrBullet));                    

                    //Generate link control  
                    HtmlGenericControl linkControl = new HtmlGenericControl("a");
                    linkControl.Attributes.Add("href", string.Format("{0}{1}&TabID={2}",appSet,LinkID,tabID));
                    linkControl.Attributes.Add("target", "_blank");
                    linkControl.InnerText = linkText;

                    //Add linkControl to container div  
                    divControl.Controls.Add(linkControl);

                    //Generate HTML string and dispose object   
                    divControl.RenderControl(htmlWriter);
                    sbControlHtml.Append(stringWriter.ToString());
                    divControl.Dispose();
                }
            }

            sOutput = sbControlHtml.ToString();
        }

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.