Creating A jQuery Modal Preview Dialog When Submitting a ASP.Net Form

modal-confirmation-dialog-cap

1. .aspx

<%@ Page Title=”” Language=”C#” MasterPageFile=”~/Site.Master” AutoEventWireup=”true”
CodeBehind=”Home.aspx.cs” Inherits=”ModalBox.Home” %>

<asp:Content ID=”Content1″ ContentPlaceHolderID=”HeadContent” runat=”server”>
<link rel=”stylesheet” type=”text/css” href=”jquery-ui.min.css” />
<script type=”text/javascript” src=”jquery-2.1.3.min.js”></script>
<script type=”text/javascript” src=”jquery-ui.min.js”></script>
<script type=”text/javascript”>
jQuery(document).ready(function() {
$(‘#dialog’).dialog({
autoOpen: false,
modal:true,
buttons: {
“Submit”: function()
{
$(this).dialog(“close”);
<%= Page.ClientScript.GetPostBackEventReference(this.btnSubmit, string.Empty) %>;
},
“Cancel”: function()
{
$(this).dialog(“close”);
}
}
});

$(‘#<%=btnSubmit.ClientID%>’).click(function (e) {
$(“p#dialog-email”).html($(“#<%=emailJQ.ClientID%>”).val());
$(‘#dialog’).dialog(‘open’);
return false;
});
});
</script>
</asp:Content>
<asp:Content ID=”Content2″ ContentPlaceHolderID=”MainContent” runat=”server”>
<form id=”testconfirmJQ” name=”testconfirmJQ” runat=”server” method=”post”>
<fieldset>
<legend>jQuery UI Modal Submit</legend>
<p>
<label for=”email”>
E-mail:</label><br />
<asp:TextBox ID=”emailJQ” name=”emailJQ” runat=”server”></asp:TextBox>
</p>
<p>
<asp:Button ID=”btnSubmit” runat=”server” Text=”Submit” OnClick=”btnSubmit_Click”
CausesValidation=”true” />
</p>
</fieldset>
</form>
<div id=”dialog” title=”Verify Form jQuery UI Style”>
<p>
<span class=”ui-icon ui-icon-alert” style=”float: left; margin: 0 7px 0 0;”></span>
You entered your e-mail address as:</p>
<p id=”dialog-email”>
</p>
<p>
If this is correct, click Submit Form.</p>
<p>
To edit, click Cancel.<p>
</div>
</asp:Content>

2. .cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ModalBox
{
public partial class Home : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnSubmit_Click(object sender, EventArgs e)
{
String email = emailJQ.Text.Trim();
}
}
}

you can see:

Ref: http://www.nivisec.com/2011/07/using-jquery-ui-dialog-as-confirmation.html

Using jQuery UI Dialog as a confirmation box for an ASP.Net button

This will add a jQuery UI Dialog confirmation when clicking a button in asp.Net. This is a non-generic, one button <-> one dialog example.

The button:
    <asp:Button ID=”btnSubmit” runat=”server” Text=”Submit” OnClick=”btnSubmit_Click” CausesValidation=”true” OnClientClick=”jQuery(‘#confirmation’).dialog(‘open’); return false;” />

The Dialog display div:
    <div id=“confirmation” title=“Submit Form?”>

        <span class="ui-icon ui-icon-alert" style="margin: 0 7px 50px 0; float: left;"></span>
        <span>Are you sure you would like to submit this form?</span>
    </div>

The Javascript:
    <script type=“text/javascript”>

        jQuery(document).ready(function() {
            $('#confirmation').dialog({
                autoOpen: false,
                modal:true,
                buttons: {
                    "Submit": function() { $(this).dialog("close"); <%= Page.ClientScript.GetPostBackEventReference(this.btnSubmit, string.Empty) %>; },
                    "Cancel": function() { $(this).dialog("close"); }
                }
            });
         });
    </script>
NOTE:  As sisyphus has pointed out below, this does not call client side validation.  I rarely use this method for submitting anything requiring it, so it never occured to me to include it.   I'm including code that should allow client side validation to occur.  It's not tested, but I'm pretty sure it is accurate :)
The Javascript with Client Side Validation invoked:    
<script type="text/javascript">
        jQuery(document).ready(function() {
            $('#confirmation').dialog({
                autoOpen: false,
                modal:true,
                buttons: {
                    "Submit": function() { $(this).dialog("close"); if (Page_ClientValidate()) <%= Page.ClientScript.GetPostBackEventReference(this.btnSubmit, string.Empty) %>; },
                    "Cancel": function() { $(this).dialog("close"); }
                }
            });
         });
    </script>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s