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

checkboxlist in Radgrid

4 Answers 206 Views
Grid
This is a migrated thread and some comments may be shown as answers.
mac
Top achievements
Rank 1
mac asked on 23 Oct 2008, 09:54 PM
I don't know why I have such a hard time grasping the fundamentals of radgrid when in edit/insert mode but I sure do. I do not see how to edit/trap the values I am editing. I am trying to work with a checkbox list that has the days of week in a gridtemplatecolum. I am using editForms mode as per tutorials. I can using the insert_command get the selected values and convert those to a string for sql_insert to a column, however I can't get the value out when in edit mode back into the list. ie. convert Monday,Tuesday into the checkbox list as I can't find the checkboxlist. I am using EditCommand. once i can find the control, reading it back in should not be a problem... its a matter of finding it.

Dim appid As String  
    Dim cblstr As String  
 
      
    Protected Sub SqlDataSource1_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSource1.Inserting  
        e.Command.Parameters("@AppID").Value = Applicants1.appid  
        e.Command.Parameters("@NotAvailalble").Value = cblstr 
    End Sub  
 
 
    Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting  
        If Applicants1.appid IsNot Nothing Then  
            appid = Applicants1.appid  
            e.Command.Parameters("@AppID").Value = appid 
        End If  
 
    End Sub  
 
    Protected Sub RadGrid1_EditCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.EditCommand  
        If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then  
            Dim cbl As CheckBoxList = (TryCast(e.Item, GridEditableItem)).Controls(0).FindControl("CBNotAvail")  
            ' this totally fails as it says it can't find the item  
        End If  
    End Sub  
 
    Protected Sub RadGrid1_InsertCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.InsertCommand  
          
        If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then  
            Dim cbl As CheckBoxList = (TryCast(e.Item, GridEditableItem)).Controls(0).FindControl("CBNotAvail")  
            cblstr = functions.CBListValues(cbl)  
        End If  
 
 
    End Sub 


Source
        <telerik:GridTemplateColumn UniqueName="NotAvailable" HeaderText="Not Available" Visible="False">  
                     <EditItemTemplate> 
                    <asp:CheckBoxList ID="CBNotAvail" runat="server" SelectedValue='<%# Bind("NotAvailalble") %>' > 
                    <asp:ListItem>Monday</asp:ListItem> 
                    <asp:ListItem>Tuesday</asp:ListItem> 
                                            </asp:CheckBoxList> 
                    </EditItemTemplate> 
                    </telerik:GridTemplateColumn> 

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 24 Oct 2008, 05:00 AM
Hello Mac,

If you need to access certain controls on edit/insert, a more suitable place would be the ItemDataBound event of RadGrid as this event would fire after the controls are bound with data:
cs:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
        { 
            GridEditableItem item = (GridEditableItem)e.Item; 
            RadDatePicker datePicker = (RadDatePicker)item.FindControl("CBNotAvail"); 
        } 
    } 

Thanks
Princy.
0
mac
Top achievements
Rank 1
answered on 24 Oct 2008, 04:03 PM
thanks for the reply Princy. I worked a bit more on a demo last night and came up with the following. Kindly asking if you could give it a lookover to see if it is the best method used. The concept is to present the user with a checkboxlist of items. After update or insert, take those selected values and string them together to insert into one column. At time of clicking edit, get the column value and split it back into the checkboxes. I have included my source and code below.
 It does work, but seems like quite a bit of code in order to produce the desired effect.  Thoughts are appreciated as I am constantly learning new methods

Imports Telerik.Web.UI  
Partial Class Default2  
    Inherits System.Web.UI.Page  
    Dim str As String  
    Dim cb As CheckBoxList  
   
   
    Protected Sub RadGrid1_InsertCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.InsertCommand  
        If (TypeOf e.Item Is GridEditableItem) AndAlso (e.Item.IsInEditMode) Then  
            Dim edititem As GridEditableItem = DirectCast(e.Item, GridEditableItem)  
            cb = DirectCast(edititem.FindControl("cblist"), CheckBoxList)  
   
            For i As Integer = 0 To cb.Items.Count - 1  
                If cb.Items(i).Selected = True Then  
                    str += cb.Items(i).Value & ","  
                End If  
            Next  
        End If  
    End Sub  
   
   
    Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound  
        If (TypeOf e.Item Is GridEditableItem) AndAlso (e.Item.IsInEditMode) Then  
            Dim edititem As GridEditableItem = DirectCast(e.Item, GridEditableItem)  
            Dim ddl As TextBox = DirectCast(edititem.FindControl("item"), TextBox)  
            Dim arr As Array = Split(ddl.Text, ",")  
            cb = DirectCast(edititem.FindControl("cblist"), CheckBoxList)  
            For i As Integer = 0 To cb.Items.Count - 1  
                For x As Integer = 0 To UBound(arr)  
                    If arr(x) = cb.Items(i).Value Then  
                        cb.Items(i).Selected = True 
                    End If  
                Next  
            Next  
        End If  
    End Sub  
    Protected Sub RadGrid1_UpdateCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.UpdateCommand  
        If (TypeOf e.Item Is GridEditableItem) AndAlso (e.Item.IsInEditMode) Then  
            Dim edititem As GridEditableItem = DirectCast(e.Item, GridEditableItem)  
            cb = DirectCast(edititem.FindControl("cblist"), CheckBoxList)  
           For i As Integer = 0 To cb.Items.Count - 1  
                If cb.Items(i).Selected = True Then  
                    str += cb.Items(i).Value & ","  
               End If  
            Next  
        End If  
        Label1.Text = str 
    End Sub  
    Protected Sub AccessDataSource1_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles AccessDataSource1.Inserting  
        e.Command.Parameters("Item").Value = str 
    End Sub  
    Protected Sub AccessDataSource1_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles AccessDataSource1.Updating  
        e.Command.Parameters("Item").Value = str 
    End Sub  
End Class  
 
'SOURCE  
<EditFormSettings EditFormType="Template">       <FormTemplate> 
               <table><tr><td> 
               <asp:CheckBoxList ID="cblist" runat="server">  
               <asp:ListItem>1</asp:ListItem> 
               <asp:ListItem>2</asp:ListItem> 
               </asp:CheckBoxList> 
               </td></tr></table> 
<asp:TextBox ID="Item" Text='<%# Bind("item") %>' runat="server" Visible="false" /> 
<asp:Button ID="btnUpdate" Text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "Insert", "Update") %>' 
  runat="server" CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>'>  
    </asp:Button>&nbsp;  
<asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" 
CommandName="Cancel"></asp:Button> 
               </FormTemplate>  </EditFormSettings> 
 
 
 
0
Perry Blanchard
Top achievements
Rank 1
answered on 20 Jan 2015, 08:49 PM
I use your code. the insert commend works, but the update commend only works on first time and it not work on second time when I check different checkbox. I don't know why?
0
Kostadin
Telerik team
answered on 23 Jan 2015, 08:59 AM
Hello,

I test the provided code and on my side seems to work correctly. I attached the test sample, so could you please let me know how it differs from you real setup.

Regards,
Kostadin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
mac
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
mac
Top achievements
Rank 1
Perry Blanchard
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or