0

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 2

1

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

4 Comments

This will run before server code though, I need server code to be run first so need to trigger it from code behind.
then call your clientside funtion at the end of your serverside code
@user1166905: are you using UpdatePanel ?
Perfect, that last one did it although had to change like so: ScriptManager.RegisterStartupScript(this,GetType(), "Javascript", "javascript:displayMessageBox(); ", true);
1

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

Sorry yes it does there but I need to run it after a button click event on server side!

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.