Prevent jQuery Modal popup to reopen on browser back button Click using jQuery in ASP.Net

lingers
 
on Sep 11, 2021 06:16 AM
519 Views

I have this GridView with checkbox which one can use to add and remove record from database. Now whenever you add or remove record from the database and you click on the backward arrow on the menu bar of a browser, it doesn’t go back to the previous page.

It first display or the actions you performed that is if for instance you added 2 items and removed one item via checking or unchecking the check box.

When you click on the backward button it ought to take you to the previous page which is issue.aspx immediately, instead when the click on the backward button on the menu bar of a browser, it will display all the message box in the order with which you performed the various action (whether adding or remove action), and you have to escape all the message box before the backward arrow on the menu bar of the browser will take you to the previous page.

What i want is for the backward arrow to take you to the previous page without displaying the message box of the action performed either by checking or unchecking the check box.

Please help

<script>
    function ShowPopup(message) {
        $("#lblMessage").html(message);
        $("#dialog").dialog({
            modal: true,
            title: "Message Alert",
            width: 350,
            height: 160
        });
    }
</script>

 

protected void OnChckedChanged(object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    GridViewRow row = (GridViewRow)chk.NamingContainer;
 
    string number = row.Cells[1].Text;
    string pid ="206";
 
    string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constring))
    {
        string acceptedCylinder = GetAcceptedCylinder(pid);
        if (chk.Checked)
        {
            if (acceptedCylinder == "[]")
            {
                using (SqlCommand cmd = new SqlCommand("UPDATE issuesheet SET cylinders = @cylinders where pid='206'", con))
                {
                    cmd.Parameters.AddWithValue("@cylinders", "['" + number.Trim() + "']");
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('Record Inserted successfully.');", true);
            }
            else
            {
                List<string> cylinderNo = acceptedCylinder.Replace("[", "").Replace("]", "").Replace("'", "").Split(',').ToList();
                cylinderNo.Add(number);
                using (SqlCommand cmd = new SqlCommand("UPDATE issuesheet SET cylinders = @cylinders where pid='206'", con))
                {
                    cmd.Parameters.AddWithValue("@cylinders", "['" + string.Join("','", cylinderNo.Distinct()) + "']");
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('Record Updated Successfully.');", true);
            }
        }
        else
        {
            if (!string.IsNullOrEmpty(acceptedCylinder))
            {
                List<string> cylinderNo = acceptedCylinder.Replace("[", "").Replace("]", "").Replace("'", "").Split(',').ToList();
                cylinderNo.Remove(number);
                using (SqlCommand cmd = new SqlCommand("UPDATE issuesheet SET cylinders = @cylinders where pid='206' ", con))
                {
                    cmd.Parameters.AddWithValue("@cylinders", cylinderNo.Distinct().Count() > 0 ? "['" + string.Join("','", cylinderNo.Distinct()) + "']" : "[]");
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('Record Deleted Successfully.');", true);
            }
        }
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 11, 2021 06:48 AM
on Sep 13, 2021 06:26 AM

Hi lingers,

Check this example. Now please take its reference and correct your code.

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

HTML

<div>
    <div id="dialog" style="display: none" align="center">
        <span id="lblMessage"></span>
    </div>
    <asp:GridView ID="avDetails" runat="server" AutoGenerateColumns="False" Font-Names="Candara"
        Font-Size="14px" DataKeyNames="EmployeeId" Width="100%" Style="margin-bottom: 0px"
        CssClass="grid" CellPadding="2" ForeColor="#333333" GridLines="None" Font-Bold="True">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="EmployeeId" HeaderText="ID" />
            <asp:BoundField DataField="FirstName" HeaderText="Name" />
            <asp:BoundField DataField="City" HeaderText="City" />
            <asp:BoundField DataField="Country" HeaderText="Country" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkSelect" runat="server" OnCheckedChanged="OnChckedChanged" AutoPostBack="True" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/themes/smoothness/jquery-ui.css" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/jquery-ui.min.js"></script>
    <script type="text/javascript">
        var isBack = false;
        function ShowPopup(message) {
            $("#lblMessage").html(message);
            $("#dialog").dialog({
                modal: true,
                title: "Message Alert",
                width: 350,
                height: 160
            });
        }

        window.onload = function () {
            if (isBack) {
                $("#dialog").dialog("close");
            }
        }
        if (performance.navigation.type == 2) {
            isBack = true;
        }
    </script>
</div>

Namespaces

C#

using System.Configuration;
using System.Data;
using System.Data.SqlClient;

VB.Net

Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindUserDetails1();
    }
}

protected void BindUserDetails1()
{
    string constr1 = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
    using (SqlConnection con1 = new SqlConnection(constr1))
    {
        using (SqlDataAdapter sda1 = new SqlDataAdapter("SELECT * from Employees", con1))
        {

            using (DataTable dt1 = new DataTable())
            {
                sda1.Fill(dt1);
                avDetails.DataSource = dt1;
                avDetails.DataSource = dt1;
                avDetails.DataBind();
            }
        }
    }
}

protected void OnChckedChanged(object sender, EventArgs e)
{
    ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('Record Inserted successfully.');", true);
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        BindUserDetails1()
    End If
End Sub

Protected Sub BindUserDetails1()
    Dim constr1 As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
    Using con1 As SqlConnection = New SqlConnection(constr1)
        Using sda1 As SqlDataAdapter = New SqlDataAdapter("SELECT * from Employees", con1)
            Using dt1 As DataTable = New DataTable()
                sda1.Fill(dt1)
                avDetails.DataSource = dt1
                avDetails.DataSource = dt1
                avDetails.DataBind()
            End Using
        End Using
    End Using
End Sub

Protected Sub OnChckedChanged(ByVal sender As Object, ByVal e As EventArgs)
    ClientScript.RegisterStartupScript(Me.GetType(), "Popup", "ShowPopup('Record Inserted successfully.');", True)
End Sub

Screenshot