简体   繁体   中英

How do I stop a page refresh when C# code behind is executed and a JavaScript startup script is registered?

I have a button that executes code behind in c#. The code behind gets some information from the database and writes a fairly long piece of JavaScript that it then registers and runs on the client. The code behind looks something like this:

protected void Button1_Click(object sender, EventArgs e)
{
   System.Text.StringBuilder sb = new System.Text.StringBuilder();
   sb.Append(@"
        <script language='javascript'>
        //Lots of JavaScript
       </script>");
   System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "JCall1", sb.ToString(), false);
}

The code works and does what it should. The problem is between the press of the button and the execution of the JavaScript, I get a page reload flash. I would like to stop that flash. I can tell it is happening before the JavaScript executes because one of the first things I do in the JavaScript is start a processing icon. It spins while some promises are resolved/handled. The flash occurs before the icon starts processing. Once the JavaScript starts (icon appears and spins), everything is good. It is probably something I don't understand in the lifecycle of this button. I'm surmising that when I RegisterStartupScript, the page is refreshing to load the new script. I can't do it all in JavaScript Client-Side because internal data needs to be provided to the API's. Yes, it would be great if the site was.Net Core and I could do it all in a Razor Page but this is not what I am dealing with. It's WebForms. There may not be anything I can do about it but if there is, I could use some help.

I think what you're looking for is for the button click to execute asynchronously. This other SO post should help you craft it to your needs: Calling async method on button click

Yes, it would be great if the site was.Net Core and I could do it all in a Razor Page but this is not what I am dealing with.

Poorly placed sarcasm aside, the answer is the same in both a modern Asp.Net MVC Core application and in legacy WebForms: nothing you wrote here implies server-side code, so don't put it on the server side.

If you want to handle a button and make it execute some JavaScript code (ie, on the client, which is what your block is doing), simply register a handler on the client side and write your JS code there:

$('#<%= Button1.ClientID %>').click(function() {
    // code here
});

Because otherwise what you're doing is wrong on so many levels, not the least of which is the fact that ScriptManager.RegisterStartupScript works by inserting a <script> tag in the page itself, and it should be obvious to you that it requires reloading the entire page for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM