【发布时间】:2014-07-14 09:42:01
【问题描述】:
我在启用分页的 aspx 页面上有一个 gridview。 此网格视图包含来自数据库的一些数据字段和每行的复选框。
我开始想知道如果我在循环遍历所有行之前重新绑定数据源是否会记住复选框选项,但很快确定即使从一页转到下一页然后再返回复选框选项是丢了。
为了保持复选框选中状态,我在本教程中尝试了自定义实现:http://aspalliance.com/774_Maintaining_State_of_CheckBoxes_While_Paging_in_a_GridView_Control.all
我想计算在我的 asp.net 页面上选中的复选框的数量,如果 count = 5 然后将按钮状态从禁用更改为启用,但是当我在 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";
}
}
【问题讨论】: