在 Telerik RadGrid 控件中进行分页时保持复选框状态
Persisting CheckBox State While Paging in Telerik RadGrid Control
我有一个使用 SelectAll 的 telerik-radgrid。对于 SelectAll 我使用了复选框。寻呼后检查状态消失。
如何持久化 Checkbox 的选中状态,即使分页后也能保留。
你好解决了这个问题....
在复选框 OnCheckedChanged 事件中调用 checkChanged
将选中的值存储在视图状态中,并在项目数据绑定上读取视图状态的值。
这里有 .cs 代码:-
public 常量字符串 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;
}
}
}
}
}
}
我有一个使用 SelectAll 的 telerik-radgrid。对于 SelectAll 我使用了复选框。寻呼后检查状态消失。 如何持久化 Checkbox 的选中状态,即使分页后也能保留。
你好解决了这个问题....
在复选框 OnCheckedChanged 事件中调用 checkChanged 将选中的值存储在视图状态中,并在项目数据绑定上读取视图状态的值。
这里有 .cs 代码:-
public 常量字符串 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;
}
}
}
}
}
}