String

S

shapper

Hello,

I have an Action where I define a ViewData property named Ad:

public ActionResult GoogleAdSense(string client, int height,
string slot, int width) {
StringBuilder ad = new StringBuilder();
ad.AppendLine("<script type='text/javascript'>");
ad.AppendLine("<!--");
ad.AppendLine(String.Concat("google_ad_client = '", client,
"';"));
ad.AppendLine(String.Concat("google_ad_slot = '", slot, "';"));
ad.AppendLine(String.Concat("google_ad_width = ",
width.ToString(), ";"));
ad.AppendLine(String.Concat("google_ad_height = ",
height.ToString(), ";"));
ad.AppendLine("//--> ");
ad.AppendLine("</script>");
ad.AppendLine("<script type='text/javascript' src='http://
pagead2.googlesyndication.com/pagead/show_ads.js'></script>");
ViewData["Ad"] = ad;
return View("GoogleAdSense");
}

Then I use, on the control:

<%= Html.Encode(ViewData["Ad"]) %>

This is not working because I get in my Page Source:

&lt;script type='text/javascript'&gt;
&lt;!--
google_ad_client = '###########';
google_ad_slot = '######';
google_ad_width = 300;
google_ad_height = 250;
//--&gt;
&lt;/script&gt;
&lt;script type='text/javascript' src='http://
pagead2.googlesyndication.com/pagead/show_ads.js'&gt;&lt;/script&gt;

The problem is with the '<' and '>' characters. Any idea why?

One more question: in C# when I want a quote inside a "" should I use
' ?
The generated code shows ' but in Google Ad Sense they say to use ".
I tried to use "" to create a " but I get an error on my code. Why?

Thanks,

Miguel
 
G

G.S.

Hello,

I have an Action where I define a ViewData property named Ad:

    public ActionResult GoogleAdSense(string client, int height,
string slot, int width) {
      StringBuilder ad = new StringBuilder();
      ad.AppendLine("<script type='text/javascript'>");
      ad.AppendLine("<!--");
      ad.AppendLine(String.Concat("google_ad_client = '", client,
"';"));
      ad.AppendLine(String.Concat("google_ad_slot = '", slot, "';"));
      ad.AppendLine(String.Concat("google_ad_width = ",
width.ToString(), ";"));
      ad.AppendLine(String.Concat("google_ad_height = ",
height.ToString(), ";"));
      ad.AppendLine("//--> ");
      ad.AppendLine("</script>");
      ad.AppendLine("<script type='text/javascript' src='http://
pagead2.googlesyndication.com/pagead/show_ads.js'></script>");
      ViewData["Ad"] = ad;
      return View("GoogleAdSense");
    }

Then I use, on the control:

<%= Html.Encode(ViewData["Ad"]) %>

This is not working because I get in my Page Source:

&lt;script type='text/javascript'&gt;
&lt;!--
google_ad_client = '###########';
google_ad_slot = '######';
google_ad_width = 300;
google_ad_height = 250;
//--&gt;
&lt;/script&gt;
&lt;script type='text/javascript' src='http://
pagead2.googlesyndication.com/pagead/show_ads.js'&gt;&lt;/script&gt;

The problem is with the '<' and '>' characters. Any idea why?

One more question: in C# when I want a quote inside a "" should I use
' ?
The generated code shows ' but in Google Ad Sense they say to use ".
I tried to use "" to create a " but I get an error on my code. Why?

Thanks,

Miguel

test this in your HTML
<%= Html.Encode("<") %>

then test this
<%= "<" %>


As far as your second question, you need to "escape" the " character
like this:
ad.AppendLine("This string contains the \" character");

See this:
http://msdn.microsoft.com/en-us/library/aa691090(VS.71).aspx

and this:
http://en.csharp-online.net/CSharp_FAQ:_What_are_the_CSharp_character_escape_sequences

or do a search on "C# escape"
 
R

Rad [Visual C# MVP]

Hello,

I have an Action where I define a ViewData property named Ad:

public ActionResult GoogleAdSense(string client, int height,
string slot, int width) {
StringBuilder ad = new StringBuilder();
ad.AppendLine("<script type='text/javascript'>");
ad.AppendLine("<!--");
ad.AppendLine(String.Concat("google_ad_client = '", client,
"';"));
ad.AppendLine(String.Concat("google_ad_slot = '", slot, "';"));
ad.AppendLine(String.Concat("google_ad_width = ",
width.ToString(), ";"));
ad.AppendLine(String.Concat("google_ad_height = ",
height.ToString(), ";"));
ad.AppendLine("//--> ");
ad.AppendLine("</script>");
ad.AppendLine("<script type='text/javascript' src='http://
pagead2.googlesyndication.com/pagead/show_ads.js'></script>");
ViewData["Ad"] = ad;
return View("GoogleAdSense");
}

Then I use, on the control:

<%= Html.Encode(ViewData["Ad"]) %>

This is not working because I get in my Page Source:

&lt;script type='text/javascript'&gt;
&lt;!--
google_ad_client = '###########';
google_ad_slot = '######';
google_ad_width = 300;
google_ad_height = 250;
//--&gt;
&lt;/script&gt;
&lt;script type='text/javascript' src='http://
pagead2.googlesyndication.com/pagead/show_ads.js'&gt;&lt;/script&gt;

The problem is with the '<' and '>' characters. Any idea why?

One more question: in C# when I want a quote inside a "" should I use
' ?
The generated code shows ' but in Google Ad Sense they say to use ".
I tried to use "" to create a " but I get an error on my code. Why?

Thanks,

Miguel

You should HTMLEncode the data that you are putting in the StringBuilder.
That ought to sort out your problem.

For the quote issue, you can escape the double quote to place it in a
string. Like so:

string s= "\"http://www.google.com\""
 
D

Dhruvin Gajjar

Shapper,

The problem why you are getting < and such encoded string in your browser is
because you are using HTMLEncode method to put your javascript onto your page.

Instead try the following on your aspx side and it will emit the exact
javascript block that you want.
<%= ViewData["Ad"] %>

--
Dot Net Developer


Rad said:
Hello,

I have an Action where I define a ViewData property named Ad:

public ActionResult GoogleAdSense(string client, int height,
string slot, int width) {
StringBuilder ad = new StringBuilder();
ad.AppendLine("<script type='text/javascript'>");
ad.AppendLine("<!--");
ad.AppendLine(String.Concat("google_ad_client = '", client,
"';"));
ad.AppendLine(String.Concat("google_ad_slot = '", slot, "';"));
ad.AppendLine(String.Concat("google_ad_width = ",
width.ToString(), ";"));
ad.AppendLine(String.Concat("google_ad_height = ",
height.ToString(), ";"));
ad.AppendLine("//--> ");
ad.AppendLine("</script>");
ad.AppendLine("<script type='text/javascript' src='http://
pagead2.googlesyndication.com/pagead/show_ads.js'></script>");
ViewData["Ad"] = ad;
return View("GoogleAdSense");
}

Then I use, on the control:

<%= Html.Encode(ViewData["Ad"]) %>

This is not working because I get in my Page Source:

<script type='text/javascript'>
<!--
google_ad_client = '###########';
google_ad_slot = '######';
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type='text/javascript' src='http://
pagead2.googlesyndication.com/pagead/show_ads.js'></script>

The problem is with the '<' and '>' characters. Any idea why?

One more question: in C# when I want a quote inside a "" should I use
' ?
The generated code shows ' but in Google Ad Sense they say to use ".
I tried to use "" to create a " but I get an error on my code. Why?

Thanks,

Miguel

You should HTMLEncode the data that you are putting in the StringBuilder.
That ought to sort out your problem.

For the quote issue, you can escape the double quote to place it in a
string. Like so:

string s= "\"http://www.google.com\""
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top