admin管理员组

文章数量:1312923

I have a few Database things to read in the Page_Load of my Asp page.

if (xxx == 1)
  //Add OnLoad("clock()")

How to do this? How to add a OnLoad-Method for a JavaScript clock in the page load method?

I have a few Database things to read in the Page_Load of my Asp page.

if (xxx == 1)
  //Add OnLoad("clock()")

How to do this? How to add a OnLoad-Method for a JavaScript clock in the page load method?

Share Improve this question asked Apr 25, 2011 at 10:45 PassionateDeveloperPassionateDeveloper 15.2k35 gold badges111 silver badges190 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

1st Make your body tag a servercontrol

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

2nd reference it like

if (xxx == 1)  
  BodyTag.Attributes.Add("onload", "clock()");

Use the RegisterClientScriptBlock method to add client code to the page:

Page.ClientScript.RegisterClientScriptBlock(
  this.GetType(),
  "onload",
  "window.onload = function(){ clock(); }",
  true
);

Register a startup script in the Page_Load. ClientScriptManager (http://msdn.microsoft./en-us/library/z9h4dk8y.aspx) or ScriptManager (http://msdn.microsoft./en-us/library/bb310408.aspx)... whichever is appropriate.

string clockStuff =
    @"function callClockFunction(){ 
    Sys.Application.remove_load(callClockFunction); 
    clock(); }
    Sys.Application.add_load(callClockFunction);";
ScriptManager.RegisterStartupScript(this, this.GetType(), "callClockFunction", clockStuff, true);

本文标签: javascriptAdd OnLoad() in PageLoad()Stack Overflow