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

CheckBox Problem.

1 Answer 94 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Ryan
Top achievements
Rank 1
Ryan asked on 05 Feb 2009, 05:22 PM
Here is a link my recent, but also current problem: Recent RadGrid post relative to this.

In the interum I have created a function to store the checked boxes and retrieve the ID's for said checks.

I have added teleriks code for ItemCreated and ItemPreRender (see below);
Protected Sub RG2_ItemCreated(ByVal sender As ObjectByVal e As GridItemEventArgs) Handles RG2.ItemCreated  
        If TypeOf e.Item Is GridDataItem Then 
            AddHandler e.Item.PreRender, AddressOf RG2_ItemPreRender  
        End If 
    End Sub 
 
    Protected Sub RG2_ItemPreRender(ByVal sender As ObjectByVal e As EventArgs)  
        CType(CType(sender, GridDataItem)("CheckBoxTemplateColumn").FindControl("chkbxFacility"), CheckBox).Checked = CType(sender, GridDataItem).Selected  
    End Sub 
My new error is "Unable to cast object of type 'Telerik.Web.UI.RadGrid' to type 'Telerik.Web.UI.GridDataItem'", so what gives.

Even with my new method, to track each check and uncheck, no data fills the RadGrid. Actually, the grid shows up with only the header column showing.

Another minor problem is when I fill the last RadGrid with data (forced), if I check the select all check box nothing happens, yet if check the internal rows they select all...I can't seem to figure this issue.

Here is how I pull and convert, keep track of the checks and changes to my data;
For Each ID As String In facilityID  
                IDList += ID & "," 
            Next 
            IDList.Remove(IDList.LastIndexOf(","), 1)  
 
'Track the checks  
 
Dim facilityID As New List(Of String)  
        For Each dataItem As GridDataItem In RG3.MasterTableView.Items  
            If CType(dataItem.FindControl("chkbxFacility"), CheckBox).Checked = True Then 
                facilityID.Add(dataItem.OwnerTableView.DataKeyValues(dataItem.ItemIndex)("FacilityID").ToString())  
            End If 
        Next 
        Return facilityID  
 
'function for any changes or additions  
 
If (CType(sender, CheckBox)).Checked Then 
            For Each dataItem As GridDataItem In RG2.MasterTableView.Items  
                CType(dataItem.FindControl("chkbxFacility"), CheckBox).Checked = True 
                dataItem.Selected = True 
            Next 
        Else 
            For Each dataItem As GridDataItem In RG2.MasterTableView.Items  
                CType(dataItem.FindControl("chkbxFacility"), CheckBox).Checked = False 
                dataItem.Selected = False 
            Next 
        End If 
        'Make alternate call to FillDepartment to recognize any changes to the facility check box list  
        FillDepartment(RG2_GetFacilityIDs) 


Any help with all or some would be greatly appreciated.

Thanks,

-Ryan

1 Answer, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 09 Feb 2009, 05:48 PM
Hello Ryan,

I was able to implement your approach, and successfully find and check the ASP CheckBox. Please review the following code snippet:
    Protected Sub RadGrid1_ItemCreated(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated 
        If TypeOf e.Item Is GridDataItem Then 
            AddHandler e.Item.PreRender, AddressOf RadGrid1_ItemPreRender 
        End If 
    End Sub 
 
    Protected Sub RadGrid1_ItemPreRender(ByVal sender As ObjectByVal e As EventArgs) 
        DirectCast(DirectCast(sender, Telerik.Web.UI.GridDataItem)("CheckBoxTemplateColumn").FindControl("chkbxFacility"), CheckBox).Checked = True '"set the proper value" 
    End Sub 
 
I will suggest you use the DirectCast method, rather than CType.

You can use the same approach in the ItemPreRender event handler for your custom method.

Best regards,
Georgi Krustev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Ajax
Asked by
Ryan
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Share this question
or