John
John

Reputation: 3945

Using MessageBox, change to 'Yes' or 'No'

string LocalWorkSiteName = WorkSite.Site_Name;

System.Windows.Forms.MessageBox.Show("Are you sure you want to delete invoice " for " + LocalWorkSiteName);

1) Why does the pop up always appear behind the browser? I want it to load in front of the browser.

2) How can I add a 'Yes' and 'No' button and remove the current 'OK'? So in code behind, if 'yes' do one thing if 'no' do the other:

if(yesIsPressed)
{
...
}
if(noIsPressed)
{
...
}

Am I going about this the correct way or are there more suitable methods?


EDIT Thanks for the reply guys...Going to go with the JS side, but I may need some assistance with it. So my button:

<asp:Button runat="server" ID="RemoveInvoice" Text="Remove Invoice" OnClick="RemoveInvoice_Click" CssClass="FadeOutOnEdit" />

runs the code behind function 'RemoveInvoice_CLick'...can I also get it to run the JS or do I need to change it to an input button and then do the code behind updates from there

Upvotes: 1

Views: 2433

Answers (2)

Jim Dagg
Jim Dagg

Reputation: 2032

You're loading a Windows Forms message box in a web page -- that's a control for a desktop app, not a web app. You'd probably be better served using a JavaScript popup, which is a web control. Using jQuery, you could attach a callback to a button's click event:

$("#buttonid").click(function() {
  var answer = confirm("Are you sure you want to delete invoice for " + LocalWorkSiteName + "?");
  if (answer)
  {
       // post to server-side
  }
});

Or if you want to stick with an ASP.net button control, you could wire up the event handler in its tag:

<asp:Button id="RemoveInvoice" runat="server" Text="Remove Invoice" 
        OnClick ="RemoveInvoice_Click" 
        OnClientClick="return confirm('Are you sure you want to delete invoice?');" 
        CssClass="FadeOutOnEdit" />

The OnClientClick attribute will prevent the server-side event from firing if the code returns false, which is what happens if the user clicks Cancel on the JavaScript popup from the call to confirm.

Upvotes: 1

freshbm
freshbm

Reputation: 5632

Maybe something like this:

DialogResult result1 = System.Windows.Forms.MessageBox.Show("Are you sure you want to delete invoice for " + LocalWorkSiteName,
        "Important Question",
        MessageBoxButtons.YesNo);

if(result1 == DialogResult.Yes) { ... }
if(resutl1 == DialogResult.No) { ... }

Note*

But you shouldn't do that with MessageBox :), because that is for desktop app (System.Windows.Forms), you should use some jQuery or javascript code for it. It even won't show on your client side, it will only popUp on your server.

You can try with jQueryUI Dialog

Upvotes: 2

Related Questions