How to call a javascript fn on page load of aspx page

hi ,

 

i m new to dot net can any one post the code tocall a javascript fn on page load of aspx  page

0
prasath_s01
3/8/2008 8:58:46 AM
asp.net.client-side 24353 articles. 2 followers. Follow

9 Replies
689 Views

Similar Articles

[PageSpeed] 50

You would do it in the same way as any other type of web page:

<body onLoad="myfunction();"> 

 


Regards Mike
[MVP - ASP/ASP.NET]
My site
0
Mikesdotnetting
3/8/2008 10:16:51 AM

in ur page load

btnSumbit.Attributes.Add("onClick","Functionname(Param1,Param2.......)")

hope this helps

 


Saima Ghumman
--Mark the post as Answer if the Post helps you--
0
Sam
3/8/2008 10:22:57 AM

Sam.Dev:

in ur page load

btnSumbit.Attributes.Add("onClick","Functionname(Param1,Param2.......)")

 

That doesn't cause a function to be called on page load.  It merely wires up a button to a clientside event handler.  The function will only be executed when the button is clicked.

 


Regards Mike
[MVP - ASP/ASP.NET]
My site
0
Mikesdotnetting
3/8/2008 10:26:01 AM

Hi,

Welcome to the ASP.NET forums.

I think he does not want it on a button click. He wants the js method to be executed as soon as page loads.

The other way is :

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>Untitled Page</title>

<script type="text/javascript">

function ExecuteMe()

{

alert(
'executed');

}

</script>

</head>

<body>

<form id="form1" runat="server">

<div>

</div>

</form>

</body>

</html>

Code Behind 

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

Page.ClientScript.RegisterClientScriptBlock(
this.GetType(), "Something", "ExecuteMe()",true);

}

}

 

HTH,
Suprotim Agarwal


ASP.NET Grid With Excel Capabilities | 27 GridView Tips & Tricks | Hosting your site? | SQL Server Tips
0
Suprotim
3/8/2008 10:40:51 AM
thanx ..but i need to call the function from my aspx page.....
0
prasath_s01
3/8/2008 1:11:51 PM

thanx ......but i have to call the function from aspx page on page load

0
prasath_s01
3/8/2008 1:14:46 PM

Hi If you want to call the page from Markup(.aspx page) then you should go with the Mike's Suggestion i.e.  <body onload="Test()">

0
itsdinesh123
3/8/2008 1:55:22 PM

Hi,

Well the code given precisely does that.

Could you explain in detail what is your requirement.

 

HTH,
Suprotim Agarwal


ASP.NET Grid With Excel Capabilities | 27 GridView Tips & Tricks | Hosting your site? | SQL Server Tips
0
Suprotim
3/8/2008 2:00:07 PM

The samples that have been offered do that.  Bear in mind that the server-side Page_Load event is for executing server side code before the actual html is created by ASP.NET and then sent to the browser.  Once it has been sent to the browser, there is also a client side event called onload, which is when clientside functions can be called. Javascript is a clientside technology.  It can't be executed in the server-side Page_Load event.

 


Regards Mike
[MVP - ASP/ASP.NET]
My site
0
Mikesdotnetting
3/8/2008 2:53:01 PM
Reply: