Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
below is code which is used to regsiter javascript but is not working.
C#
protected void Page_Load(object sender, EventArgs e)
       {

            string scriptTest = @"
                               <script type='text\javascript'>
                                    $(document).ready(function(){
                                               alert('in code');
                                               $.jqplot.config.enablePlugins = true;
                                               var s1 = [2, 6, 7, 10];
                                               var ticks = ['a', 'b', 'c', 'd'];

                                               plot1 = $.jqplot('Div1', [s1], {
                                                   // Only animate if we're not using excanvas (not in IE 7 or IE 8)..
                                                   animate: !$.jqplot.use_excanvas,
                                                   seriesDefaults:{
                                                       renderer:$.jqplot.BarRenderer,
                                                       pointLabels: { show: true }
                                                   },
                                                   axes: {
                                                       xaxis: {
                                                           renderer: $.jqplot.CategoryAxisRenderer,
                                                           ticks: ticks
                                                       }
                                               },
                                            highlighter: { show: false }
                                           });

                                           $('#Div1').bind('jqplotDataClick',
                                               function (ev, seriesIndex, pointIndex, data) {
                                                   $('#info1').html('series: '+seriesIndex+', point: '+pointIndex+', data: '+data);
                                               }
                                           );
                                               });</script>";
           string script = "<script type=\"text/javascript\"> alert('hi'); </script>";

           ScriptManager.RegisterStartupScript(this.Page, GetType(), "ClosePopup", scriptTest, false);
       }

please suggest solution.
Thanks
Posted
v2
Comments
Sergey Alexandrovich Kryukov 28-Dec-15 18:09pm    
Solution for what? "Not working" is not informative. You did not explain what you want to achieve. You seemingly know how to register a startup script...
—SA

1 solution

To be blunt, this is a terrible way to deal with JavaScript. Instead, add an empty .JS file to your project and put the JavaScript in it. Then in the head of your page reference it using a script tag.

You can then call it when loading by using jQuery's document.ready .ready() | jQuery API Documentation[^] method.

Unless there is a very good reason, don't build big strings of script through C#.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Dec-15 22:06pm    
I pretty much agree, a 5. I would say, it can be useful to declare few JavaScrip data elements dynamically on the server side, to inject this data into JavaScript, but most of the client code (if such code is required) should be statically written JavaScript.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900