最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Display a "YesNo" alert box in C# code behind - Stack Overflow

programmeradmin5浏览0评论

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.

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.

Share Improve this question edited Sep 30, 2010 at 14:30 ChrisLively 88.1k27 gold badges173 silver badges248 bronze badges asked Sep 30, 2010 at 14:10 user279521user279521 4,80721 gold badges79 silver badges110 bronze badges 3
  • 1 Do you mean you can't use jQuery, or you can't use javascript? – Carvellis Commented Sep 30, 2010 at 14:15
  • can't use jQuery. Javascript is fine; – user279521 Commented Sep 30, 2010 at 14:16
  • You should add the tag webforms to this question – Thomas Kjørnes Commented Sep 30, 2010 at 14:28
Add a comment  | 

6 Answers 6

Reset to default 12

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?');";
}

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.)

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.

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

Use RegisterStartupScript

ScriptManager.RegisterStartupScript(this, GetType(), "unique_key",
    "element.onclick = function(){ return confirm('Are you sure you want to delete?'); };",
    true);

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")
                {...}
发布评论

评论列表(0)

  1. 暂无评论