Reputation: 1407
I have a checkbox in aspx form page, external to a GridView.
If I checked this checkbox executed one query that populated this gridview and update your rows.
This Gridview is paginated and when I change page returns to initial GridView rows and lost the selection in checkbox.
Is there a way to keep checked after sorting and paging?
Thanks.
Edit #1
My code below.
When checkbox is checked in BindData(); is executed a different query to the initial.
int ck = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvrPager = gvProducts.BottomPagerRow;
DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages");
gvProducts.PageIndex = ddlPages.SelectedIndex;
BindData();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvProducts.PageIndex = e.NewPageIndex;
BindData();
}
protected void Paginate(object sender, CommandEventArgs e)
{
int intCurIndex = gvProducts.PageIndex;
switch (e.CommandArgument.ToString().ToLower())
{
case "First":
gvProducts.PageIndex = 0;
break;
case "Prev":
gvProducts.PageIndex = intCurIndex - 1;
break;
case "Next":
gvProducts.PageIndex = intCurIndex + 1;
break;
case "Last":
gvProducts.PageIndex = gvProducts.PageCount - 1;
break;
}
gvProducts.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "money")) > 100000)
{
e.Row.BackColor = System.Drawing.Color.AntiqueWhite;
}
}
if (e.Row.RowType == DataControlRowType.Pager)
{
DropDownList ddl = (DropDownList)(e.Row.FindControl("ddlpages"));
Label lblPageCount = (Label)e.Row.FindControl("lblPageCount");
if (lblPageCount != null)
lblPageCount.Text = gvProducts.PageCount.ToString();
for (int i = 1; i <= gvProducts.PageCount; i++)
{
ddl.Items.Add(i.ToString());
}
ddl.SelectedIndex = gvProducts.PageIndex;
if (gvProducts.PageIndex == 0)
{
((ImageButton)e.Row.FindControl("ImageButton1")).Visible = false;
((ImageButton)e.Row.FindControl("ImageButton2")).Visible = false;
}
if (gvProducts.PageIndex + 1 == gvProducts.PageCount)
{
((ImageButton)e.Row.FindControl("ImageButton3")).Visible = false;
((ImageButton)e.Row.FindControl("ImageButton4")).Visible = false;
}
}
}
protected void chkLinked_CheckedChanged(Object sender, EventArgs args)
{
if (sender != null)
{
try
{
if (((CheckBox)sender).Checked)
{
ck = 1;
BindData();
}
else
{
ck = 0;
BindData();
}
}
catch { }
}
}
Edit #2
private DataSet RetrieveProducts()
{
if (ViewState["Products"] != null)
return (DataSet)ViewState["Products"];
if (ck > 0)
{
sql = @" SELECT * FROM doTable Where money > 100000; ";
}
else
{
sql = @" SELECT * FROM doTable; ";
}
DataSet dsProducts = new DataSet();
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
cn.Open();
using (OdbcCommand cmd = new OdbcCommand(sql, cn))
OdbcDataAdapter adapter = new OdbcDataAdapter(cmd);
adapter.Fill(dsProducts);
}
}
return dsProducts;
}
private void BindData()
{
gvProducts.DataSource = RetrieveProducts();
gvProducts.DataBind();
}
Upvotes: 1
Views: 2997
Reputation: 686
The possible reason can be Viewstate is not enabled for the check box that is why it is not preserving the state.To enable it you can use EnableViewState property of checkbox.
<asp:CheckBox ID="chkLinked" runat="server" EnableViewState="true"/>.
Upvotes: 2