Is it possible to run a one off client side script from an asp.net server control?
I know you can use Page.ClientScript.RegisterStartupScript() on a normal page but obviously doesn't work on server control.
2 Answers
use on onClientClick="Javascript_FuncName" attribute
sample
<asp:Button ID="Button1" runat="server" Text="SAVE & SUBMIT"
OnClick="BtnSave_Click" onClientClick="return CheckFunction"></asp:Button>
As you want to run your serverside code 1st then your client side, then at the end of server side code add this line
ClientScript.RegisterStartupScript(GetType(), "Javascript",
"javascript:FUNCTIONNAME(); ", true);
If you using UpdatePanel then try like this
ScriptManager.RegisterStartupScript(GetType(), "Javascript",
"javascript:FUNCTIONNAME(); ", true);
4 Comments
user1166905
This will run before server code though, I need server code to be run first so need to trigger it from code behind.
Satinder singh
then call your clientside funtion at the end of your serverside code
Satinder singh
@user1166905: are you using UpdatePanel ?
user1166905
Perfect, that last one did it although had to change like so: ScriptManager.RegisterStartupScript(this,GetType(), "Javascript", "javascript:displayMessageBox(); ", true);
I know you can use Page.ClientScript.RegisterStartupScript() on a normal page but obviously doesn't work on server control.
Yes, it does work on a server control. For example, you can call Page.ClientScript.RegisterStartupScript in your server control's OnPreRender method.
1 Comment
user1166905
Sorry yes it does there but I need to run it after a button click event on server side!