在Telerik RadGrid控件中进行分页时保持CheckBox状态

时间:2015-12-24 14:20:52

标签: c# asp.net telerik-grid

我有一个telerik-radgrid,我使用SelectAll。对于SelectAll,我使用了复选框。分页后检查状态将消失。 即使在分页之后,我们如何能够保持Checkbox的检查状态。

1 个答案:

答案 0 :(得分:4)

嗨解决了这个......

在复选框OnCheckedChanged事件中调用checkChanged 在viewstate和item数据库中存储已检查的值,读取viewstate的值。

这里我们有.cs代码: -

public const string SELECTED_CUSTOMERS_INDEX =“UserIndex”;

protected void CheckChanged(Object sender, System.EventArgs e)
{
    CheckBox box = (CheckBox)sender;
    GridDataItem item = (GridDataItem)box.NamingContainer;
    var rowIndex = item.ItemIndex;
    var idex =  radStoreUsers.MasterTableView.DataKeyValues[rowIndex];
    string datakey = idex["Id"].ToString();
    if (box.Checked)
    {
        PersistRowIndex(datakey);
    }
    else
    {
        RemoveRowIndex(datakey);
    }
}

private void PersistRowIndex(string chkId)
{
    if (!SelectedCustomersIndex.Exists(i => i == chkId))
    {
        SelectedCustomersIndex.Add(chkId);
    }
}

private void RemoveRowIndex(string chkId)
{
    SelectedCustomersIndex.Remove(chkId);
}

private List<string> SelectedCustomersIndex
{
    get
    {
        if (ViewState[SELECTED_CUSTOMERS_INDEX] == null)
        {
            ViewState[SELECTED_CUSTOMERS_INDEX] = new List<string>();
        }

        return (List<string>)ViewState[SELECTED_CUSTOMERS_INDEX];
    }
}

protected void radStoreUsers_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = e.Item as GridDataItem;
        CheckBox box = (CheckBox)item.FindControl("chkBox");
        if (item.OwnerTableView.DataMember == "Users")
        {
            if (SelectedCustomersIndex != null)
            {
                foreach(string id in SelectedCustomersIndex)
                {
                    if(item.GetDataKeyValue("Id").ToString() == id)
                    {
                        box.Checked = true;
                    }
                }
            }
        }
    }
}