This is a migrated thread and some comments may be shown as answers.

Telerik gridview checkbox needs to persistant.

3 Answers 37 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Siva
Top achievements
Rank 1
Siva asked on 25 Jul 2013, 07:05 AM
Hi admin and all,

I have a rad grid with a check box in each row. whenever I change page or scroll, the checkboxes loose their selected state. How can I handle that?. Small additional information i am using virtual scrolling and virtual paging in radgrid.

This is an urgent requirement for me. Give reply as soon as possible.
Thanks and Regards,

3 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 1
answered on 25 Jul 2013, 09:44 AM
Hi Siva,

Please try the below example code snippet to get the checkbox item to keep its state on paging or scrolling.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server"  AllowPaging="true"
    AutoGenerateColumns="false" OnPreRender="RadGrid1_PreRender">
    <MasterTableView DataKeyNames="OrderID">
        <Columns>
            <telerik:GridBoundColumn DataField="OrderID" HeaderText="OrderID" UniqueName="Name">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" CausesValidation="false"
                        OnCheckedChanged="CheckBox1_CheckedChanged" />
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
    <ClientSettings Scrolling-EnableVirtualScrollPaging="true" Scrolling-AllowScroll="true">
    </ClientSettings>
</telerik:RadGrid>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label><%--To display the rows checked--%>

C#:
public string _SelectedID
{
    set
    {
        ViewState["SelectedID"] = value;
    }
    get
    {
        if (ViewState["SelectedID"] != null)
        {
            return Convert.ToString(ViewState["SelectedID"]);
        }
        else
        {
            return ",0,";
        }
    } 
}
      
protected void RadGrid1_PreRender(object sender, EventArgs e)
{ 
    foreach (GridDataItem item in RadGrid1.Items)
    {
        if (_SelectedID.Contains("," + Convert.ToInt32(item.GetDataKeyValue("OrderID")).ToString() + ","))
        {
            ((CheckBox)item.FindControl("CheckBox1")).Checked = true;
        }
    }
  
}
        
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    CheckBox CheckBox1 = sender as CheckBox;
    GridDataItem item = (GridDataItem)CheckBox1.NamingContainer;
    int ID = Convert.ToInt32(item.GetDataKeyValue("OrderID"));
    if (CheckBox1.Checked)
    {
        _SelectedID += ID.ToString() + ",";
        Label1.Text = _SelectedID.ToString();//To display the rows checked Using DatakeyValue of a column
    }
    else
    {
        _SelectedID += _SelectedID.Replace(ID.ToString() + ",", "");
    }
}

Hope this helps,let me know if any concern.

Thanks,
Princy
0
Siva
Top achievements
Rank 1
answered on 25 Jul 2013, 11:18 AM
Thank You Sir,
0
Rod
Top achievements
Rank 1
answered on 16 Jan 2014, 03:57 PM
I believe the last line in Princy's solution should read "=" instead of "+="  - thanks Princy, this really helped!

_SelectedID = _SelectedID.Replace(ID.ToString() + ",", "");
Tags
General Discussions
Asked by
Siva
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Siva
Top achievements
Rank 1
Rod
Top achievements
Rank 1
Share this question
or