Javascript – confirm message box

asp.netcjavascriptpopup

I have to show a yes no popup messagebox for a function>

This is what i do for an alert popup>

  Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script>alert('File Updated');</script>");

This is what i want to do in the code behind:

if (ID != 0)
{
     Page.ClientScript.RegisterStartupScript(this.GetType(), "Confirm", "<script>confirm('are you sure?');</script>");

    if (yes)
    {
        perform function
    }
    else
    {
        return;
    }

}

The confirm is not working,,, any suggestions on how to do this
thanks

Edit Portion:

  1. Navigate to a page
  2. Add values to a textbox
  3. Click "Save" Button to add value to database
  4. Ask the user if he is sure he want to do it 'are you sure'?,, the confirm pop up
  5. Now the above confirm box will only happen if the ID is != 0 or else there is no need for a popup.
  6. if he says yes then add to database and show alert popup that values have been enterd in the DB.
  7. if NO then just dont add to Db and just return.

so i get the confirm box like this.. but how can i get what is selected

string scriptString = "<script language='JavaScript'> ";
            scriptString += "confirm ('Are you sure you want to Close this period.')";
            scriptString += "</script>";
            Response.Write(scriptString);

Best Answer

Is there a button you are clicking on to trigger the action? If so, you should add the client events to your web control like so:

<asp:ImageButton runat="server" ID="DeleteUrlImageButton" ImageUrl="~/Images/icon_remove.gif"
  OnClick="DeleteUrlImageButton_Clicked"
  OnClientClick="return confirm('Are you sure you want to delete?');" />

If the user selects yes, the postback happens as usual. If they select no, the even is cancelled and no postback occurs. This, IMO, is the way to handle it because it prevents any extra server activity if they select no.