0

I am using ASP.NET 3.5.

I have a content page and I want to call a javascript function on this page's load event.

I tried adding:

onload="GetLocalDate();"

within the content page placeholder tag, but it is not working. But when I call this function from any button's OnClientClick event, it works.

How to make it work on Content Page's load event?

RKh
  • 13,818
  • 46
  • 152
  • 265
  • What does this "GetLocalDate()" do? Does it reference elements or just the date object? Any code block would be appreciated. – War10ck Dec 05 '12 at 18:39

4 Answers4

0

Have you tried calling from document.ready?

$(document).ready(function () {
    GetLocalDate();
}

Put that inside script tag on your page

Felipe Skinner
  • 16,246
  • 2
  • 25
  • 30
  • hmmm thats wierd... see if this question helps http://stackoverflow.com/questions/8830074/what-is-the-different-between-window-onload-init-and-window-onload-init – Felipe Skinner Apr 09 '13 at 17:08
0

The content page "Placeholder" tag is a server side only control. It doesn't produce any code on the client other than arranging its contents etc. As such, the JavaScript onload handle is never rendered.

Examine your browser / client-side source to verify this.

andleer
  • 22,388
  • 8
  • 62
  • 82
0
<script type="text/javascript">
function pageLoad(){
    GetLocalDate();
}
</script>
Joel S
  • 1
0
$(document).ready(function () {
    GetLocalDate();
}

Should work. Since it was not working for you, I would assume that you do not have a reference to the jQuery library in your page.

If you don't want to include the jQuery library in your project for some reason, you could inject it from server-side code within your content page:

ClientScriptManager cs = Page.ClientScript; cs.RegisterStartupScript(...) <-- add necessary details here (the Type, scriptname, the text, and a Boolean to whether you need it to include its own tags)

You should also check to make sure it hasn't already been registered before using it though (IsStartupScriptRegistered).

dougczar
  • 585
  • 7
  • 8