473,553 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Run Javascript function upon Page Load from Page Load event handle

Hello,

I have a asp.net 2.0 (vb.net) page, which uses a master page, and there is a
javascript function (myfunction) defined in the <headof that master page.
Is there a way to run the function from the page load event of the page?

I am able to run the function when there is a change in, say, a text box, by
adding the following to the page load event:
TextBox.Attribu tes.Add("onChan ge", "javascript:myf unction()")

However, the following gives me an error that "Attributes is not a member of
pagename":
Me.Attributes.A dd("onLoad", "javascript:myF unction()")

I'm trying to get myfunction to run when the page loads by adding the
correct code to Me.Load.

Thank you for your help.
Jun 27 '08 #1
5 3424
On 27 May, 23:29, Matcon <Mat...@discuss ions.microsoft. comwrote:
Hello,

I have a asp.net 2.0 (vb.net) page, which uses a master page, and there isa
javascript function (myfunction) defined in the <headof that master page.. *
Is there a way to run the function from the page load event of the page?

I am able to run the function when there is a change in, say, a text box, by
adding the following to the page load event:
TextBox.Attribu tes.Add("onChan ge", "javascript:myf unction()")

However, the following gives me an error that "Attributes is not a member of
pagename":
Me.Attributes.A dd("onLoad", "javascript:myF unction()")

I'm trying to get myfunction to run when the page loads by adding the
correct code to Me.Load.

Thank you for your help.
Are you confusing the server-side PageLoad event with the client-side
OnLoad event?

The PageLoad event is handled by the server. Any JavaScript contained
in the page file is passed transparently back to the client. Niether
IIS or the ASP.NET worker process understand JavaScript and cannot
meaningfully execute it during the rendering of the page.

Am I making any sense?
Jun 27 '08 #2
"Matcon" <Ma****@discuss ions.microsoft. comwrote in message
news:B9******** *************** ***********@mic rosoft.com...
I have a asp.net 2.0 (vb.net) page, which uses a master page, and there is
a
javascript function (myfunction) defined in the <headof that master
page.
OK.
Is there a way to run the function from the page load event of the page?
Yes.
I am able to run the function when there is a change in, say, a text box,
by
adding the following to the page load event:
TextBox.Attribu tes.Add("onChan ge", "javascript:myf unction()")
OK. FYI, the "javascript :" part is unnecessary...
However, the following gives me an error that "Attributes is not a member
of
pagename":
Me.Attributes.A dd("onLoad", "javascript:myF unction()")
Yes, it would do.

[Also, bear in mind that JavaScript is case-sensitive e.g. myfunction() is
not the same as myFunction()...]
I'm trying to get myfunction to run when the page loads by adding the
correct code to Me.Load.
1) Modify the MasterPage's <bodytag as follows:

<body id="objMasterBo dy" runat="server">

2) Add the following to your content page's Page_Load event:

Master.FindCont rol("objBody"). Attributes.Add( "onload", "myfunction();" )
Alternatively, if you want the function to run for every content page which
uses this MasterPage, then just do:

<body onload="myfunct ion();">
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #3
Thanks for that information - I think I'm close. I followed step 1 and 2,
but I'm getting an error that reads:

"'Attribute s' is not a member of 'System.Web.UI. Control'."

Thoughts on what I should try?
"Mark Rae [MVP]" wrote:
"Matcon" <Ma****@discuss ions.microsoft. comwrote in message
news:B9******** *************** ***********@mic rosoft.com...
I have a asp.net 2.0 (vb.net) page, which uses a master page, and there is
a
javascript function (myfunction) defined in the <headof that master
page.

OK.
Is there a way to run the function from the page load event of the page?

Yes.
I am able to run the function when there is a change in, say, a text box,
by
adding the following to the page load event:
TextBox.Attribu tes.Add("onChan ge", "javascript:myf unction()")

OK. FYI, the "javascript :" part is unnecessary...
However, the following gives me an error that "Attributes is not a member
of
pagename":
Me.Attributes.A dd("onLoad", "javascript:myF unction()")

Yes, it would do.

[Also, bear in mind that JavaScript is case-sensitive e.g. myfunction() is
not the same as myFunction()...]
I'm trying to get myfunction to run when the page loads by adding the
correct code to Me.Load.

1) Modify the MasterPage's <bodytag as follows:

<body id="objMasterBo dy" runat="server">

2) Add the following to your content page's Page_Load event:

Master.FindCont rol("objBody"). Attributes.Add( "onload", "myfunction();" )
Alternatively, if you want the function to run for every content page which
uses this MasterPage, then just do:

<body onload="myfunct ion();">
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #4
"Matcon" <Ma****@discuss ions.microsoft. comwrote in message
news:7B******** *************** ***********@mic rosoft.com...

[top-posting corrected]
>2) Add the following to your content page's Page_Load event:

Master.FindCon trol("objBody") .Attributes.Add ("onload", "myfunction();" )

Thanks for that information - I think I'm close. I followed step 1 and 2,
but I'm getting an error that reads:

"'Attribute s' is not a member of 'System.Web.UI. Control'."

Thoughts on what I should try?
Apologies - my fault.

Try something like this:

DirectCast(Mast er.FindControl( "objBody"),
HtmlGenericCont rol).Attributes .Add("onload", "alert('Hello') ;")

I think that should work...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #5
That worked! Thanks very much for your help.
"Mark Rae [MVP]" wrote:
"Matcon" <Ma****@discuss ions.microsoft. comwrote in message
news:7B******** *************** ***********@mic rosoft.com...

[top-posting corrected]
2) Add the following to your content page's Page_Load event:

Master.FindCont rol("objBody"). Attributes.Add( "onload", "myfunction();" )
Thanks for that information - I think I'm close. I followed step 1 and 2,
but I'm getting an error that reads:

"'Attribute s' is not a member of 'System.Web.UI. Control'."

Thoughts on what I should try?

Apologies - my fault.

Try something like this:

DirectCast(Mast er.FindControl( "objBody"),
HtmlGenericCont rol).Attributes .Add("onload", "alert('Hello') ;")

I think that should work...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4776
by: Erik Steffl | last post by:
I am trying to create a simple custom web spider using mozilla and javascript, the basic functionality is to open a website and then manipulate it using DOM (possibly opening links etc.). it seems like pretty easy task to do, the only pr0oblem is that I can't figure out how to wait for the website to load - I use mozilla security settings...
4
5530
by: Carlo Marchesoni | last post by:
I have a button that opens a new Window (some kind of search-window), which is fired using JavaScript (btnSearch.Attributes=".....";) Now I need to run some code behind code BEFORE this JavaScript runs. I tried to define another Button with an EventHandler associated, and from this (after doing my code) fire 'btnSearch.click()'. But I can't make...
6
2519
by: den 2005 | last post by:
Hi everybody, Question 1: How do you set the values from server-side to a client-side control or how do you execute a javascript function without a button click event? Question 2: How do you get response from a Confirm() popup window to uncheck all server-side checkboxes placed in a panle of a web user control? I am using ASP.Net 2.0, ...
0
7568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7492
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7772
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7526
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6109
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3537
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1106
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
808
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.