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

Creating A Row Selection - Checkbox Column

2 Answers 106 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ben
Top achievements
Rank 1
Ben asked on 17 Apr 2012, 02:40 PM
I am trying to create a RowSelection Checkbox Column on my grid programatically, no Templates on the ASCX file. Reason for programatically creating it is because I am trying to create a "Wrapper" class using the RADGrid, so I can easily use it all over my application directly in my code.

I first tried using Telerik.Web.UI.GridClientSelectColumn. Problem is that because it's a "ClientSide" selection, it kept losing the SelectedRow values on PageChange. I looking into retaining the SelectedRows using JavaScript, but from what I can tell, it wouldn't be feasible for my particular situation (Passing the selected rows back and forth between client/server seemed way to complicated and possible unworkable for my situation). So I decided to switch to Server-Side code. From what I gathered, in order to do that I had to use a different ColumnType, so I found Telerik.Web.UI.GridCheckBoxColumn. I have a few problems that I cant seem to figure out for the life of me...

1) The column when rendered, is disabled..have no clue how to enable it
2) The column is missing the checkbox in the header as a "Select All" feature
3) After creating the column, in the related event methods, I can't seem to obtain a reference to said column.
     - ie: In the ItemCreated() event, e.Item.FindControl("chkSelect") returns Nothing

Below are snippets of my code...

Column Creation Code:
'***************************************
' Initialize Variables
'***************************************
Dim objColumn_Select As New Telerik.Web.UI.GridCheckBoxColumn
 
'***************************************
' Add Button Column To Grid
'***************************************
Me.gridMain.Columns.Add(objColumn_Select)
 
'***************************************
' Define Column Properties
'***************************************
With objColumn_Select
     .ItemStyle.HorizontalAlign = HorizontalAlign.Center
     .HeaderText = ""
     .UniqueName = "chkSelect"
End With


Grid's ItemCreated() event:
'***************************************
' Add Handler To Custom ItemPreRender Event
'***************************************
AddHandler e.Item.PreRender, AddressOf Me.gridMain_ItemPreRender
 
'***************************************
' Add "OnCheckedChanged" Event
'***************************************
CType(e.Item.FindControl("chkSelect"), CheckBox).Attributes("onCheckedChanged") = "gridMain_ToggleRowSelection"

The above code doesn't work, crashing on the FindControl() method because it doesn't find the control "chkSelect".

Any help would be greatly appreciated!

2 Answers, 1 is accepted

Sort by
0
Ben
Top achievements
Rank 1
answered on 17 Apr 2012, 09:01 PM
Ok, I've completely redesigned my code after having found some information on Programatically creating Templates.

Now, I have the CheckBoxes appearing in the Header & Row perfectly... The only issue I have, is that the "CheckChanged() event is not firing. But I did add the event handler.  Below is my code, can anyone help me please?

Custom Class That Implements ITemplate:
'''------------------------------------------------------------------------------------------
''' <summary>   Implements the InstantiateIn method for a ColumnTemplate. </summary>
'''
''' <param name="p_objContainer">   Reference to the containing control. </param>
'''
''' <history>
'''     Date       Developer          Description
'''     =====================================================================================
'''     04/17/2012 [Ben Santiago]     Created
''' </history>
'''------------------------------------------------------------------------------------------
Public Sub InstantiateIn(ByVal p_objContainer As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
     '***************************************
     ' Initialize Variables
     '***************************************
     Dim objCheckBox As New CheckBox
 
     '***************************************
     ' Define CheckBox Control
     '***************************************
     With objCheckBox
          .ID = "chkSelectRow"
          .AutoPostBack = True
          AddHandler .CheckedChanged, AddressOf Me.Checkbox_CheckChanged
     End With
 
     '***************************************
     ' Add Control To Template
     '***************************************
     p_objContainer.Controls.Add(objCheckBox)
End Sub

The Custom Event Handler Method:


'''------------------------------------------------------------------------------------------
''' <summary>   Handles the CheckChanged event of the CheckBox control. </summary>
'''
''' <param name="sender">   Source of the event. </param>
''' <param name="e">        Event information. </param>
'''
''' <history>
'''     Date       Developer          Description
'''     =====================================================================================
'''     04/17/2012 [Ben Santiago]     Created
''' </history>
'''------------------------------------------------------------------------------------------
Public Sub Checkbox_CheckChanged(ByVal sender As Object, ByVal e As System.EventArgs)
     '***************************************
     ' Initialize Variables
     '***************************************
     Dim objCheckBox As CheckBox = CType(sender, CheckBox)
     Dim objGridHeaderItem As GridHeaderItem = CType(objCheckBox.NamingContainer, GridHeaderItem)
 
     '***************************************
     ' Do Whatever Code I Need To Do Here
     '***************************************
 
End Sub

Problem is, the above Event Handler Method never fires... Any ideas?????
0
Ben
Top achievements
Rank 1
answered on 18 Apr 2012, 02:49 PM
I fixed my problem.  Apparently there was an error happening in the code that because of "Partial Rendering" wasing displaying an error. Once I fixed the code error, the handler bindings worked.
Tags
Grid
Asked by
Ben
Top achievements
Rank 1
Answers by
Ben
Top achievements
Rank 1
Share this question
or