Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to call javascript function in asp.net pageload event only on pageload

i tried it, i got the output but it is executed on all the time that means when i click the button it gets executed so that if any body tell how can i execute that function only once on pageload

i used this code
<body  onload="Return myfunction();"
Posted
Updated 10-Dec-12 20:56pm
v2

C#
window.onload = function()
                {
                   Function();
                };
 
Share this answer
 
Comments
Member 9644686 10-Dec-12 5:06am    
where can i place it,
[no name] 10-Dec-12 5:08am    
in javascript...
Hi,

I assume you are using AJAX control in your page,
you can call your JAVASCRIPT function by using SCRIPTMANAGER control in your page_load event like

if(!ispostback)
{
 ScriptManager.RegisterStartupScript(this, this.GetType(), "KeyClient", "<script>return myFunction();;</Script>", false);
}
 
Share this answer
 
C#

C#
if (!IsPostBack)
  
{
          
ddlTaskLevel.Attributes.Add("onchange", "javascript:return function()"); 
} // for example i took a drop down box. 


place this in page load in code behind.
 
Share this answer
 
v3
HI,

you can try like this also.

XML
<head runat="server">
    <title>Calling javascript function from code behind example</title>
        <script type="text/javascript">
            function showDialogue() {
                alert("this dialogue has been invoked through codebehind.");
            }
        </script>
</head>
..........

lblJavaScript.Text = "<script type='text/javascript'>showDialogue();</script>";


Or you can also try like the below code in page load.

Page.ClientScript.RegisterClientScriptBlock(typeof(ScriptManager), "CallMyMethod", "myMethod();");


Thanks
 
Share this answer
 
inside head tag


window.onload = function()
{
Function();
};
 
Share this answer
 
Comments
CHill60 8-May-14 8:15am    
No different to Solution 1 posted a year and a half ago
Dileep Goplakrishnan 7-Jan-15 3:44am    
Thanks bro...
You want to add the event only first time or when Page Load first. You can either check IsPostBack property within Page_Load event and Add the JS functional if it is False. Whenever the page is posted back, the IsPostBack becomes True, so it won't be added again.

C#
protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
        {
           btnYourButton.Attributes.Add("onclick","JSFunctionaName();");
        }
}
 
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