Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling Javascript on page load

Status
Not open for further replies.

mellomel70

Programmer
Nov 2, 2010
10
0
0
US
Hi - I have an ASP.Net 2.5 application and I need to call a javascript function when one of the pages loads initially. I've tried:

Code:
string ScriptToCall = @"EnableCheckboxes(" + cblDetails.ClientID + ", false);";
ClientScript.RegisterStartupScript(this.GetType(), "addScript", ScriptToCall, true);

where EnableCheckboxes is a javascript function on the page. This doesn't work (doesn't disable the CheckBoxList), although the EnableCheckboxes function DOES work when I call it in other ways, so I'm sure that the problem is that I'm not calling it properly on page load.

I've tried:

Code:
ClientScript.RegisterStartupScript(this.GetType(), "addScript", "alert('Hello');", true);

which DOES work, so I know I'm on the right track, but how do I do this?

Thanks!
 
If you want to know how to do this using .Net syntax, then you're probably better off asking in a .Net forum rather than here.

That aside, a cast-iron way of detecting page load is simply to hook into the window's 'onload' event. Here's how I would do it in Firefox / Chrome / Safari:

Code:
function pageLoad() {
   // do something
}

window.addEventListener('load', pageLoad, false);

You'll need to add code for IE compatibility, depending on your needs (investigate 'attachEvent').

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
An easier way is to simply add the function call to the body's onload event directly.

Code:
<body onload="myfuinction();">



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Alas, I think I need to do this via .Net because I only want to call this when the page first loads, not on every page load. I'll post on a .Net or C# forum, thanks!
 
A co-worker helped me out. Here's how you do it: Put the following code under the CheckBoxList definition on the aspx page:

Code:
<script type="text/javascript" language="javascript">
                                var controlID = '<%= cblDetails.ClientID %>';
var chk = document.getElementById(controlID);
chk.disabled = true;
</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top