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

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>");
Sign up to request clarification or add additional context in comments.

1 Comment

+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
1

You can use this,

Page.ClientScript.RegisterStartupScript(Page.GetType(), "script", 
   "urfunction()", true);

Comments

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
});

1 Comment

You might want to replace RegisterClientScript with RegisterClientScriptBlock, since no RegisterClientScript method exists

Your Answer

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.