running java script on page loading
-
hi!, there is a page, say, my.aspx. in this there is a .... block contains a function say func(). i want whenever my.aspx page loaded this function func() get executed on every post back.
-
You can call that javascript function on onload event of the body tag in the html section.
Best Regards, Apurva Kaushal
-
Thanks! but i have to passed some values from code behind from to that java script function.
-
Thanks! but i have to passed some values from code behind from to that java script function.
-
exactly same way you can do as psamy said. also you can even write complete function in code behind itself.
Best Regards, Apurva Kaushal
-
something like this you can use..:
string str = ""; str += "function Focu(){"; str += "document.getElementById('TextBox3').focus();}<"; str += "/"; str += "script>"; if(!IsStartupScriptRegistered("Test")) { Page.RegisterStartupScript("Test",str); }</code> <div class="ForumSig">Best Regards, Apurva Kaushal</div></x-turndown>
-
something like this you can use..:
string str = ""; str += "function Focu(){"; str += "document.getElementById('TextBox3').focus();}<"; str += "/"; str += "script>"; if(!IsStartupScriptRegistered("Test")) { Page.RegisterStartupScript("Test",str); }</code> <div class="ForumSig">Best Regards, Apurva Kaushal</div></x-turndown>
-
Thanks! but i have to passed some values from code behind from to that java script function.
There are several ways. You could insert a value in the function:
var x = <asp:Literal id="objValue" runat="server" />;
and in the codebehind insert the value:objValue.Text = "42";
You could make the body tag accessible from codebehind:<body id="Body" runat="server">
and set the onload event from codebehind:Body.Attributes["onload"] = "somefunc(42);";
You could add the code in a script tag:this.ClientScript.RegisterStartupScript(this.GetType(), "init", "somefunc(42);", true);
Note that if you use RegisterStartupScript the code will run while the page is loading, not when it has finished loading, as it does when you use the onload event.--- b { font-weight: normal; }