Unable to get the RegisterClientScriptBlock() method to work when passing "true" as last Boolean parameter

251 views Asked by At

Here are the 3 versions tried to include scripts in page:

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "JqueryTestFile1",
     "<script type=\"text/javascript\" src=\"jqueryTEST1.js\"></script>", false);

Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "JqueryTestFile2",
     "jquery-1.11.0.min.js");

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "JqueryTestFile3",
     "src=\"jqueryTEST3.js\"", true);

The First two versions are working fine . However the 3rd one with "true" as the last parameter is actually rendering <![CDATA[ in source code and I can't get this to work.

Why is it rendering CData ? What purpose does it serves in this context? How can I get the third version working without removing the "true" parameter ? Below is the html code rendered in browser::

<form method="post" action="ScriptsManager.aspx" id="Form1">
     <script type="text/javascript" src="jqueryTEST1.js"></script>
     <script src="jquery-1.11.0.min.js" type="text/javascript"></script>
     <script type="text/javascript">
          //<![CDATA[
          src="jqueryTEST3.js"//]]>
     </script>
     <input type="button" value="ClickMe" onclick="DoClick()" />     
</form>

Additional Info::

[ Just for avoiding any confusion that all these JS files: JqueryTest1.js etc.. are present at proper location in my solution ]. In JqueryTest3.js, have only below code:

function DoClick(){
alert("Hi");
}

If I place this DoClick() function in JqueryTest1.js file, it works perfectly.

.aspx Markup:

<form id="Form1" runat="server">
  <input type="button" value="ClickMe" onclick="DoClick()" />
 </form>
1

There are 1 answers

0
King of kings On

Replace the code by the following

    ScriptManager.RegisterStartupScript(this,this.GetType(), "JqueryTestFile3",
 "src=\"jqueryTEST3.js\"", true)

Remember to add to your page