Thursday 14 July 2016

display confirmbox code behind (server side) in asp.net

Server Side (Code Behind) Yes No Confirmation Message Box in ASP.Net

Description:

In this example we explain that how to display Confirmation box in server side in asp.net. Or display confirm box in code behind in c# in asp.net.or how to build a confirm message or dialog box at server side or code behind in asp.net.

In client side using JavaScript it is possible to create confirm box simply but how can I call on server side because JavaScript only post back when we click on “OK” but when we click on “Cancel” it will not post back. So how can I perform operation when click on OK or Cancel as per click.

Below is the example that demonstrate display confirm box when click on button and display message to the user you press ok button or cancel button at server side or code behind in asp.net.
confirmBox.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="confirmBox.aspx.cs" Inherits="confirmBox" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Server Side (Code Behind) Confirmation Message Box in ASP.Net</title>
    <script type="text/javascript">
        function checkWhichOptionisClicked() {
            var dynamicControl = document.createElement("INPUT");
            dynamicControl.type = "hidden";
            dynamicControl.name = "dynamicControl";
            if (confirm("Are you sure to save data...?")) {
                dynamicControl.value = "Yes";
            } else {
                dynamicControl.value = "Cancel";
            }
            document.forms[0].appendChild(dynamicControl);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="checkWhichOptionisClicked()"
        OnClick="btnSave_Click" />
    </form>
</body>
</html>


 confirmBox.aspx.cs:

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

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

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string confirmValue = Request.Form["dynamicControl"];
            if (confirmValue == "Yes")
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
                //perform Yes operation Here
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked Cancel!')", true);
                //perform No operation Here
            }
        }
        catch (Exception)
        {
           
            throw;
        }
       
    }
}

0 comments:

Post a Comment