Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have an javascript Function in Abc.aspx file

ex :

JavaScript
<script type="text/javascript">
     Function ShowChart(){
        //Calling Google API and 
        // Diplaying Chart 
    }    
    </script>


<pre lang="HTML">
<input id="btnPlay" type="button"  runat="server" value="Play"  önclick="initialize()"
                style="width: 80px; height: 30px;" />
<asp:Button ID="loadCh" runat="server" Text="Load Next Value" Width="120px" />


here the Above Java Script Works Fine When i click button "btnPlay" and chart is displayed
but when i click button "loadCh" a post back occurs and i loses the chart value so i need to display the chart again.
Note button "LoadCh" will retrieve data from Server and populates in text boxes and all.

after post back i want my ShowChart() Java script to be executed!

i have tried this below Code in code behind
VB
'System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>")
'System.Web.HttpContext.Current.Response.Write("ShowChart();")
'System.Web.HttpContext.Current.Response.Write("</SCRIPT>")
' another Code <pre>

'Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "script", "ShowChart();", True)
'Page.ClientScript.RegisterStartupScript(Me.GetType(), "script", "ShowChart();", True)


but none of the Code Fired the function ShowChart()

Any any one help me with a solution
Posted
Comments
Prasad Khandekar 16-May-13 10:44am    
Hello Diler,

Have you tried using JQuery. You can use code something like below to invoke ShowChart function on every page load.

$(document).ready(function() {
ShowChart();
});

Regards,
Sergey Alexandrovich Kryukov 16-May-13 11:43am    
Of course, this is just the very convenient way and a good point (you could put it as the formal answer), but the root of the problem is: JavaScript is no called from code behind.
Please see my answer where I tried to explain some basics.
—SA
Diler Patel 17-May-13 1:52am    
I m using an master page and content page i dont have body and onload().

is there any solution for this ??

regards
Sergey Alexandrovich Kryukov 17-May-13 1:57am    
You do have the innerHtml of the body in your content page. I already answered on this part. If you put script at this level, it will also be executed.

And Prasad's code sample will make the handler function executed when the document is ready.

—SA
Diler Patel 17-May-13 1:54am    
hi Prasad

i dont know about jQuery can u tell me where to write that piece of code
i wrote something like this

<script type="text/jscript">
$(document).ready(function () { ShowChart(); });
</script>

but it didnt worked can u correct me ?

You cannot "fire" JavaScript functions from code behinds, because code behind is executed on the server side, and JavaScript — on client side. This is not how it works. If you understand ASP.NET, you can see that you can generate any thinkable content, including HTML page with JavaScript, but it always happens in HTTP response, in response to HTTP request. In other words, when JavaScript can be executed on some HTML page, the whole code behind is already completed its execution.

In other words, you cannot call a JavaScript function from code behind, but you can inject a script calling it when a page is loaded. Note that the script of the onload attribute and the script written as the child element of the "body" will be called when the page is loaded.

—SA
 
Share this answer
 
Use RegisterStartupScript function.

ScriptManager.RegisterStartupScript(this, this.GetType(), "Showdata", "Showdata();", true);

ClientScript.RegisterStartupScript(GetType(), "Showdata", "Showdata();", true);
:) Enjoy
 
Share this answer
 
v2
Try this,

ScriptManager.RegisterStartupScript(Page, typeof(Page), "MyScriptShowChart", "ShowChart();", true);
 
Share this answer
 

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