Confirm Message Box with OK or Cancel option in C#

Last post 02-20-2009 12:25 AM by susain. 6 replies.

Sort Posts:

  • Confirm Message Box with OK or Cancel option in C#

    02-17-2009, 3:46 AM
    • Member
      3 point Member
    • divseep
    • Member since 06-30-2008, 5:59 AM
    • Posts 18

    Hi  All,

    I have a function call which  results in two conditions(True and False).When the condition is false,I wanted the code to display a message box with the two options (OK and Cancel) and when the user clicks the OK button It should direct to another page.

    Please anyone help me with the code ( in C# ) to display a confirm message box with this two options.Its urgent.

    Thanks for your help.

     

     

  • Re: Confirm Message Box with OK or Cancel option in C#

    02-17-2009, 4:12 AM
    private void button3_Click(object sender, System.EventArgs e) { if (MessageBox.Show("Really delete?","Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes) { // a 'DialogResult.Yes' value was returned from the MessageBox // proceed with your deletion } }
    Avantha Siriwardana
    Beware of bugs in the above code; I have only proved it correct, not tried it.
    (Donald Knuth)
    http://www.linkedin.com/in/avanthasiriwardana
  • Re: Confirm Message Box with OK or Cancel option in C#

    02-19-2009, 9:50 PM
    Answer

    divseep:

    Hi  All,

    I have a function call which  results in two conditions(True and False).When the condition is false,I wanted the code to display a message box with the two options (OK and Cancel) and when the user clicks the OK button It should direct to another page.

    Please anyone help me with the code ( in C# ) to display a confirm message box with this two options.Its urgent.

    Thanks for your help.

     

     

    Please refer to the following code:

        protected void Button1_Click(object sender, EventArgs e)
        {
            ClientScriptManager CSM = Page.ClientScript;
            if (!ReturnValue())
            {
                string strconfirm = "<script>if(!window.confirm('Are you sure?')){window.location.href='Default.aspx'}</script>";
                CSM.RegisterClientScriptBlock(this.GetType(), "Confirm", strconfirm, false);
            }
        }
        bool ReturnValue()
        {
            return false;
        }
     
    Gary yang - MSFT
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Confirm Message Box with OK or Cancel option in C#

    02-19-2009, 10:08 PM
    Answer
    • Participant
      1,696 point Participant
    • nishanthnair
    • Member since 11-08-2005, 5:50 AM
    • India
    • Posts 303

    If you can use modl popup, your app will look neat.

    http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ModalPopup/ModalPopup.aspx

    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.

    http://blog.nishanthnair.com
  • Re: Confirm Message Box with OK or Cancel option in C#

    02-20-2009, 12:08 AM
    • Member
      8 point Member
    • iyngarangce
    • Member since 07-30-2008, 6:00 AM
    • Posts 4

    Hi you can use this script for getting confirmation from the user

    function ConfirmSave()
        {       
            var Ok = confirm('Are you sure want to save the changes?');
            if(Ok)
                return true;
            else
                return false;
        }
    ---------------------------------------//For redirection to the URL

    function ConfirmCancel(URL)
        {       
            var Ok = confirm('Are you sure want to Cancel the changes?');
            if(Ok == true)
            {
                document.location.href=URL;           
            }       
            //else
                //return false;
        }

    In server side you can add this line for adding attributes of onclick()

    btnSave.Attributes["onclick"] = "javascript:return ConfirmSave();";           

    btnCancel.Attributes["onclick"] = "javascript:ConfirmCancel('InspectionNew.aspx');";

     

    Thanks & regards

    Dhinesh paramasivam

     

    thanks & Regards
    Dhinesh paramasivam
  • Re: Confirm Message Box with OK or Cancel option in C#

    02-20-2009, 12:20 AM

    You can do like this..

            if (true)
            {
                //Your code
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "open", "show();", true);
            }

     And

        <script type="text/javascript">
            function show()
            {
                if(confirm('r u sure?'))
                {
                    window.location.replace('yourpage.aspx');
                }
            }
        
     

     

    Regards,
    Ram Reddy Mekha, +91-994-840-4315
    http://abhiramreddymekha.blogspot.com

    Please Mark as Answer if the post helps you.
  • Re: Confirm Message Box with OK or Cancel option in C#

    02-20-2009, 12:25 AM
    • Contributor
      2,324 point Contributor
    • susain
    • Member since 10-29-2007, 7:51 AM
    • Hyderabad, Andhra Pradesh, India
    • Posts 646

    Hi,

    Take a hidden field, label and button and write the javascript function between the head tags as shown below

    <div>

    <asp:HiddenField ID="hdnbox" runat="server" />

    <br />

    <asp:Label ID="lblResult" runat="server"></asp:Label><br />

    <asp:Button ID="Button1" runat="server" Text="Button" /></div>

    <SCRIPT type="text/javascript" language="javascript">function getMessage()

    {

    var ans; ans=window.confirm('Is it your confirmation.....?'); //alert (ans); if (ans==true)

    {

    //alert('Yes');

    document.Form1.hdnbox.value='Yes';

    }

    else

    {

    //alert('No');

    document.Form1.hdnbox.value='No';}

    }

    </SCRIPT>

    And in the code behind write this code

    Page load event

    Button1.Attributes.Add("onclick", "getMessage()")

    button click event

    If Request.Form("hdnbox") = "Yes" Then

    lblResult.Text = "You have clicked Ok" ' Write the logic when Ok is clicked i.e., redirect to pag1.aspx lets say

    Else

    lblResult.Text = "You have clicked Cancel" ' Write the logic when Cancel is clicked i.e., redirect to pag2.aspx lets say or do nothing

    End If

    Hope this will help! 

    Thanks,

    Farooq

    Linked In: http://www.linkedin.com/in/mohammedfarooq123

    Mark as answer if this post help full to you.
Page 1 of 1 (7 items)