2

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
1
  • 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 :) Commented Aug 13, 2010 at 6:51

3 Answers 3

2

try with RegisterStartupScript

E.g:

 RegisterStartupScript("Msg1", "<script language='javascript'> alert('Hello World') </script>");
answered Aug 13, 2010 at 6:33
1
  • +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 calling Page.ClientScript.RegisterStartupScript(Page, Page.GetType(), "key", "MyFunc();", true);, but much shorter, and more readable Commented Aug 13, 2010 at 6:43
1

You can use this,

Page.ClientScript.RegisterStartupScript(Page.GetType(), "script", 
 "urfunction()", true);
answered Aug 13, 2010 at 6:35
1
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
1
  • You might want to replace RegisterClientScript with RegisterClientScriptBlock, since no RegisterClientScript method exists Commented Aug 13, 2010 at 6:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.