Maintain viewstate with controls in CommandItemTemplate

3 posts, 0 answers
  1. Michael Gois
    Michael Gois avatar
    17 posts
    Member since:
    Oct 2008

    Posted 11 Dec 2008 Link to this post

    I have a RadGrid with a custom CommandItemTemplate which contains a checkbox within it. The data which is displayed in the grid is dependent on whether the checkbox is checked or not, which means that in the onCheckChanged handler for the checkbox, I must call the grid.Rebind() method. The problem however is that whenever I call the rebind method, the checkbox resets its state back to unchecked.

    Here is the ASPX code:

            <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
                Skin="Vista" Width="287px">
                <mastertableview autogeneratecolumns="False" commanditemdisplay="Top">
    <telerik:GridBoundColumn DataField="ID" EmptyDataText="&amp;nbsp;" HeaderText="ID" UniqueName="ID"></telerik:GridBoundColumn>
    <asp:CheckBox id="chkShowExistingMappings" runat="server" Text="Show nodes with existing mappings" AutoPostBack="True" Width="237px" __designer:wfdid="w26" OnCheckedChanged="chkShowExistingMappings_CheckedChanged"></asp:CheckBox>

    And here is the code behind:

    protected DataTable GetData()
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("Product", typeof(string));
            table.Columns.Add("MinPrice", typeof(double));
            table.Columns.Add("Price", typeof(double));
            table.Rows.Add(1, "Tea", 1.00, 1.32);
            table.Rows.Add(2, "Chocolate", 4.00, 4.70);
            table.Rows.Add(3, "Cookies", 2.50, 3.06);
            return table;

        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
            RadGrid1.DataSource = GetData();

        protected void chkShowExistingMappings_CheckedChanged(object sender, EventArgs e)

    If you run it and tick the checkbox, you will see that it doesnt maintain its checked state. Can anyone tell me how to get this working please?

  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 12 Dec 2008 Link to this post

    Hello Michael,

    To persist the checkbox state on postbacks, you can go through the following help link. You can modify the code according to your requirements.
    Persisting CheckBox control state in GridTemplateColumn on rebind

  3. Michael Gois
    Michael Gois avatar
    17 posts
    Member since:
    Oct 2008

    Posted 16 Dec 2008 Link to this post

    Thanks Princy,
    Managed to sort it out now with help from that link. Just had to modify the code slightly in Grid_DataBinding to check if e.Item is GridCommandItem rather than GridEditFormItem.

Back to Top