I need to call a javascript function from C# code after page Load, or is it there any way that I can do this on .aspx page itself??
Thanks
asked Aug 13, 2010 at 6:32
-
Thanks Guys for your answers as I mentioned I need to call the function after page load I did in a simple way, added that function in <Head> and call that function after page body...and it works..Thanx to all, so it done on .aspx page itself :)BreakHead– BreakHead2010年08月13日 06:51:07 +00:00Commented Aug 13, 2010 at 6:51
3 Answers 3
try with RegisterStartupScript
E.g:
RegisterStartupScript("Msg1", "<script language='javascript'> alert('Hello World') </script>");
answered Aug 13, 2010 at 6:33
-
+1 because I didn't know about the existance of this short version compared to the
ClientScriptManager.RegisterStartupScript
method. Looks like it's the same as callingPage.ClientScript.RegisterStartupScript(Page, Page.GetType(), "key", "MyFunc();", true);
, but much shorter, and more readableGiuseppe Accaputo– Giuseppe Accaputo2010年08月13日 06:43:14 +00:00Commented Aug 13, 2010 at 6:43
You can use this,
Page.ClientScript.RegisterStartupScript(Page.GetType(), "script",
"urfunction()", true);
answered Aug 13, 2010 at 6:35
string script = "..." // your script here without <script> tags
ClientScript.ClientScript.RegisterStartupScript(GetType(), "key", script, true)
Also if you want to use it directly from the .aspx you can use jquery
$(document).ready( function() {
//... your script here
});
answered Aug 13, 2010 at 6:33
-
You might want to replace
RegisterClientScript
withRegisterClientScriptBlock
, since noRegisterClientScript
method existsGiuseppe Accaputo– Giuseppe Accaputo2010年08月13日 06:44:09 +00:00Commented Aug 13, 2010 at 6:44
default