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

General Question - CheckBoxList in Grid

1 Answer 105 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mario
Top achievements
Rank 1
Mario asked on 29 Mar 2012, 11:00 AM
Hello,
how can i use a checkboxlist in the grid?
I have created a grid with inside a EditItemTemplate:
<telerik:GridTemplateColumn DataField="TypeObjectTypes"
    FilterControlAltText="Filter TypeObjectTypes column" HeaderText="Object Type"
    UniqueName="TypeObjectTypes">
    <EditItemTemplate>
        <asp:CheckBoxList ID="CblObjectTypes" runat="server">
            <asp:ListItem Value="DynamicImage">Dynamic Image</asp:ListItem>
            <asp:ListItem Value="StaticURL">Static URL</asp:ListItem>
            <asp:ListItem>Static Text</asp:ListItem>
            <asp:ListItem Value="DynamicScrolltext">Dynamic Scrolltext</asp:ListItem>
            <asp:ListItem Value="FreeText">Free Text</asp:ListItem>
            <asp:ListItem>Object List</asp:ListItem>
        </asp:CheckBoxList>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="TypeObjectTypesLabel" runat="server"
            Text='<%# Eval("TypeObjectTypes") %>'></asp:Label>
    </ItemTemplate>
</telerik:GridTemplateColumn>
...
</telerik:RadGrid>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server"
    DeleteMethod="DeleteLayoutType" InsertMethod="InsertLayoutType"
    SelectMethod="GetLayoutTypes"
    TypeName="LayoutTypesBLL" UpdateMethod="UpdateLayoutType" >
    <DeleteParameters>
        <asp:Parameter Name="TypeID" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="TypeName" Type="String" />
        <asp:Parameter Name="TypeText" Type="String" />
        <asp:Parameter Name="TypeObjectTypes" Type="String" />
        <asp:Parameter Name="TypeMenu" Type="String" />
        <asp:Parameter Name="TypePriority" Type="Int32" />
        <asp:Parameter Name="ProjectID" Type="Int64" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="TypeName" Type="String" />
        <asp:Parameter Name="TypeText" Type="String" />
        <asp:Parameter Name="TypeObjectTypes" Type="String" />
        <asp:Parameter Name="TypeMenu" Type="String" />
        <asp:Parameter Name="TypePriority" Type="Int32" />
        <asp:Parameter Name="ProjectID" Type="Int64" />
        <asp:Parameter Name="TypeID" Type="Int32" />
    </UpdateParameters>
</asp:ObjectDataSource>

When I create a new record i need this format in the database field "TypeObjectTypes" from the selected items from the CheckBoxList:
"DynamicScrolltext,FreeText".
How can i save the selected items in the DataBase and which event can i use for this?
When i show the EditItemTemplate, how can i select the right items in the CheckBoxList from this format ("DynamicScrolltext,FreeText")?

Best regardsReiner

1 Answer, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 03 Apr 2012, 10:23 AM
Hi,

 In order to get and set values to the CheckBoxList you first need to access it from the EditTemplate.
When you put the row in edit mode you can use the ItemDataBound event to access the CheckBoxList and set the desired value:

void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        var formItem = e.Item as GridEditFormItem;
        if (formItem != null && formItem.IsInEditMode)
        {
            var checkBoxList = formItem["TypeObjectTypes"].FindControl("CblObjectTypes") as CheckBoxList;
 
            //you can get the DataBase value holding the comma-separated format from the DataItem property
            var businessObject = (CustomObject)formItem.DataItem;
            var commaSeparatedValues = businessObject.SomeProperty;
            //split the text and check the proper values in the CheckBoxList
        }
    }


You can use the UpdateCommand event of the grid (with AllowAutomaticUpdates set to "false" ) to save values to the database. Then this event should fire and you can access there the CheckBoxList and save the desired values:
void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        var checkBoxList = (e.Item as GridEditFormItem)["TypeObjectTypes"].Controls[0] as CheckBoxList;
        //get values from the checkBox list
        //save them to the database
        //rebind the grid
    }

More information about accessing cells and controls can be found in this help topic:
http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html 

Regards,
Marin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Mario
Top achievements
Rank 1
Answers by
Marin
Telerik team
Share this question
or