简体   繁体   中英

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..

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.

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