Search code examples
javascriptjqueryasp.netmessagebox

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


Solution

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