Writing JavaScript from a Custom Control

Last post 07-01-2009 9:08 AM by dba123. 8 replies.

Sort Posts:

  • Writing JavaScript from a Custom Control

    06-30-2009, 5:39 PM
    • Contributor
      2,724 point Contributor
    • dba123
    • Member since 12-12-2003, 7:04 PM
    • Posts 1,336

    I'm new to writing custom controls.  I have MyCustomControl.cs and in my Render method I want to render out about 50 lines of JavaScript.  What's the best way to do this, use the writer?

    protected override void Render(HtmlTextWriter writer)
            {
              

                writer.Write(@"
                                function decode(s)
                                {
                                    return s.replace(/&/g, ""&"")
                                    .replace(/"/g, '""')
                                    .replace(/'/g, ""'"")
                                    .replace(/&lt;/g, ""<"")
                                    .replace(/&gt;/g, "">"");
                                };"
                );

    I plan on having around 6 more writer.Write to write out some more sections here.  Is that the best approach to actually perform the writing of JavaScript in this manor?

    or should I use ClientScript.RegisterClientScriptBlock?

    When is Microsoft going to get rid of VB.NET!
  • Re: Writing JavaScript from a Custom Control

    06-30-2009, 5:49 PM
    • Contributor
      5,624 point Contributor
    • RatheeshC
    • Member since 04-25-2008, 6:05 PM
    • Posts 1,198

    You can decare a stringbuilder class.. initiate it and append the script code

    to the string builder..and finally you could use

    writer.write(stringbuildername here)

    eg:

    StringBuilder sb = new StringBuilder();
    sb.Append("<script type=\"text\\javascript\" language=\"javacsript\">");
    sb.Append("function TestFf(){  alert(\"hi\")");

    writer.write(sb);

    Thanks

     

    Thanks
    Ratheesh

    Please mark it as answer if it resolves your issue.
  • Re: Writing JavaScript from a Custom Control

    06-30-2009, 5:59 PM
    • Contributor
      2,724 point Contributor
    • dba123
    • Member since 12-12-2003, 7:04 PM
    • Posts 1,336

    What if I want to retain indentation so that the JavaScript is actually readable once rendered in source?  Would this retain indentation as I have it?


    StringBuilder carouselJavaScript = new StringBuilder();

                carouselJavaScript.Append(
                                            @"jQuery(document).ready(function()
                                                {
                                                    $('#mycarousel').jcarousel({
                                                    itemLoadCallback: mycarousel_itemLoadCallback
                                                });
                                            });");

    When is Microsoft going to get rid of VB.NET!
  • Re: Writing JavaScript from a Custom Control

    06-30-2009, 7:49 PM
    • Contributor
      5,624 point Contributor
    • RatheeshC
    • Member since 04-25-2008, 6:05 PM
    • Posts 1,198

    Hi,

    You have to do that from your code behind manually usig string.PadLeft() and PadRight() function

    Thanks

    Thanks
    Ratheesh

    Please mark it as answer if it resolves your issue.
  • Re: Writing JavaScript from a Custom Control

    06-30-2009, 8:23 PM
    • Contributor
      2,724 point Contributor
    • dba123
    • Member since 12-12-2003, 7:04 PM
    • Posts 1,336

    this is being spit out from a custom control, not code behind.  This is a plan .cs file like MyCustomControl.  They will not have a code behind.

    When is Microsoft going to get rid of VB.NET!
  • Re: Writing JavaScript from a Custom Control

    06-30-2009, 9:32 PM
    • Contributor
      5,624 point Contributor
    • RatheeshC
    • Member since 04-25-2008, 6:05 PM
    • Posts 1,198

     Hi,

    So where are you going tto write these statements..?

    protected override void Render(HtmlTextWriter writer)
            {
              

                writer.Write(@"
                                function decode(s)
                                {
                                    return s.replace(/&amp;/g, ""&"")
                                    .replace(/&quot;/g, '""')
                                    .replace(/&#039;/g, ""'"")
                                    .replace(/&lt;/g, ""<"")
                                    .replace(/&gt;/g, "">"");
                                };"
                );

    Thanks
    Ratheesh

    Please mark it as answer if it resolves your issue.
  • Re: Writing JavaScript from a Custom Control

    07-01-2009, 1:41 AM
    Answer

    dba123:

    I'm new to writing custom controls.  I have MyCustomControl.cs and in my Render method I want to render out about 50 lines of JavaScript.  What's the best way to do this, use the writer?

    protected override void Render(HtmlTextWriter writer)
            {
              

                writer.Write(@"
                                function decode(s)
                                {
                                    return s.replace(/&amp;/g, ""&"")
                                    .replace(/&quot;/g, '""')
                                    .replace(/&#039;/g, ""'"")
                                    .replace(/&lt;/g, ""<"")
                                    .replace(/&gt;/g, "">"");
                                };"
                );

    I plan on having around 6 more writer.Write to write out some more sections here.  Is that the best approach to actually perform the writing of JavaScript in this manor?

    or should I use ClientScript.RegisterClientScriptBlock?

     

    Hi,

    If you are developing custom control and you want to use JavaScrip code. And it should be easily maintainable and readable for developer
    Then you can write your JavaScript code in separate .js file. and use this JavaScript file as a embedded resource in your custom control dll.
    So that your javascript code will be easily maintanable, readable as well as there is no deployment issue...because the .js fiel is embadded in the 
    custom Controls dll.

    For this method

    1) create one javascript file in the custom Control project
    2) Add one namespace level attribut in your code
    [assembly: WebResource("namespace.className.AsynchronousRequestManager.js", "text/javascript")]
    3) And include this javascript code in your custom control

     if (!Page.ClientScript.IsClientScriptIncludeRegistered(GetType(), "MyPortal_AsynchronousRequestManager"))
                    Page.ClientScript.RegisterClientScriptInclude(GetType(), "MyPortal_AsynchronousRequestManager", Page.ClientScript.GetWebResourceUrl(GetType(), "namespace.classname.AsynchronousRequestManager.js"));            

     

    If you you want some more information , then search as 'Embadded Web resource in .net

     

    Please mark this post as answer, if it helped

    Thanks,
    Tushar Galankar
    Approva System Private Ltd.
    www.approva.net
  • Re: Writing JavaScript from a Custom Control

    07-01-2009, 2:54 AM
    Answer
    • All-Star
      17,485 point All-Star
    • imran_ku07
    • Member since 06-04-2008, 1:21 PM
    • KARACHI, PAKISTAN
    • Posts 3,195

    ClientScript.RegisterClientScriptBlock is better where you have need to render only once for a given key while writer.Write will write as many times as you can.

    By the Performace is the key for writer.write

  • Re: Writing JavaScript from a Custom Control

    07-01-2009, 9:08 AM
    • Contributor
      2,724 point Contributor
    • dba123
    • Member since 12-12-2003, 7:04 PM
    • Posts 1,336

    Thanks all.. let me reiterate some more here.


    1) We don't really have a concern of the js being in the dll.  We're not sending this to lets say a consumer but yea I get your point regardless. 

    2) Does it really matter how I render the final JavaScript?  Meaning to me it seems like I have several choices and no matter which way you dice it, they're all gonna do the same thing..spit out the JavaSCript one way or another.  So I see my options are:

    a) Use the HttpTextWriter...yea you have to specify the start and end tag of the javascript but so what...

    b) Use the RegisterClientScriptInclude or just RegisterClientScript (if not including it in a .js)

    c) If you want to append the INamingContainer clientID you could also use Page.ClientScript.RegisterExpandoAttribute but in my case I don't need to do this for the jQuery that I'll be rendering out.


    am I right on?

    When is Microsoft going to get rid of VB.NET!
Page 1 of 1 (9 items)