1

I have used below code to show the alert messages from code behind in asp.net.

Home.aspx:

 <asp:ScriptManager runat="server" ID="smHome"></asp:ScriptManager>

Home.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
      ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowStartStatus", "javascript:alert('Process is started');", true);
      LoadGridView();
   }
}

protected void LoadGridView() 
{
   //ADO.NET code here
   ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowCompletedStatus", "javascript:alert('Process is completed');", true);
}

But both of the alert messages have been shown continuously after completing the ADO.NET process that is available in LoadGridView() method. I want to show the first alert message before LoadGridView() method. Also I need to show the second alert message after completing ADO.NET process. I have also tried ClientScriptManager.RegisterClientScriptBlock method to show these alert messages. But it is also not working as expected. How to resolve the above issue?

RGS
  • 5,131
  • 6
  • 37
  • 65
  • 1
    Do you realize that the backend finishes, the front end gets to run? The front end and backend do not run together. – epascarello Mar 01 '19 at 14:51
  • 1
    "I want to show the first alert message before LoadGridView() method". You can't. You appear not to understand your page lifecycle. Basically when your browser tries to load your aspx page, it sends a HTTP request to the server. ASP.NET runs the code in your aspx code-behind. Then it returns the resulting data (i.e. a web page, which may include HTML, CSS and JavaScript) to the browser. Then the server stops executing the code. The page is now in the user's browser. At that point, any JavaScript code included in the page will be compiled and executed by the browser. – ADyson Mar 01 '19 at 14:53
  • @ADyson, Is there any alternate ways available to achieve the above requirement? – RGS Mar 01 '19 at 14:55
  • 1
    No more .NET code runs again until the browser makes another request to the server (e.g by clicking on a link or submitting a form or something). If you wanted to display a message on screen while your gridview is loading, then you'd have to load the page, display the message, and the trigger the loading of the gridview via an AJAX request (in ASP.NET WebForms you can often implement this nicely by using an UpdatePanel control), so that the loading happens in a separate request to the server (and an AJAX request specifically doesn't cause the rest of your page to re-load in the meantime). – ADyson Mar 01 '19 at 14:55
  • I suggest you study the ASP.NET page lifecycle documentation provided by Microsoft, and/or more general tutorials / guides which explain the HTTP lifecycle in general. And maybe read this as well: https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – ADyson Mar 01 '19 at 14:56
  • @ADyson, Thank you for your valuable inputs. Please add these comments as an answer. I will accept the answer. – RGS Mar 01 '19 at 14:57

2 Answers2

2

"I want to show the first alert message before LoadGridView() method".

You can't. You appear not to understand your page's lifecycle.

Basically when your browser tries to load your aspx page, it sends a HTTP request to the server. ASP.NET runs the code in your aspx code-behind. Then it returns the resulting data (i.e. a web page, which may include HTML, CSS and JavaScript) to the browser. Then the server stops executing the code. The page is now in the user's browser. At that point, any JavaScript code included in the page will be compiled and executed by the browser. No more .NET code runs again until the browser makes another request to the server (e.g by clicking on a link or submitting a form or something).

In summary - JavaScript and .NET code run at different times, in different contexts. You can't make them interlock like that using a conventional lifecycle.

If you wanted to display a message on screen while your gridview is loading, then you'd have to load the page, display the message, and the trigger the loading of the gridview via an AJAX request (in ASP.NET WebForms you can often implement this nicely by using an UpdatePanel control), so that the loading happens in a separate request to the server (and an AJAX request specifically doesn't cause the rest of your page to re-load in the meantime).

ADyson
  • 57,178
  • 14
  • 51
  • 63
-1

Have you tried this?

ASP.NET Web Application Message Box

ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + message+ "');", true);
Ben
  • 1
  • 2
  • I think you missed the point of the question. It's not about which JS code to use, it's about the overall process. – ADyson Mar 01 '19 at 17:11