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

Maintain viewstate with controls in CommandItemTemplate

2 Answers 91 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael Gois
Top achievements
Rank 1
Michael Gois asked on 11 Dec 2008, 03:03 PM
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">
<Columns>
<telerik:GridBoundColumn DataField="ID" EmptyDataText="&amp;nbsp;" HeaderText="ID" UniqueName="ID"></telerik:GridBoundColumn>
</Columns>
<CommandItemTemplate>
<asp:CheckBox id="chkShowExistingMappings" runat="server" Text="Show nodes with existing mappings" AutoPostBack="True" Width="237px" __designer:wfdid="w26" OnCheckedChanged="chkShowExistingMappings_CheckedChanged"></asp:CheckBox>
</CommandItemTemplate>
</mastertableview>
        </telerik:RadGrid>

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)
    {
        RadGrid1.Rebind();
    }

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?
Thanks



2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 12 Dec 2008, 09:48 AM
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

Thanks
Princy.
0
Michael Gois
Top achievements
Rank 1
answered on 16 Dec 2008, 06:20 PM
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.

Tags
Grid
Asked by
Michael Gois
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Michael Gois
Top achievements
Rank 1
Share this question
or