Hi Telerik Team - I have a grid that has gridTemplateColumn checkbox column. so If the user checks some rows and in the second screen if he checks some rows then return backs to previous rows needs to show correct records. I searched in this forum, found code and created with northwind database. But sometimes when I check some items in 4 pages and then returns back not preserving the checkboxes as expected. Can anyone please guide me on this?
aspx:
<
telerik:GridTemplateColumn
UniqueName
=
"TemplateColumn"
SortExpression
=
"LastName"
>
<
ItemTemplate
>
<
asp:checkbox
ID
=
"checkColumn"
runat
=
"server"
/>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridBoundColumn
DataField
=
"OrderID"
DataType
=
"System.Int32"
HeaderText
=
"OrderID"
ReadOnly
=
"True"
SortExpression
=
"OrderID"
UniqueName
=
"OrderID"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"ProductID"
DataType
=
"System.Int32"
HeaderText
=
"ProductID"
ReadOnly
=
"True"
SortExpression
=
"ProductID"
UniqueName
=
"ProductID"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"UnitPrice"
DataType
=
"System.Decimal"
HeaderText
=
"UnitPrice"
SortExpression
=
"UnitPrice"
UniqueName
=
"UnitPrice"
>
</
telerik:GridBoundColumn
>
</
Columns
>
aspx.cs:
ArrayList selectedItems;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["Selected"] = null;
}
}
private void RememberSelected()
{
selectedItems = new ArrayList();
int index = -1;
// foreach (GridDataItem row in RadGrid1.MasterTableView.Items)//RadGrid1.Items)
foreach (GridDataItem row in RadGrid1.Items)
{
index = (int)row.GetDataKeyValue("ProductID");
bool result = ((CheckBox)row.FindControl("CheckColumn")).Checked;
// Check in the Session
if (ViewState["Selected"] != null)
selectedItems = (ArrayList)ViewState["Selected"];
if (result)
{
if (!selectedItems.Contains(index))
selectedItems.Add(index);
}
else
selectedItems.Remove(index);
}
if (selectedItems != null && selectedItems.Count > 0)
ViewState["Selected"] = selectedItems;
}
protected void RadGrid1_PageIndexChanged(object source, Telerik.Web.UI.GridPageChangedEventArgs e)
{
RememberSelected();
}
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (selectedItems != null)
{
if (e.Item is GridDataItem)
{
GridDataItem row = (GridDataItem)e.Item;
int index = (int)row.GetDataKeyValue("ProductID");
if (selectedItems.Contains(index))
{
e.Item.Selected = true;
}
}
}
}
protected void RadGrid1_SortCommand(object source, GridSortCommandEventArgs e)
{
RememberSelected();
}