Devi Prasad
Devi Prasad

Reputation: 839

How to display alert popup before execute next line in asp.net c#

I want to display alert/message in my web application. I tried the below code but the alert displays after all lines of code execution.

ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Only alert Message');", true); 

But i want to display i middle of line of code execution, is it possible if it possible please help me..

Upvotes: 1

Views: 992

Answers (1)

Rajesh
Rajesh

Reputation: 24955

As I have already commented:

You cannot. RegisterStartupScript as you can read will register a startup script not call it. Also this script will be called only when response is received by client and processed. You can try a workaround for your scenario. Create a hidden button, and define a function to be processed after alert to this button. Then in your script, show alert and call click of this button.

Your function might look like this:

public string myFunction(){
  int a = 123;
  .
  .
  .

  ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Only alert Message');", true); 

  .
  .
  .
  .
  .
  return str;

}

You have to split this function in 2 parts:

public string myFunction1(){
  int a = 123;
  .
  .
  .

  ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Only alert Message');", true); 

  return str;
}

public string myFunction2(){
  .
  .
  .
  .
  .

}

and associate myFunction1 to original event and myFunction2 to hidden button. Then update your script to:

ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Only alert Message'); document.getElementById('<% hiddenBtn.clientId %>').click()", true); 

This will call remaining code and process logic.

Note This is a work-around. Ideally you should try to split functionality and try and use background processing.

Upvotes: 2

Related Questions