How to make a simple yes/no popup in ASP.NET that return the result back to my c#?

前端 未结 7 979
再見小時候
再見小時候 2020-12-21 01:05

With ASP.NET, how do I prompt the user for a yes/no question and getting the result back to my .ascx?

So far I can open a confirmation dialog with use of Javascript,

相关标签:
7条回答
  • 2020-12-21 01:26

    This is not a good practice to do this. you can get your confirm using javascript and postback or callback result to server.

    but if you want to do this, this will help you :

    A Simple ASP.NET Server Control: Message Box & Confirmation Box

    0 讨论(0)
  • 2020-12-21 01:26

    You need to use ajax, or to make a postback to the server. Your c# code is server side and the javascript is client side. If you use the ajax extensions for asp .net you can use javascript page methods:

    PageMethods.YourMethod(confirm('your text'), OnSuccess, OnFailure);
    
    0 讨论(0)
  • 2020-12-21 01:32

    If you insist on using webforms, another solution could be the AJAX Control kit. Simply create a ModalPopup and have you confirm buttons inside that.

    Read more here: http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/ModalPopup/ModalPopup.aspx

    0 讨论(0)
  • 2020-12-21 01:34

    You can use standart JavaScript confirm() function to show popup and do Post Back in case of Yes or No. For example:

    if (confirm('Question')) {
        __doPostBack('', 'Yes_clicked');
    } else {
        __doPostBack('', 'No_clicked')
    }  
    

    Then on server in Page_Load() method do:

    if (IsPostBack)
    {
        var result = Request.Params["__EVENTARGUMENT"];
    }
    

    You can also do it async by specifying the first parameter of __doPostBack() function as ID of any update panel.

    0 讨论(0)
  • 2020-12-21 01:36

    I use this. As far as I know it prevents the rest of the button event from executing.

    btnMyButton.Attributes.Add("onClick", "return confirm('Are you really sure?')");
    
    0 讨论(0)
  • add this in head of source

    function confirm_Edit() { if (confirm("Are you sure want to Edit?")==true) return true; else return false; }

    call it like this

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