0

I have a block of code in my C# that executes on a button click that looks like so:

protected void LoadButton(object sender, EventArgs e)
{
    LoadDoc();
    ReadXML();

    ScriptManager.RegisterClientScriptBlock(loadXML, this.GetType(), "FormMath", "FirstCalculation();", true);
}

With that last line, I want to call a JS function called 'FirstCalculation'. The problem is that FirstCalculation exists not in the default.aspx file, but in an external js file that we'll call external.js. I can't (shouldn't) move FirstCalculation away from external.js, but I still need to call it on this one button click. Of course, I need to be able to call up some ASP as well.

The end goal is such that I'm able to call an ASP function and Javascript function in that order on a button click.

Also worth noting, I have no idea what the third argument is supposed to be in the RegisterClientScriptBlock call. I copied that from a different resource so if that needs to be something specific, that might be a thing to look at.

edit: A lot of the other examples I ran into seem to be referencing code that is inside the default.aspx page and that's where my hang up is, since this isn't in the same file.

1 Answer 1

2

Rather than using RegisterClientScriptBlock use RegisterStartupScript

This will ensure that the javascript your'e injecting will appear at the bottom of the page - thus it will be after your .js file. Should work fine then..

See here for more info: RegisterStartupScript On MSDN

Also - the 3rd parameter (true) is telling your code to include the <script> tags in whatever javascript your injecting; that is to say, the RegisterClientScriptBock & RegisterStartupScript will first inject the <script> tags and then put your string in the middle of them. You need this to be true in your case. You would set it to false if you were injecting "<script>FirstCalculation();</script>"

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

Comments

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.