Click here to Skip to main content
12,508,422 members (53,600 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 9-Dec-12 23:03pm
Updated 10-Dec-12 20:56pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

window.onload = function()
                {
                   Function();
                };
  Permalink  
Comments
Member 9644686 10-Dec-12 5:06am
   
where can i place it,
Krunal Rohit 10-Dec-12 5:08am
   
in javascript...
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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);
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

HI,

you can try like this also.

<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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

inside head tag
  Permalink  
Comments
CHill(Hawkeye)60 8-May-14 8:15am
   
No different to Solution 1 posted a year and a half ago
Member 10759813 7-Jan-15 3:44am
   
Thanks bro...
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.

protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
        {
           btnYourButton.Attributes.Add("onclick","JSFunctionaName();");
        }
}
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160927.1 | Last Updated 8 May 2014
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100