-3

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.

Steve Cross
  • 89
  • 1
  • 13

2 Answers2

1

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

EspressoBeans
  • 1,747
  • 12
  • 22
  • I followed this and I was able to determine how to run the JS on the client-side. Instead of constructing my JS in the code-behind, I did put it in my aspx file. I used some hidden fields to make the data available to the JS. I still get the flash but it is cleaner. Thanks. – Steve Cross Jun 10 '21 at 21:27
1

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.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • So, how do I get the data that I need from my database? Are you saying I should access the database directly from the JavaScript? I thought that was bad. I am using server-side to get data from SQL that needs to be in my JavaScript API request. I'm really sorry to be such a neophyte. I suppose this is not the best place to try to learn how to do something. I will probably be banned from asking questions since I have so many down votes. Not too encouraging for me trying to learn. – Steve Cross Jun 10 '21 at 20:46
  • You're imagining things, there's no database access in your post. But if there were, I'd remind you about AJAX calls. – Blindy Jun 10 '21 at 21:22
  • Part of my original post said: I can't do it all in JavaScript Client-Side because internal data needs to be provided to the API's. – Steve Cross Jun 10 '21 at 21:24
  • That's a false implication, you don't need to write your code on the server side just because you need to provide data to it, you just choose to. Just how you choose to use obsolete technology like WebForms. I'll say it again, the answer to that particular problem is AJAX. – Blindy Jun 10 '21 at 21:27
  • Thank you. I feel better about how my code is structured even though it still does the flash. I always appreciate a good piece of condescension. Have a nice day. – Steve Cross Jun 10 '21 at 21:38