Display a “Yes / No” alert box in C# code behind

2019-02-17 23:36发布

I am trying to display a "Yes / No" messagebox from codebehind in C#. I want to call an "AddRecord" procedure if the user clicks "Yes", and do nothing if the user clicks "No".

Ideally, I want to use the code below, but from codebehind:

OnClientClick = "return confirm('Are you sure you want to delete?');"

I search on SO and google, but was not able to find anything helpful.

6条回答
成全新的幸福
2楼-- · 2019-02-17 23:58

on your Add Record button, just do the following:

    <asp:button ID="AddRecordbutton" runat="server" Text="Add Record"
 onclick="AddRecordButton_Click" onclientclick="return confirm('add record?');" />

In your code behind, just put the add record code in your AddRecordButton_Click event handler. It will only be called if they click Yes on the popup.


Alternatively, you could have your codebehind assign the onclientclick code when the button is initially rendered.

For example:

protected void Page_Load(object sender, EventArgs e) {
  AddRecordButton.OnClientClick = @"return confirm('Add Record?');";
}
查看更多
我只想做你的唯一
3楼-- · 2019-02-18 00:04

If you use the Ajax Control Toolkit Modal Popup Extender on a Panel with your two buttons this will fire an event on the server which can be handled and execute whichever method/functions you wish

See here for an example

查看更多
Juvenile、少年°
4楼-- · 2019-02-18 00:05

Use RegisterStartupScript

ScriptManager.RegisterStartupScript(this, GetType(), "unique_key",
    "element.onclick = function(){ return confirm('Are you sure you want to delete?'); };",
    true);
查看更多
Emotional °昔
5楼-- · 2019-02-18 00:13

No, you don't.

You seem to be misunderstanding that basic concept of webpage.

An ASPX page is a short program, which starts-up, generates seem HTML, and then terminates. The HTML is then sent across the Internet to the users browser. EVERYTHING you do in a codebehind must be complete before the user ever sees any of it.

You really want a javascript dialog box. (Actually, from what you describe, you could just create a messagebox-looking div in HTML with a standard HTML form on it.)

查看更多
劳资没心,怎么记你
6楼-- · 2019-02-18 00:15

To display an actual messagebox you will need javascript as it is done on the client-side. For whatever reason, if you cannot use javascript, you could do what AEMLoviji has suggested and "fake" it with some cleverness.

Note that you do not need jQuery to display a messagebox, simple javascript will suffice.

查看更多
萌系小妹纸
7楼-- · 2019-02-18 00:16

To show yes/no

<script>     
        function AlertFunction() {
            if (confirm('Are you sure you want to save this thing into the database?')) {
               $('#ConfirmMessageResponse').val('Yes');
            } else {
                $('#ConfirmMessageResponse').val('No');
            }
        }
    </script>

to handle it from .net side:

string confirmValue = ConfirmMessageResponse.Value;
                if (confirmValue == "Yes")
                {...}
查看更多
登录 后发表回答