[Solved] ASPX Page gets blank when displaying alert message box in ASP.Net

lingers
 
on Sep 01, 2021 06:39 AM
1113 Views

My page goes blank when check box is checked or unchecked. Please i don't want it to go blank. It is after you click ok in the alert box that the page returns

protected void OnChckedChanged(object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    GridViewRow row = (GridViewRow)chk.NamingContainer;
 
    string number = row.Cells[2].Text;
    string pid = Label27.Text;
 
    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='" + pid + "'", con))
                {
                    cmd.Parameters.AddWithValue("@cylinders", "['" + number.Trim() + "']");
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('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='" + pid + "'", con))
                {
                    cmd.Parameters.AddWithValue("@cylinders", "['" + string.Join("','", cylinderNo.Distinct()) + "']");
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('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='" + pid + "' ", con))
                {
                    cmd.Parameters.AddWithValue("@cylinders", cylinderNo.Distinct().Count() > 0 ? "['" + string.Join("','", cylinderNo.Distinct()) + "']" : "[]");
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Deleted successfully')", true);
            }
        }
    }
}

Please help

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 08, 2021 07:46 AM

That is the default behaviour as the page postback it refreshed the page.

Alert box is opened, once you clicked Ok the page loads the data.

To over come the issue you can use jQuery UI popup.

HTML

<div id="dialog" style="display: none" align="center">
    <span id="lblMessage"></span>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Id">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <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>
    function ShowPopup(message) {
        $("#lblMessage").html(message);
        $("#dialog").dialog({
            modal: true,
            title: "jQuery Dialog Popup",
            width: 350,
            height: 160
        });
    }
</script>

Code

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

protected void BindUserDetails()
{
    System.Data.DataTable dt = new System.Data.DataTable();
    dt.Columns.Add("Id");
    dt.Columns.Add("Name");
    dt.Columns.Add("Country");
    dt.Rows.Add(1, "John Hammond", "United States");
    dt.Rows.Add(2, "Mudassar Khan", "India");
    dt.Rows.Add(3, "Suzanne Mathews", "France");
    dt.Rows.Add(4, "Robert Schidner", "Russia");
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

protected void OnChckedChanged(object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    GridViewRow row = (GridViewRow)chk.NamingContainer;
    if (chk.Checked)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('Record updated successfully.');", true);
    }
    else
    {
        ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('Record deleted successfully.');", true);
    }
}