Saving CheckBox state while paging in Telerik RadGrid Control

I have a telerik-radgrid where I use SelectAll. For SelectAll, I used the checkbox. Verified status is disabled after paging. How we can save the checked status of the checkbox can be saved even after paging.

+4
source share
1 answer

Hi, I decided this ....

calling checkChanged in the OnCheckedChanged flag of the event to save the verification value in the viewstate and read the value of viewstate in the item's data table.

And here we have the .cs code: -

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;
                    }
                }
            }
        }
    }
}
+4
source

Source: https://habr.com/ru/post/1621628/


All Articles