CheckBox in Command Item Template

6 posts, 1 answers
  1. David Penny
    David Penny avatar
    174 posts
    Member since:
    Jun 2006

    Posted 08 Jan 2009 Link to this post

    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
  2. Daniel
    Admin
    Daniel avatar
    4945 posts

    Posted 08 Jan 2009 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. David Penny
    David Penny avatar
    174 posts
    Member since:
    Jun 2006

    Posted 08 Jan 2009 Link to this post

    Daniel,

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

    David Penny
  5. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 08 Jan 2009 Link to this post

    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

  6. David Penny
    David Penny avatar
    174 posts
    Member since:
    Jun 2006

    Posted 09 Jan 2009 Link to this post

    Shinu,

    Thanks - that works brilliantly.

    David Penny
  7. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 09 Jan 2009 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017