1

I've found other solution on how to executes a javascript function from code-behind. The problem is that it firing before the page loads, so I can't add logic to it.

if(result)
{
   Page.ClientScript.RegisterClientScriptBlock(typeof(Page), 
                                               "Script", "confirmPwdChange();", true);
}

On the client side

<script>
   confirmPwdChange()
   {
     alert("Password has been successfully changed.");
     //Add more logic here, after user clicks the OK button...
   }
</script>

No Jquery, if possible.

Thanks for helping.

1

2 Answers 2

2

You might want to include the window.onload in your script file. something like this..

StringBuilder script = new StringBuilder();
script.Append("var existingHandler = window.onload;");
script.Append("window.document.body.onload = function(){ confirmPwdChange(); if (existingHandler){ existingHandler()}}");
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), 
                                           "Script", script.ToString(), true);

this ensure, your change password is called on page load and retains any other existing handlers declared already on the page.

Sign up to request clarification or add additional context in comments.

7 Comments

I'm getting this error: Uncaught SyntaxError: Unexpected token <.
my bad. corrected the code to not include <script> tags
Now I'm getting the error: Uncaught ReferenceError: changePassword is not defined at window.onload
I gave as a reference method name, replace the changePassword to confirmPwdChange in the server code. updated the snipet
It executed but, the same problem as it executed before the page completely loaded. While the alert was displayed, page went blank then loaded.
|
1

I think you're looking for PageRequestManager

List of PageRequestManager events

var prm = Sys.WebForms.PageRequestManager.getInstance();

// beginRequest: before postback to server
prm.add_beginRequest(BeginRequestHandler);

// endRequest: after postback to server
prm.add_endRequest(EndRequestHandler);

function BeginRequestHandler(sender, args) {
    // do stuff
}

function EndRequestHandler(sender, args) {
    // do stuff
}

1 Comment

Can you please show how to link this to the success/failure of the code executed in the code behind.

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.