Сохранить состояние флажка Gridview после перемещения страницы - PullRequest
0 голосов
/ 14 апреля 2011

это мой код для запоминания значений для флажка

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

1 Ответ

0 голосов
/ 14 апреля 2011

Вы неправильно заполняете свои флажки в 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;
            }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...