Confirmation message box in webapplication

后端 未结 8 1645
刺人心
刺人心 2020-12-19 19:16

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 r

相关标签:
8条回答
  • 2020-12-19 19:20

    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" />
    
    0 讨论(0)
  • 2020-12-19 19:22
    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;
        }    
    }
    
    0 讨论(0)
  • 2020-12-19 19:23

    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();"/>
    
    0 讨论(0)
  • 2020-12-19 19:28

    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 讨论(0)
  • 2020-12-19 19:33

    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>
    
    0 讨论(0)
  • 2020-12-19 19:36

    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

    0 讨论(0)
提交回复
热议问题