Confirmation message box in webapplication

Am using the below message box in asp.net web application. Now i want to convert this message box as a confirmation message box and do something when it is true else means reject the application.

   ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('Are you sure, you want to apply?');</script>", false);

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I think you are going about this the wrong way. You should display this confirmation before posting back, and then only post back if they choose to “Apply”.

Using ASP.NET web controls, the Button control has an OnClientClick property which can be used to call javascript prior to Http POST:

You could do something like this:

<asp:button id="btn"
            runat="server"
            Text="Apply"
            OnClientClick="return confirm('Are you sure you wish to apply?');"
            OnClick="btn_Click" />

Method 2

register script bellow instead of alert

<script type="text/javascript">
var r=confirm("Are you sure you are sure?")
if (r==true)
{
  //You pressed OK!
}
else
{
  //You pressed Cancel!
}
</script>

Method 3

The javascript equivalent to a confirmation box is the confirm() method. This method returns a true or false value depending on the user’s “OK” or “Cancel” button response.

Usage:

var confirmed = confirm('Are you sure?','Are you sure you want to delete this item?');

if(confirmed){
  //do something
} else {
  //do something else
}

Method 4

Try this

Add this on your cs file to display a confirm instead of alert

string confirm = 
"if(confirm('Are you surely want to do this ??')) __doPostBack('', 'confirmed');";
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", confirm, true);

Add this on same page to check when user is coming from that confirmation box
protected void Page_Load(object sender, EventArgs e)
{
    string parameter = Request["__EVENTARGUMENT"];
    if (string.Equals("confirmed", 
                        parameter, 
                        StringComparison.InvariantCultureIgnoreCase))
    {
        // Call your server side method here
    }
}

For this I used __doPostBack you can learn more about it from here.
Hope it’ll help you

Method 5

private void showMessage(string msg){

        ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('"+ msg +"');</script>", false);

protected void BtnReg_Click(object sender, EventArgs e)
{
        OracleHelper.OracleDBOpen();
        object flag = OracleHelper.OracleExecuteScalar("your select Query ");
        if (flag == null)
        {
                      showMessage("Failed !!! ");

        }
        else
        {
            string reg = String.Format("your Insert Query ");

            showMessage("successfuly");
            OracleHelper.OracleExecuteNonQuery(reg);

        }                
        OracleHelper.OracleDBClose();
    }
}

Method 6

To do this completely within C#, you can try this:

    protected override void OnInit(EventArgs e)
    {  
        AddConfirmationButton();   
        base.OnInit(e);
    }

    private void AddConfirmationButton()
    {   
        Button confirmButton = new Button();
        confirmButton.Text = "Action Foo";
        string confirmationMessage = "Are you sure you wish to do action Foo?";
        confirmButton.OnClientClick = "return confirm('" + confirmationMessage + "');";
        confirmButton.Command += confirmButton_Command;

        Controls.Add(confirmButton);

    }

    void confirmationMessage_Command(object sender, CommandEventArgs e)
    {
        DoActionFoo();   //work your magic here.
    }

This presents and “OK/Cancel” dialog box to the user from the webpage. If the user clicks ‘ok’, the function from the command Event fires. If the user clicks ‘cancel’, nothing happens.

Method 7

ScriptManager.RegisterStartupScript(page,this.GetType(), "temp","javascript:calopen();
",true);

function calopen()    
{    
    if (confirm("Are you sure?"+'n'+"Are you want to delete"))    
    {   
        enter code here    
    }
    else   
    {   
        return false;
    }    
}

Method 8

try this code check on OnClientClick event when user click on button

<script type="text/javascript">
      function check()
     {          
        var chk =confirm("Are you sure you are sure?")
        if (chk==true)
        {
            // try you want
        }
        else
        {
            // try you do not want
        }
       return true;
     }

</script>        

 <asp:button id="Button1"
        runat="server"
        Text="Button1"
        OnClientClick="return check();"/>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x