String

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    String

    Hello,

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

    public ActionResult GoogleAdSense(s tring client, int height,
    string slot, int width) {
    StringBuilder ad = new StringBuilder() ;
    ad.AppendLine(" <script type='text/javascript'>");
    ad.AppendLine(" <!--");
    ad.AppendLine(S tring.Concat("g oogle_ad_client = '", client,
    "';"));
    ad.AppendLine(S tring.Concat("g oogle_ad_slot = '", slot, "';"));
    ad.AppendLine(S tring.Concat("g oogle_ad_width = ",
    width.ToString( ), ";"));
    ad.AppendLine(S tring.Concat("g oogle_ad_height = ",
    height.ToString (), ";"));
    ad.AppendLine("//--");
    ad.AppendLine(" </script>");
    ad.AppendLine(" <script type='text/javascript' src='http://
    pagead2.googles yndication.com/pagead/show_ads.js'></script>");
    ViewData["Ad"] = ad;
    return View("GoogleAdS ense");
    }

    Then I use, on the control:

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

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

    &lt;script type='text/javascript'&gt;
    &lt;!--
    google_ad_clien t = '###########';
    google_ad_slot = '######';
    google_ad_width = 300;
    google_ad_heigh t = 250;
    //--&gt;
    &lt;/script&gt;
    &lt;script type='text/javascript' src='http://
    pagead2.googles yndication.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.S.

    #2
    Re: String

    On Aug 25, 1:54 pm, shapper <mdmo...@gmail. comwrote:
    Hello,
    >
    I have an Action where I define a ViewData property named Ad:
    >
        public ActionResult GoogleAdSense(s tring client, int height,
    string slot, int width) {
          StringBuilder ad = new StringBuilder() ;
          ad.AppendLine(" <script type='text/javascript'>");
          ad.AppendLine(" <!--");
          ad.AppendLine(S tring.Concat("g oogle_ad_client = '", client,
    "';"));
          ad.AppendLine(S tring.Concat("g oogle_ad_slot = '", slot, "';"));
          ad.AppendLine(S tring.Concat("g oogle_ad_width = ",
    width.ToString( ), ";"));
          ad.AppendLine(S tring.Concat("g oogle_ad_height = ",
    height.ToString (), ";"));
          ad.AppendLine("//--");
          ad.AppendLine(" </script>");
          ad.AppendLine(" <script type='text/javascript' src='http://
    pagead2.googles yndication.com/pagead/show_ads.js'></script>");
          ViewData["Ad"] = ad;
          return View("GoogleAdS ense");
        }
    >
    Then I use, on the control:
    >
    <%= Html.Encode(Vie wData["Ad"]) %>
    >
    This is not working because I get in my Page Source:
    >
    &lt;script type='text/javascript'&gt;
    &lt;!--
    google_ad_clien t = '###########';
    google_ad_slot = '######';
    google_ad_width = 300;
    google_ad_heigh t = 250;
    //--&gt;
    &lt;/script&gt;
    &lt;script type='text/javascript' src='http://
    pagead2.googles yndication.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:


    and this:


    or do a search on "C# escape"

    Comment

    • Rad [Visual C# MVP]

      #3
      Re: String

      On Mon, 25 Aug 2008 10:54:25 -0700 (PDT), shapper wrote:
      Hello,
      >
      I have an Action where I define a ViewData property named Ad:
      >
      public ActionResult GoogleAdSense(s tring client, int height,
      string slot, int width) {
      StringBuilder ad = new StringBuilder() ;
      ad.AppendLine(" <script type='text/javascript'>");
      ad.AppendLine(" <!--");
      ad.AppendLine(S tring.Concat("g oogle_ad_client = '", client,
      "';"));
      ad.AppendLine(S tring.Concat("g oogle_ad_slot = '", slot, "';"));
      ad.AppendLine(S tring.Concat("g oogle_ad_width = ",
      width.ToString( ), ";"));
      ad.AppendLine(S tring.Concat("g oogle_ad_height = ",
      height.ToString (), ";"));
      ad.AppendLine("//--");
      ad.AppendLine(" </script>");
      ad.AppendLine(" <script type='text/javascript' src='http://
      pagead2.googles yndication.com/pagead/show_ads.js'></script>");
      ViewData["Ad"] = ad;
      return View("GoogleAdS ense");
      }
      >
      Then I use, on the control:
      >
      <%= Html.Encode(Vie wData["Ad"]) %>
      >
      This is not working because I get in my Page Source:
      >
      &lt;script type='text/javascript'&gt;
      &lt;!--
      google_ad_clien t = '###########';
      google_ad_slot = '######';
      google_ad_width = 300;
      google_ad_heigh t = 250;
      //--&gt;
      &lt;/script&gt;
      &lt;script type='text/javascript' src='http://
      pagead2.googles yndication.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\ ""
      --

      Comment

      • =?Utf-8?B?RGhydXZpbiBHYWpqYXI=?=

        #4
        Re: String

        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 [Visual C# MVP]" wrote:
        On Mon, 25 Aug 2008 10:54:25 -0700 (PDT), shapper wrote:
        >
        Hello,

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

        public ActionResult GoogleAdSense(s tring client, int height,
        string slot, int width) {
        StringBuilder ad = new StringBuilder() ;
        ad.AppendLine(" <script type='text/javascript'>");
        ad.AppendLine(" <!--");
        ad.AppendLine(S tring.Concat("g oogle_ad_client = '", client,
        "';"));
        ad.AppendLine(S tring.Concat("g oogle_ad_slot = '", slot, "';"));
        ad.AppendLine(S tring.Concat("g oogle_ad_width = ",
        width.ToString( ), ";"));
        ad.AppendLine(S tring.Concat("g oogle_ad_height = ",
        height.ToString (), ";"));
        ad.AppendLine("//--");
        ad.AppendLine(" </script>");
        ad.AppendLine(" <script type='text/javascript' src='http://
        pagead2.googles yndication.com/pagead/show_ads.js'></script>");
        ViewData["Ad"] = ad;
        return View("GoogleAdS ense");
        }

        Then I use, on the control:

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

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

        <script type='text/javascript'>
        <!--
        google_ad_clien t = '###########';
        google_ad_slot = '######';
        google_ad_width = 300;
        google_ad_heigh t = 250;
        //-->
        </script>
        <script type='text/javascript' src='http://
        pagead2.googles yndication.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\ ""
        --

        >

        Comment

        Working...