checkboxlist in Radgrid

5 posts, 0 answers
  1. mac
    mac avatar
    137 posts
    Member since:
    May 2008

    Posted 23 Oct 2008 Link to this post

    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> 
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 24 Oct 2008 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. mac
    mac avatar
    137 posts
    Member since:
    May 2008

    Posted 24 Oct 2008 Link to this post

    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> 
     
     
     
  5. Perry Blanchard
    Perry Blanchard  avatar
    8 posts
    Member since:
    Jul 2012

    Posted 20 Jan 2015 in reply to mac Link to this post

    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?
  6. Kostadin
    Admin
    Kostadin avatar
    1708 posts

    Posted 23 Jan 2015 Link to this post

    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.

     
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017