stighy
stighy

Reputation: 7170

ASP.NET content page: how to run a script on "body" with "body onload=myfunc()"

i've added to the master page my script "myscript.js". Then, in a content page, i would like to load myscript() at startup (body onload).

How can i do this ?

Upvotes: 2

Views: 12052

Answers (5)

Dilip Kumar Choudhary
Dilip Kumar Choudhary

Reputation: 469

Assign id and server="runnat" in master page body tag

<body runat="server" id="body_master">

Then your aspx .CS file insert below code in pageLoad event

    HtmlGenericControl body = this.Master.FindControl("body_master") as HtmlGenericControl;

body.Attributes.Add("onLoad", "SessionTimeOuts();");

SessionTimeOuts() function should be defined in your .aspx file

Thank you

Upvotes: 0

HeathenWorld
HeathenWorld

Reputation: 117

this will add onload client side event to the master page's body tag:

in the master page:

body id="PageBody" runat="server"

in the content page:

HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("PageBody");

body.Attributes.Add("onload", "doSomething();");`

Upvotes: 2

sshow
sshow

Reputation: 9084

I use a ContentPlaceHolder in the master-page for this purpose. This makes it possible to include specific scripts in the <head> only when you want it.

Upvotes: 1

Daniel Dyson
Daniel Dyson

Reputation: 13230

Either use

Page.RegisterStartupScript

  if (!IsStartupScriptRegistered (key))
  {
      String script = "<script type=\"text/javascript\">" +
        "alert('Hello World');</" + "script>";
      RegisterStartupScript(key, script);
  }

Or you could use the JQuery library and add the function call to the document.ready function

http://jquery.com/

 $(document).ready(function(){
   // Add your function call here

 });

Upvotes: 3

Saurabh
Saurabh

Reputation: 11

The form has onLoad event. So use the tag in your Masterpage

Upvotes: 0

Related Questions