JavaScript Confirm on ASP.NET Form Submit

This is a quick demo showing how to prompt the user to confirm when submitting a form on an ASP.Net page.  We start with a simple form.

form

There is some server side code which will populate a label with the current date and time when the form is submitted.

protected void SubmitButton_Click(object sender, EventArgs e)
{
    Timestamp.Text = DateTime.Now.ToString();
}

If the user is simply required to confirm the submitting of the form then an OnClientClick action can be specified.

<asp:Button runat="server" ID="SubmitButton" Text="Submit" CssClass="btn btn-default" OnClientClick="return confirm('Do you want to submit this page?')" CausesValidation="false" OnClick="SubmitButton_Click" />

When the form is submitted it will display the confirmation prompt.
confirm

If the Cancel button is pressed the form will not be submitted. If the OK button is pressed the form will be submitted and the current date and time will be displayed in a label.

form-submitted

The form in this example very simple. In a real application forms are usually more complicated with fields that require validation. Suppose we only want to prompt the user to confirm the form submission if the form is validated. This is still simple to do with very minor changes, the trick is to check the status of the validation on the client side first by calling the Page_ClientValidate method.

<div class="form-group">
    <asp:Label runat="server" AssociatedControlID="Name" CssClass="col-md-2 control-label">Name</asp:Label>
<div class="col-md-10">
        <asp:TextBox runat="server" ID="Name" CssClass="form-control" />
        <asp:RequiredFieldValidator runat="server" ControlToValidate="Name" CssClass="text-danger" ErrorMessage="Name is required." />
    </div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
        <asp:Button runat="server" ID="SubmitButton" Text="Submit" CssClass="btn btn-default" OnClientClick="if (Page_ClientValidate()){return confirm('Do you want to submit this page?')}" CausesValidation="false" OnClick="SubmitButton_Click" />
    </div>
</div>

With this code the form is not valid until the Name textbox contains a value. Only when all the validation controls are valid will the Confirm prompt be displayed. As before, if the Cancel button is pressed the form will not be submitted. If the OK button is pressed the form will be submitted and the current date and time will be displayed in a label.

Configuring SpecFlow and NUnit in Visual Studio 2015

How to set up Specflow and NUnit v3 in Visual Studio 2015. I have already added a new Class Library project named SpecFlowTest to an empty solution.


Step 1. Install SpecFlow extension for Visual Studio 2015

Once the SpecFlow extension is installed you will be able to add SpecFlow items to a project from the Installed Templates list of the Add Item dialog box.

  1. In the Tools menu select Extension and Updates…. This will open the Extensions Manager dialog.
  2. In the dialog choose the Online gallery in the left pane.
  3. In the right pane type specflow in the search box.
  4. In the middle pane find Specflow for Visual Studio 2015 in the results list and click the Download button. If you already have it installed a installed-tick tick will be displayed instead of a download button.

specflow-extension-vs2015

The package will be downloaded

specflow-extension-vs2015-download

A dialog will appear with a summary on the installation and prompting confirmation of the install

specflow-extension-vs2015-install

The installation will complete.

specflow-extension-vs2015-installing

Once installed a web page normally opens in Visual Studio with this address, http://www.specflow.org/guidance/first-steps. In some cases a restart of Visual Studio may be required.


Step 2. Installing NuGet packages

As usual NuGet packages can be installed via the window interface
Tools > NuGet Package Manager > Manage NuGet Packages for Solution…
or if you prefer, via the command line
Tools > NuGet Package Manager > Package Manager Console
There are a few ways to add the SpecFlow and NUnit libraries to a project,  either by installing individual packages for each product or by using combined packages.

Option 1. Combined Package

Open NuGet Package Manager and locate the SpecFlow.NUnit package.  This option will install both the SpecFlow and NUnit libraries as well as the NUnit runners for running the tests from TestExplorer in Visual Studio.  In the package description it does say NUnit version 2.6+ will be installed, at the time of this post NUnit version 3.0.0 was installed. In the future this may be a newer version of NUnit.
nuget-vs2015-specflow-nunit
And to install via command…

Install-Package SpecFlow.NUnit

Although NUnit version 3.0.0 is installed it can be upgraded to the latest version.  At this time the current version is 3.4.1.

nuget-vs2015-nunit3-update

And the NUnit update via command
Update-Package NUnit

And with that everything should be good to go.


Option 2. Installing Individual Packages

The packages can be installed separately.  Doing so would allow you to install a different test runner or a different unit testing framework.  Whatever the reason, if that is the case then you can follow these steps.

Install SpecFlow
With NuGet Package Manager open search online for the SpecFlow package.

nuget-vs2015-specflow

And via command

Install-Package SpecFlow

Install NUnit

This will install version is 3.4.1, no need for update.
nuget-vs2015-nunit3-install

And via command

Install-Package NUnit

Install NUnit3TestAdapter

Here I am installing a different test runner to the one installed in the combined package.  The result is the same, tests can be run from within Test Explorer.
nuget-vs2015-nunit3-testadapter

Install-Package NUnit3TestAdapter

If feature files have already been added to the project the below message will prompt for them to be re-generated.

specflow-change-detected

Conclusion

It seems logical to use the SpecFlow.NUnit combined package if NUnit is the unit test framework of choice.  If you will be using MSTest or a different test runner installing separate packages is not that much more work.