lundi 11 janvier 2016

CheckBoxes in GridVIew does not remain selected when paginating through the GridView

I'm trying to work through some examples to understand how to make CheckBoxes remain selected when paginating through GridView.

I'm trying to replicate an example of paging through the GridView. I'm using the following example:

http://ift.tt/1jqIcLm

The only difference is that I use my own GridView as following:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" Inherits="GridViewExample._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:GridView ID="gvCustomers" DataKeyNames="Key" PageSize="4" AllowPaging="true" OnPageIndexChanging="gvCustomersPageChanging" runat="server" AutoGenerateColumns="false">

    <Columns>

    <asp:TemplateField>
        <ItemTemplate>
        <asp:CheckBox ID="chkSelect" runat="server" Checked="false" Value='<%#Eval("Key") %>' />
        </ItemTemplate>
    </asp:TemplateField>


    <asp:TemplateField HeaderText="Key">
        <ItemTemplate>
        <%# Eval("Key") %>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="First Name">
        <ItemTemplate>
        <%# Eval("FirstName") %>
        </ItemTemplate>
    </asp:TemplateField>

     <asp:TemplateField HeaderText="Last Name">
        <ItemTemplate>
        <%# Eval("LastName") %>
        </ItemTemplate>
    </asp:TemplateField>

    </Columns>

    </asp:GridView>
</asp:Content>

This is the data loading logic:

protected void Page_Load(object sender, EventArgs e)
{
    BindData();
}

private void BindData()
{
    var customers = new List<Customer>{
        new Customer { FirstName = "Mohammad",  LastName = "Azam", Key = 1},
        new Customer { FirstName = "John",  LastName = "Doe",  Key = 2},
        new Customer { FirstName = "Mary",  LastName = "Kate",  Key = 3},
        new Customer { FirstName = "Jerry",  LastName = "Seinfeld",  Key = 4},
        new Customer { FirstName = "Alex",  LastName = "Rowe",  Key = 5},
        new Customer { FirstName = "James",  LastName = "Smith",  Key = 9},
        new Customer { FirstName = "Jerry",  LastName = "Grand",  Key = 10},
        new Customer { FirstName = "Thomas",  LastName = "Anders",  Key = 11},
        new Customer { FirstName = "Diter",  LastName = "Bolen",  Key = 12},
        new Customer { FirstName = "Ryan",  LastName = "McDonag",  Key = 13},
        new Customer { FirstName = "Stewart",  LastName = "Black",  Key = 14},
        new Customer { FirstName = "James",  LastName = "Soth",  Key = 15},
        new Customer { FirstName = "Alex",  LastName = "Bowldwin",  Key = 18},
        new Customer { FirstName = "Arnold",  LastName = "Strong",  Key = 19},
    };


    gvCustomers.DataSource = (from c in customers select c).ToList();

    gvCustomers.DataBind();
}

This is the code from the example:

   private void RememberOldValues()
    {
        ArrayList categoryIDList = new ArrayList();
        int index = -1;
        foreach (GridViewRow row in gvCustomers.Rows)
        {
            index = (int)gvCustomers.DataKeys[row.RowIndex].Value;
            bool result = ((CheckBox)row.FindControl("chkSelect")).Checked;
            string id = ((CheckBox)row.FindControl("chkSelect")).ClientID;

            // Check in the Session
            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 gvCustomers.Rows)
            {
                int index = (int)gvCustomers.DataKeys[row.RowIndex].Value;
                if (categoryIDList.Contains(index))
                {
                    CheckBox myCheckBox = (CheckBox)row.FindControl("chkSelect");
                    myCheckBox.Checked = true;
                }
            }
        }
    }

The problem is even though I select the checkbox, when debugging the following line:

bool result = ((CheckBox)row.FindControl("chkSelect")).Checked;

in RememberOldValues() method, result is always false

Why is that? Any ideas?




Aucun commentaire:

Enregistrer un commentaire