[英]Maintaining state and count of checkboxes while paging in a GridView control
我在启用了分页的aspx页面上有一个gridview。 此gridview包含数据库中的一些数据字段以及每行的复选框。
我开始想知道,如果我在遍历所有行之前重新绑定数据源,是否会记住复选框选项,但是很快就确定即使从一页到下一页然后再次返回,复选框选项也会丢失。
为了保持复选框处于选中状态,我在本教程中尝试了一个自定义实现: http : //aspalliance.com/774_Maintaining_State_of_CheckBoxes_While_Paging_in_a_GridView_Control.all
我想计算在asp.net页面上选中的复选框的数量,如果count = 5,那么将按钮状态从Disabled更改为enabled,但是当我在Gridview中更改页面时,不计算网格的选定行在上一页中。
我的代码在下面。
我将非常感谢您在解决此问题方面可以给我的任何帮助。
private void RememberOldValues()
{
ArrayList categoryIDList = new ArrayList();
int index = -1;
foreach (GridViewRow row in GridView1.Rows)
{
index = (int)GridView1.DataKeys[row.RowIndex].Value;
bool result = ((CheckBox)row.FindControl("chkSelect")).Checked;
if (Session["CHECKED_ITEMS"] != null)
categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (result)
{
if (!categoryIDList.Contains(index))
categoryIDList.Add(index);
}
else
categoryIDList.Remove(index);
}
if (categoryIDList != null && categoryIDList.Count > 0)
Session["CHECKED_ITEMS"] = categoryIDList;
}
private void RePopulateValues()
{
ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
if (categoryIDList != null && categoryIDList.Count > 0)
{
foreach (GridViewRow row in GridView1.Rows)
{
int index = (int)GridView1.DataKeys[row.RowIndex].Value;
if (categoryIDList.Contains(index))
{
CheckBox myCheckBox = (CheckBox)row.FindControl("chkSelect");
myCheckBox.Checked = true;
}
}
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
RememberOldValues();
GridViewBind();
GridView1.DataSource = dset.Tables[0];
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
RePopulateValues();
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkTest = (CheckBox)sender;
GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
int count = 0;
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = (CheckBox)row.FindControl("chkSelect");
if (chk.Checked)
{
count++;
grdRow.BackColor = System.Drawing.Color.Yellow;
}
}
if (count == 5)
{
btnUpdate.Enabled = true;
btnUpdate.CssClass = "enabledImageButton";
}
else
{
btnUpdate.Enabled = false;
btnUpdate.CssClass = "disabledImageButton";
}
}
声明一个私有属性以从会话中读取arralist,以避免一次又一次地调用它。
ArrayList SelectedCategories
{
get
{
ArrayList categoryIDList;
if (Session["CHECKED_ITEMS"] != null)
categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];
else
{
categoryIDList = new ArrayList();
Session["CHECKED_ITEMS"] = categoryIDList;
}
return categoryIDList;
}
}
然后,在“复选框已更改”事件中,您可以更改代码以访问存储的选择数组列表。
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkTest = (CheckBox)sender;
GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
int index = (int)GridView1.DataKeys[grdRow.RowIndex].Value;
if (chkTest.Checked)
{
if (!SelectedCategories.Contains(index))
SelectedCategories.Add(index);
grdRow.BackColor = System.Drawing.Color.Yellow;
}
else
{
if (SelectedCategories.Contains(index))
SelectedCategories.Remove(index);
grdRow.BackColor = System.Drawing.Color.White;
}
if (SelectedCategories.Count >= 5)
{
btnUpdate.Enabled = true;
btnUpdate.CssClass = "enabledImageButton";
}
else
{
btnUpdate.Enabled = false;
btnUpdate.CssClass = "disabledImageButton";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.