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

43,617

Solution 1

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

Solution 2

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

Solution 3

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.

Share:
43,617
user279521
Author by

user279521

.Net developer in training; working on multiple projects at any given time (austink_2004);

Updated on October 18, 2020

Comments

  • user279521
    user279521 over 3 years

    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.

  • user279521
    user279521 over 13 years
    tried Ajax in the past, but the production servers (web farms) tend to mess up ajax, so I would rather avoid Ajax;
  • Dan Abramov
    Dan Abramov over 13 years
    +1 for divs, JS message boxes are pretty annoying and aren't used on any major website for about three years now.
  • Kevin
    Kevin over 13 years
    judging from the comment the OP posted, I'd say this is the best way to do it.
  • user279521
    user279521 over 13 years
    very cool @Chris. Thanks for the response. The first code sample works like a charm.
  • James Curran
    James Curran over 13 years
    @user: An alert box freezes the browser until it's cleared and follow the theme of the user desktop instead of color scheme of the webpage. (an html forum or a jQuery Dialog are part of the web page itself, and therefore have neither of those problems)
  • Dan Abramov
    Dan Abramov over 13 years
    They interrupt the user workflow. With message box displayed, I always have to focus my view on the center of the screen (although the field I'm filling might be on bottom of the page, for example). Secondly, message boxes are modal, and I have to read and close one to switch to the other tab, for example. I however must say that deleting a record is enough an excuse for displaying a message box because it actually does require to interrupt user workflow. But for things like wrong field data (or, worse, 'Update Succeeded!'), they should never be used. In my opinion.
  • user279521
    user279521 over 13 years
    @James, just for arguments sake, isn't the purpose of a alert box, to be modal and freeze the page? I agree with the color scheme point;
  • Dan Abramov
    Dan Abramov over 13 years
    Yeah, but you must understand that it's JavaScript here, not C#. You can't execute C# on client.
  • user279521
    user279521 over 13 years
    @gaearon, ok, I understand where you are coming from. Thanks for the explanation.