Hi @Gani_tpt,
Your question has nothing to do with the original question. If you have new questions in the future, I suggest you post a new post.
One point I've emphasized many times is that if you don't provide the smallest example that can reproduce the problem, how can we locate your problem.
Here i want to show the "Loader Animation" which is moving forward between one page to another page in gridview. Loader is coming, but, checked check box is loosing if i go to another page.
Do you mean leaving the current page or switching pages in gridview?
I can only implement "loader animation" for you based on the example provided in my answer.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.4/jquery.min.js"> </script>
<script src="https://malsup.github.io/jquery.blockUI.js"> </script>
<script type="text/javascript">
$(function () {
BlockUI("dvGrid");
$.blockUI.defaults.css = {};
});
function BlockUI(elementID) {
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_beginRequest(function () {
$("#" + elementID).block({
message: '<div align = "center">' + '<img src="https://i.pinimg.com/originals/d5/a2/b0/d5a2b01b8294bfb8678d67342b106795.gif" height="200px" width="200px"/></div>',
css: {},
overlayCSS: { backgroundColor: '#000000', opacity: 0.6, border: '3px solid #63B2EB' },
});
});
prm.add_endRequest(function () {
$("#" + elementID).unblock();
});
};
</script>
<script type="text/javascript">
function SelectOne(chk) {
//Get the reference of GridView.
var grid = chk.parentNode.parentNode.parentNode;
//Get the reference of GridView Row.
var row = chk.parentNode.parentNode;
//Set the color of Row.
row.className = "selected";
//Reference the Header CheckBox.
var headerCheckBox = grid.rows[0].cells[0].getElementsByTagName("input")[0];;
var checked = true;
//Loop through all GridView Rows.
for (var i = 1; i < grid.rows.length - 1; i++) {
//Reference the CheckBox.
var checkBox = grid.rows[i].cells[0].getElementsByTagName("input")[0];
if (!checkBox.checked) {
checked = false;
break;
}
}
headerCheckBox.checked = checked;
};
function SelectAll(headerCheckBox) {
//Get the reference of GridView.
var GridView = headerCheckBox.parentNode.parentNode.parentNode;
//Loop through all GridView Rows except first row.
for (var i = 1; i < GridView.rows.length - 1; i++) {
//Reference the CheckBox.
var checkBox = GridView.rows[i].cells[0].getElementsByTagName("input")[0];
//If CheckBox is checked, change background color the GridView Row.
if (headerCheckBox.checked) {
checkBox.checked = true;
GridView.rows[i].className = "selected";
} else {
checkBox.checked = false;
GridView.rows[i].className = "";
}
}
};
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div id="dvGrid" style="padding: 10px; width: 450px">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="gvCustomer" runat="server" AutoGenerateColumns="false"
AllowPaging="true" OnPageIndexChanging="OnPaging" PageSize="10">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" onclick="SelectAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" onclick="SelectOne(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CustomerID" HeaderText="Customer Id" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" />
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="btnSave_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "SELECT CustomerID, City, Country, PostalCode FROM Customers";
using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvCustomer.DataSource = dt;
gvCustomer .DataBind();
}
}
}
}
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
this.GetCheckedCheckBoxes();
gvCustomer.PageIndex = e.NewPageIndex;
this.BindGrid();
this.SetCheckedCheckBoxes();
}
private void GetCheckedCheckBoxes()
{
int checkBoxIndex;
List<int> checkBoxArray = this.CheckBoxArray;
//Loop through the GridView rows.
int i = 0;
foreach (GridViewRow row in gvCustomer.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
//Reference the CheckBox and calculate its Index.
CheckBox chk = (CheckBox)row.FindControl("CheckBox1");
checkBoxIndex = gvCustomer.PageSize * gvCustomer.PageIndex + (i + 1);
if (chk.Checked)
{
//If CheckBox is checked, add it to the ArrayList.
if (checkBoxArray.IndexOf(checkBoxIndex) == -1)
{
checkBoxArray.Add(checkBoxIndex);
}
}
else
{
//If CheckBox is unchecked, remove it from the ArrayList.
if (checkBoxArray.IndexOf(checkBoxIndex) != -1)
{
checkBoxArray.Remove(checkBoxIndex);
}
}
}
i++;
}
this.CheckBoxArray = checkBoxArray;
}
private List<int> CheckBoxArray
{
get
{
if (ViewState["CheckBoxArray"] != null)
{
return (List<int>)ViewState["CheckBoxArray"];
}
return new List<int>();
}
set
{
ViewState["CheckBoxArray"] = value;
}
}
private void SetCheckedCheckBoxes()
{
int i = 0;
bool isAllChecked = true;
foreach (GridViewRow row in gvCustomer.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
//Check if Index is present in ArrayList.
int checkBoxIndex = gvCustomer.PageSize * (gvCustomer.PageIndex) + (i + 1);
if (this.CheckBoxArray.IndexOf(checkBoxIndex) != -1)
{
//If present, check it.
CheckBox chk = (CheckBox)row.FindControl("CheckBox1");
chk.Checked = true;
row.Attributes.Add("class", "selected");
}
else
{
isAllChecked = false;
}
}
i++;
}
//If present, check it.
CheckBox chkAll = (CheckBox)gvCustomer.HeaderRow.FindControl("chkAll");
chkAll.Checked = isAllChecked;
}
protected void btnSave_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(3000);
int a = gvCustomer.PageIndex;
//Loop through All Pages
for (int i = 0; i < gvCustomer.PageCount; i++)
{
//Set Page Index
gvCustomer.SetPageIndex(i);
//After Setting Page Index Loop through its Rows
foreach (GridViewRow gvrow in gvCustomer.Rows)
{
if (gvrow.RowType == DataControlRowType.DataRow)
{
var checkbox = gvrow.Cells[0].FindControl("CheckBox1") as CheckBox;
/*******
Getting only last page record*********/
if (checkbox.Checked)
{
//var Remarks = (gvrow.FindControl("txtReasons") as TextBox).Text;
string CustomerID = gvrow.Cells[1].Text;
string City = gvrow.Cells[1].Text;
string Country = gvrow.Cells[3].Text;
string PostalCode = gvrow.Cells[4].Text;
//var Remarks = (gvrow.Cells[1].FindControl("txtReasons") as TextBox).Text;
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Customers(CustomerID,City,Country,PostalCode) VALUES(@CustomerID,@City,@Country,@PostalCode)", con))
{
cmd.Parameters.AddWithValue("@CustomerID", CustomerID);
cmd.Parameters.AddWithValue("@City", City);
cmd.Parameters.AddWithValue("@Country", Country);
cmd.Parameters.AddWithValue("@PostalCode", PostalCode);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
CheckBox chk = (CheckBox)gvrow.FindControl("CheckBox1");
chk.Checked = false;
CheckBox chkAll = (CheckBox)gvCustomer.HeaderRow.FindControl("chkAll");
chkAll.Checked = false;
}
}
}
gvCustomer.SetPageIndex(a);
}
}