Example -> have Default.aspx with button on it. I want in Button_Click() procedure in Default.aspx.cs to call some JScript function that I wrote in section of Default.aspx. Is this possible and how???
if (!Page.IsStartupScriptRegistered("ExecuteJSFunction"))
Page.RegisterStartupScript("ExecuteJSFunction", "<script>MyJSFunction();</script>");
The first line is not necessary, but good practice to include. It checks that the script is not emitted twice which could happen if it were in a control. Page.RegisterStartupScript() drops the JS just before
the closing </form> tag in the resulting html page (ie. the response to the request), while Page.RegisterClientScriptBlock() drops the JS just after the opening <form> tag. Either will do in your case since your JS function is defined in the head. I've found
RegisterStartupScript() should be used if it expects the page to have already been loaded, and RegisterClientScriptBlock() if you want to do something before the page has loaded. Dylan.
It's OK all you said it but I can just put OnMouseDown="MyScript()" in as attribute and think it will be same result ;(( So there isn't some magic command like Page.StartJavaScript ( nameOfScript ) ?? ;( Tnx for help anyway
zmsystems_de...
Member
35 Points
7 Posts
Can I activate JScript from C#?
Nov 25, 2003 09:50 PM|LINK
dylanhogg
Member
72 Points
15 Posts
Re: Can I activate JScript from C#?
Nov 26, 2003 02:11 AM|LINK
if (!Page.IsStartupScriptRegistered("ExecuteJSFunction")) Page.RegisterStartupScript("ExecuteJSFunction", "<script>MyJSFunction();</script>");The first line is not necessary, but good practice to include. It checks that the script is not emitted twice which could happen if it were in a control. Page.RegisterStartupScript() drops the JS just before the closing </form> tag in the resulting html page (ie. the response to the request), while Page.RegisterClientScriptBlock() drops the JS just after the opening <form> tag. Either will do in your case since your JS function is defined in the head. I've found RegisterStartupScript() should be used if it expects the page to have already been loaded, and RegisterClientScriptBlock() if you want to do something before the page has loaded. Dylan.Colt
All-Star
15569 Points
1986 Posts
ASPInsiders
MVP
Re: Can I activate JScript from C#?
Nov 26, 2003 02:15 AM|LINK
zmsystems_de...
Member
35 Points
7 Posts
Re: Can I activate JScript from C#?
Nov 27, 2003 12:56 AM|LINK