473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.Attributes.Add("onChange", "javascript:myfunction()")

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

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 3419
On 27 May, 23:29, Matcon <Mat...@discussions.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.Attributes.Add("onChange", "javascript:myfunction()")

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

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****@discussions.microsoft.comwrote in message
news:B9**********************************@microsof t.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.Attributes.Add("onChange", "javascript:myfunction()")
OK. FYI, the "javascript:" part is unnecessary...
However, the following gives me an error that "Attributes is not a member
of
pagename":
Me.Attributes.Add("onLoad", "javascript:myFunction()")
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="objMasterBody" runat="server">

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

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

<body onload="myfunction();">
--
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:

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

Thoughts on what I should try?
"Mark Rae [MVP]" wrote:
"Matcon" <Ma****@discussions.microsoft.comwrote in message
news:B9**********************************@microsof t.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.Attributes.Add("onChange", "javascript:myfunction()")

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

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="objMasterBody" runat="server">

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

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

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

Jun 27 '08 #4
"Matcon" <Ma****@discussions.microsoft.comwrote in message
news:7B**********************************@microsof t.com...

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

Master.FindControl("objBody").Attributes.Add("onl oad", "myfunction();")

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

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

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

Try something like this:

DirectCast(Master.FindControl("objBody"),
HtmlGenericControl).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****@discussions.microsoft.comwrote in message
news:7B**********************************@microsof t.com...

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

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

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

Thoughts on what I should try?

Apologies - my fault.

Try something like this:

DirectCast(Master.FindControl("objBody"),
HtmlGenericControl).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
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...
4
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...
6
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.