4

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);
Milton Fernando
  • 125
  • 1
  • 3
  • 10
  • 2
    On confirmation what you want to do a client side function or a server side call ?? – yogi Aug 08 '12 at 09:15

8 Answers8

7

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" />
Curtis
  • 101,612
  • 66
  • 270
  • 352
3

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>
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
0

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
}
Clayton
  • 457
  • 2
  • 8
0

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

Community
  • 1
  • 1
yogi
  • 19,175
  • 13
  • 62
  • 92
0

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();"/>
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Krunal Mevada
  • 1,637
  • 1
  • 17
  • 28
0
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();
    }
}
0

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.

0
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;
    }    
}
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Dinima
  • 1