【问题标题】:Maintaining state and count of checkboxes while paging in a GridView control在 GridView 控件中分页时保持复选框的状态和计数
【发布时间】: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";
    }
}

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    声明一个私有属性以从会话中读取 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;
        }
    }
    

    然后在您的 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";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-23
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多