views:

111

answers:

6

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.

A: 

O.k if u cannot use jQuery for this project i advice to add a panel to Form. And add 2 buttons to this panel. "Yes" and "No". And position it on center with css. On clicked Event of any button make thisPanel.Visible=true; otherwise false.

And the Dean version is good

AEMLoviji
Who gave me this -1 vote? what is happening? what problem in my Reply?
AEMLoviji
+1  A: 

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.

Kevin
A: 

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

Dean
tried Ajax in the past, but the production servers (web farms) tend to mess up ajax, so I would rather avoid Ajax;
user279521
+2  A: 

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

James Curran
+1 for divs, JS message boxes are pretty annoying and aren't used on any major website for about three years now.
gaearon
@gaearon, curious..... annoying in what sense?
user279521
@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)
James Curran
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.
gaearon
@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;
user279521
@gaearon, ok, I understand where you are coming from. Thanks for the explanation.
user279521
+2  A: 

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?');";
}
Chris Lively
judging from the comment the OP posted, I'd say this is the best way to do it.
Kevin
very cool @Chris. Thanks for the response. The first code sample works like a charm.
user279521
Yeah, but you must understand that it's JavaScript here, not C#. You can't execute C# on client.
gaearon
A: 

Use RegisterStartupScript

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