473,498 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Executing Javascript on Page Load

I've created some javascript and added it to my page (from a user control)
using Page.ClientScript.RegisterClientScriptBlock. The javascript hides and
shows certain elements in response to radio buttons being selected.

It seems to work fine except that I really need to run the script when the
page loads so the required elements are shown or hidden (some of these
elements do not have runat="server").

So, I use the following code:

Page.Form.Attributes.Add("onload", initFunction);

Where initFunction is something like "OnUserFood()".

This seems to have no effect. And looking at the resulting HTML, I notice
that my script appears within the form instead of in the header. I'm not
sure if this matters. But, although the onload attribute correctly appears
in the form tag, again, it doesn't appear to be actually making the call.

Any suggestions?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Feb 14 '08 #1
2 4633

"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:OC**************@TK2MSFTNGP02.phx.gbl...
I've created some javascript and added it to my page (from a user control)
using Page.ClientScript.RegisterClientScriptBlock. The javascript hides
and shows certain elements in response to radio buttons being selected.

It seems to work fine except that I really need to run the script when the
page loads so the required elements are shown or hidden (some of these
elements do not have runat="server").

So, I use the following code:

Page.Form.Attributes.Add("onload", initFunction);

Where initFunction is something like "OnUserFood()".

This seems to have no effect. And looking at the resulting HTML, I notice
that my script appears within the form instead of in the header. I'm not
sure if this matters. But, although the onload attribute correctly appears
in the form tag, again, it doesn't appear to be actually making the call.

Any suggestions?
Page.ClientScript.RegisterClientStartupScript

Feb 14 '08 #2
Err... That would make sense, wouldn't it?

Haven't used that before but will investigate further.

Thanks!

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Scott Roberts" <sr******@no.spam.here-webworks-software.comwrote in
message news:OT**************@TK2MSFTNGP03.phx.gbl...
>
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:OC**************@TK2MSFTNGP02.phx.gbl...
>I've created some javascript and added it to my page (from a user
control) using Page.ClientScript.RegisterClientScriptBlock. The
javascript hides and shows certain elements in response to radio buttons
being selected.

It seems to work fine except that I really need to run the script when
the page loads so the required elements are shown or hidden (some of
these elements do not have runat="server").

So, I use the following code:

Page.Form.Attributes.Add("onload", initFunction);

Where initFunction is something like "OnUserFood()".

This seems to have no effect. And looking at the resulting HTML, I notice
that my script appears within the form instead of in the header. I'm not
sure if this matters. But, although the onload attribute correctly
appears in the form tag, again, it doesn't appear to be actually making
the call.

Any suggestions?

Page.ClientScript.RegisterClientStartupScript
Feb 14 '08 #3

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

Similar topics

2
29794
by: Mark Anderson | last post by:
Problem in short: user is moving (clicking a link) from my page before some JS code is run (to write a cookie). The code does not run (in Body's onLoad event) until the page loads as there are a...
4
5488
by: seans | last post by:
Hi, Is there some way from preventing the JavaScript code in a document loaded into an iframe from executing? I don't have access to the pages being loaded into the i-frame so I can't modify...
5
7122
by: ZildjianB | last post by:
Hi All, I hope you can help me with this, as I have done some exhaustive searching over the past couple days and cannot find a solution. What I am trying to do is dynamically update an image...
7
5223
by: Jibey | last post by:
Hello: I'm facing a very strange problem. When I run my Web application in Visual Studio.NET the Page_Load event is not executing. Other events like a Button_Click are executing. It doesn't...
3
2425
by: Mike | last post by:
Hi, I am trying to resize a HTML table through Javascript. When the user control loads the first time, the table is resized, but then it doesn't anymore. I am using the following code in the...
2
1526
by: John Walker | last post by:
Hi, On my Webform my ASP:Button does not execute the Javascript code which is 'added' to it on the page load, and I'm trying to figure out why. Please take a look at my code and the error message...
2
1277
by: cider123 | last post by:
Here's a quick snapshot of the top few lines of the aspx page: <%@ Page Language="C#" MasterPageFile="~/Master1.master" AutoEventWireup="true" CodeFile="TestPage.aspx.cs" Inherits="TestPage"...
7
2793
by: tshad | last post by:
I thought I understood how the SaveViewState is working and was trying to use this (as per some code I found) to detect refreshes. It seemed to be working but I found that the SaveViewState was...
13
2482
by: mowsen | last post by:
Hello Group, i'm using a little "ajax" loader script to dynamically load files into different "div" tags on my main site. the code for this part looks like: function loader() { var args =...
7
6015
by: jphaycock | last post by:
Hi all I am calling a method in a Page_Load event that makes use of a value from a text box. Unfortunately the textbox value is only set after a javascript runs and the page is rendered....
0
7125
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
7002
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
7203
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
5462
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,...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3093
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...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
290
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...

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.