【问题标题】:Maintain Gridview Checkbox state after moving Page移动页面后保持 Gridview Checkbox 状态
【发布时间】:2017-07-06 21:18:23
【问题描述】:

这是我的复选框的记住值代码

private void RememberOldGridValues()
    {
        ArrayList oUserCheckedList = new ArrayList();           
        int index = -1;
        int Value=0;
        foreach (GridViewRow row in gridViewResults.Rows)
        {
            if (((CheckBox)row.FindControl("chkSelect")).Checked)
            {
                index = row.RowIndex;
                Value=(int)gridViewResults.DataKeys[row.RowIndex].Value;
                if (Session["CHECKED_ITEMS"] != null)
                    oUserCheckedList = (ArrayList)Session["CHECKED_ITEMS"];
                if (oUserCheckedList.Count == 1)
                {
                    oUserCheckedList.RemoveAt(0);
                }
                if (!oUserCheckedList.Contains(index))
                    oUserCheckedList.Add(index);
                    oUserCheckedList.Add(Value);
            }
            if (index >= 0)
                break;
        }
        if (oUserCheckedList != null && oUserCheckedList.Count > 0)
            Session["CHECKED_ITEMS"] = oUserCheckedList;
    }

这是重新填充我无法在网格视图中获取数据时间。

private void RePopulateGridValues()
    {
        int value;
        ArrayList oUserCheckedList = (ArrayList)Session["CHECKED_ITEMS"];
        if (oUserCheckedList != null && oUserCheckedList.Count > 0)
        {
            foreach (GridViewRow row in gridViewResults.Rows)
            {
                string str = oUserCheckedList[0].ToString();
                value=Convert.ToInt32(oUserCheckedList[1].ToString());
                int i = Convert.ToInt32(str);
                if (row.RowIndex == i)
                {
                    if (value == Convert.ToInt32(row.DataItemIndex.ToString()))
                    {
                        CheckBox myCheckBox = (CheckBox)row.FindControl("chkSelect");
                        myCheckBox.Checked = true;
                    }
                }
            }
        }

【问题讨论】:

  • 你为什么要两次问同一个问题 - Grid View checkbox view state
  • 我有一个想法,因为您一次又一次地质疑。没有人会为您提供完整的代码。试试我的答案.. 祝你好运:)
  • 你可以删除我的问题,这是错误的

标签: c# asp.net gridview


【解决方案1】:

您正在以错误的方式重新填充 Gridview 复选框。您必须使用 RowDatabound 事件。

protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
         DataRow dr = ((DataRowView)e.Row.DataItem).Row;
         if (ArrayCollection.Find(dr["KeyColumnName"].ToString()))
//Dont try to store the index of your Grid and checkbox value, as if the page Index    change your Grid Index will will change. try to store KeyColumnName Value and then compare here and upon bases of set Checkbox = Chcekced
            {
                 CheckBox myCheckBox = (CheckBox)row.FindControl("chkSelect");
                 myCheckBox.Checked = true;
            }
    }
}

【讨论】:

  • RememberOldGridValues() 时我需要存储在数组集合中的内容
  • 是的,您需要存储,但是当您返回页面时,您必须使用上述技术来勾选复选框。
  • 好吧,如果我想一次只检查一个复选框,那么关于 gridview 中的分页,考虑到我有 16 条记录和 2 页,这是我正在写的东西
  • 这就是为什么我要你把主键列放在你的数组列表中而不是行索引中。无论您在哪个页面上,因为关键 Column 在每一行中都是唯一的。保持 gridview 复选框状态的最佳方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-19
  • 1970-01-01
  • 1970-01-01
  • 2017-03-15
  • 2021-08-14
  • 2013-12-29
  • 1970-01-01
相关资源
最近更新 更多