在GridView控件中分页时维护复选框的状态和计数

本文关键字:状态 复选框 维护 控件 GridView 分页 | 更新日期: 2023-09-27 18:12:41

我在启用分页的aspx页面上有一个gridview。此gridview包含来自数据库的一些数据字段和每行的复选框。

我开始怀疑如果我在遍历所有行之前重新绑定数据源,是否会记住复选框选项,但很快确定即使从一页转到下一页,然后再次返回复选框选项也会丢失。

为了保持复选框的选中状态,我在本教程中尝试了一个自定义实现:http://aspalliance.com/774_Maintaining_State_of_CheckBoxes_While_Paging_in_a_GridView_Control.all

我想计算复选框的数量,在我的asp.net页面上检查,如果计数= 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";
    }
}

在GridView控件中分页时维护复选框的状态和计数

声明一个私有属性从会话中读取数组,以避免一次又一次地调用它。

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;
    }
}

然后在Checkbox changed事件中,您可以更改代码以访问存储的选择数组列表。

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";
    }
}