简体   繁体   中英

Javascript Popup After Postback

I want to display a Javascript dialog box after postback. Then if user clicks "OK", do something. If user clicks "Cancel", do something else.

Plz help.

protected void Page_Load(object sender, EventArgs e)
  {
    if (IsPostBack)
    {
      ClientScript.RegisterClientScriptBlock(GetType(), "Javascript",
      "<script>window.onload=function(){return confirm('Are you sure?');};</script>");
    }
  }

This might help...

Client Side Code

<script type="text/javascript">
    function getConfirm() {
        var ans = confirm('Are you sure?');
        if(ans==true)
        {
           //User clicked ok
        }
        else
        {
          //User clicked cancel
        }
    }
</script>

Server Side Code

protected void Page_Load(object sender, EventArgs e)
  {
    if (IsPostBack)
    {
      Page.ClientScript.RegisterStartupScript(this.GetType(), null, "getConfirm();", true);
    }
  }

@Administrateur : your code looks correct as per what you wanted to do, i even tried it and it pops up confirm box correctly for any post back like i tried with a your code in page load and just a single button on page and button click brings the confirm box,

so i would ask you are you invoking any postback while trying/testing to see this confirm box, as your code is all correct, so i guess only thing you might be missing is you are not testing it with a real postback rather trying it wihtout making a postback for example trying it on first page load itself.

could you tell us how actually you arrived to conclusion that it's not working? May be that will help us help u better?

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