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

CheckBox in Command Item Template

5 Answers 496 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David Penny
Top achievements
Rank 2
David Penny asked on 08 Jan 2009, 04:23 PM
Hi,

I almost have the following working but am missing a little bit of information.

I have a RadGrid with the following definition in the Command Item Template:

<CommandItemTemplate> 
                    <table style="width: 100%;">  
                        <tr> 
                            <td style="width: 50%;">  
                                <href="#" onclick="return ShowInsertForm();">  
                                    <img alt="Insert" border="0" height="20" src="AddRecord.gif" width="20">  
                                    Add New Record</a> 
                            </td> 
                            <td style="width: 50%" align="right">  
                                <asp:CheckBox ID="chkShowCompleted"  OnCheckedChanged="chkShowCompleted_CheckChanged"   
                                runat="server" Text="Show Completed" Width="163px" 
                                    AutoPostBack="True" /> 
                            </td> 
                        </tr> 
                    </table> 
                </CommandItemTemplate> 

Works great and shows exactly as I want.

I then have:
    Protected Sub chkShowCompleted_CheckChanged(ByVal sender As Object, ByVal e As System.EventArgs)  
        Dim chkbox As CheckBox = sender 
        lblShowComplete.Text = "False" 
        If chkbox.Checked Then  
            lblShowComplete.Text = "True" 
        End If  
        RadGrid1.Rebind()  
    End Sub 

Again, seems to work OK.  I look at the value in lblShowComplete when rebinding the grid and can evaluate whether to show complete records or not.

What I cannot seem to work out is how to keep the checkbox checked when the grid is re-displayed.  I could do it by setting the CheckBox control, but don't know how to access it within the Command Item Template.

Any help appreciated

David Penny

5 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 08 Jan 2009, 05:06 PM
Hello David,

Please test the following approach:
GridItem commandItem = RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0]; 
CheckBox cbox = (CheckBox)commandItem.FindControl("chkShowCompleted"); 
cbox.Checked = false

Let me know if you need more information.

Best regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
David Penny
Top achievements
Rank 2
answered on 08 Jan 2009, 10:21 PM
Daniel,

Thanks for the response.  Where do I place this code?

David Penny
0
Accepted
Shinu
Top achievements
Rank 2
answered on 09 Jan 2009, 04:49 AM
Hi David,

You can try the above given code in the Grid PreRender event. For persisting the CheckBox state on Rebind I would suggest you to save the CheckBox state in a ViewState.

CS:
 protected void chkShowCompleted_CheckChanged(object sender, EventArgs e) 
    { 
        CheckBox chkbox =(CheckBox)sender; 
 
        if (chkbox.Checked) 
        { 
            lblShowComplete.Text = "True"
            ViewState["CheckBox"] = "true"
        } 
        else 
        { 
            lblShowComplete.Text = "False"
            ViewState["CheckBox"] = "false"
        } 
 
         RadGrid1.Rebind(); 
    } 
 
 
 
 protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        GridItem commandItem = RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0]; 
        CheckBox cbox = (CheckBox)commandItem.FindControl("chkShowCompleted"); 
        if (ViewState["CheckBox"]=="true"
            cbox.Checked = true
        else 
            cbox.Checked = false
 
    } 


Shinu

0
David Penny
Top achievements
Rank 2
answered on 09 Jan 2009, 09:05 AM
Shinu,

Thanks - that works brilliantly.

David Penny
0
Princy
Top achievements
Rank 2
answered on 09 Jan 2009, 10:28 AM
Hello David,

The state of CheckBox control in GridTemplateColumn will not persist on rebinding the RadGrid. You can refer the following documentation on how to retain the checkbox state on rebind. Checkout the link bekow.
Persisting CheckBox control state in GridTemplateColumn on rebind

Thanks,
Princy.
Tags
Grid
Asked by
David Penny
Top achievements
Rank 2
Answers by
Daniel
Telerik team
David Penny
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Share this question
or