locked
Writing JavaScript from a Custom Control RRS feed

  • Question

  • User1982844092 posted

    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?

    Tuesday, June 30, 2009 5:39 PM

Answers

  • User1306565749 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 1, 2009 1:41 AM
  • User401360897 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 1, 2009 2:54 AM

All replies

  • User1383809551 posted

    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

     

    Tuesday, June 30, 2009 5:49 PM
  • User1982844092 posted

    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
                                                });
                                            });");

    Tuesday, June 30, 2009 5:59 PM
  • User1383809551 posted

    Hi,

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

    Thanks

    Tuesday, June 30, 2009 7:49 PM
  • User1982844092 posted

    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.

    Tuesday, June 30, 2009 8:23 PM
  • User1383809551 posted

     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, "">"");
                                };"
                );

    Tuesday, June 30, 2009 9:32 PM
  • User1306565749 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 1, 2009 1:41 AM
  • User401360897 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 1, 2009 2:54 AM
  • User1982844092 posted

    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?

    Wednesday, July 1, 2009 9:08 AM