Click here to Skip to main content
Rate this: bad
good
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
Edited 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 at 10-Dec-12 5:06am
   
where can i place it,
Krunal Rohit at 10-Dec-12 5:08am
   
in javascript...
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 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 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 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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 515
1 OriginalGriff 498
2 Abhinav S 305
3 sanket saxena 295
4 thatraja 235
0 Sergey Alexandrovich Kryukov 8,482
1 OriginalGriff 4,830
2 Peter Leow 3,794
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 11 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid