Re: Row Selector Column

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi Terry,

I've just modified my former CheckBox Column example and added a Header
CheckBox in it which will control All Check and All UnCheck through
clientside script, here is the code:


================================
Public Class SCCheckBoxColumn
Inherits DataGridColumn

Private _datafield As String = String.Empty
Private _cbid As String = "cbValue"
Private _gridid = String.Empty


Public Property DataField() As String
Get
Return _datafield
End Get
Set(ByVal Value As String)
_datafield = value
End Set
End Property

Public Property CheckBoxID() As String
Get
Return _cbid
End Get
Set(ByVal Value As String)
_cbid = value
End Set
End Property

Public Property DataGridID() As String
Get
Return _gridid
End Get
Set(ByVal Value As String)
_gridid = Value
End Set
End Property


Public Overrides Sub InitializeCell(ByVal cell As TableCell, ByVal
columnIndex As Integer, ByVal itemType As ListItemType)
MyBase.InitializeCell(cell, columnIndex, itemType)

Select Case itemType
Case ListItemType.Header


Dim cbAll As CheckBox = New CheckBox
cbAll.ID = "cbAll"
cbAll.Attributes("onclick") =
String.Format("chk_onclick(this,'{0}','{1}');", _gridid, _cbid)

cell.Controls.Add(cbAll)

AddHandler cell.PreRender, AddressOf cell_PreRender


Exit Sub

Case ListItemType.Item, ListItemType.AlternatingItem,
ListItemType.EditItem, ListItemType.SelectedItem

Dim cb As CheckBox = New CheckBox
cb.ID = _cbid
cell.Controls.Add(cb)
AddHandler cell.DataBinding, AddressOf Cell_DataBinding
Exit Sub

Case Else

Exit Sub
End Select
End Sub


Public Sub Cell_DataBinding(ByVal sender As Object, ByVal e As
EventArgs)
' no databinding needed
If _datafield = String.Empty Then
Return
End If

Dim cell As TableCell = sender
Dim item As DataGridItem = cell.NamingContainer 'as
DataGridItem
Dim cb As CheckBox = item.FindControl(_cbid)

Try

Dim td As PropertyDescriptor
td =
TypeDescriptor.GetProperties(item.DataItem).Find(_datafield, False)

Dim obj1 As Object = td.GetValue(item.DataItem)


cb.Checked = CType(obj1, Boolean)
Catch ex As Exception
Throw New HttpException("Invalid DataField for
SCCheckBoxColumnNot ")
End Try
End Sub





Public Sub cell_PreRender(ByVal source As Object, ByVal e As
EventArgs)

Dim cbAll As Control = source


Dim sb As New System.Text.StringBuilder

sb.Append("<script language='javascript'>")
sb.Append("function chk_onclick(span,gdid,cbid){")
sb.Append("var chks = document.getElementsByTagName('input');")
sb.Append("var i;")
sb.Append("for(i=0;i<chks.length;i++){")
sb.Append("if(chks[i].type == 'checkbox'){")
sb.Append("var chk = chks[i];")
sb.Append("if(chk.id.indexOf(gdid) > -1 && chk.id.indexOf(cbid)
> -1)")
sb.Append("{chk.checked = span.checked;}}}}")
sb.Append("</script>")



cbAll.Page.RegisterStartupScript(cbAll.UniqueID &
"CheckAllScript", sb.ToString())


End Sub
End Class
==========================


# Notice that we need to specify the DataGridID besides the
CheckBoxID(DataGridID should be idential to the checkBoxColumn's Container
DataGrid's ID) , here is a example code snippet of dynamically adding such
as column:

===================
Dim cc1 As SCCheckBoxColumn = New SCCheckBoxColumn
cc1.HeaderText = "Selected"
cc1.CheckBoxID = "cbSelected"
cc1.DataGridID = dgCustom.ID

dgCustom.Columns.Add(cc1)
================================

BTW, I've also tested the postback means, and that also worked. As long as
we do not manually set initial value for the Header CheckBox, it's
CheckedChanged event will fire correctly.

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "Terry Holland" <terryeholland@xxxxxxxxxxxxxxxx>
| References: <OaeWICq6FHA.2012@xxxxxxxxxxxxxxxxxxxx>
<DlthMey6FHA.3496@xxxxxxxxxxxxxxxxxxxxx>
| Subject: Re: Row Selector Column
| Date: Thu, 17 Nov 2005 10:56:53 -0000
| Lines: 435
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
| Message-ID: <#T3SlW26FHA.3132@xxxxxxxxxxxxxxxxxxxx>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| NNTP-Posting-Host: host200.multiserv.com 194.200.135.200
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridcontrol:14733
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
|
| Hi Steven - we meet again :-)
|
| I guess that client side script is the way to go. This is outside of my
| comfort zone so I had a look through some sample code that you sent to me
on
| a previous thread. The code you sent was an example of how to dynamically
| create clientside code on one of my custom columns. I am trying to modify
| this code for use in the Row Selector column. The following code is for a
| custom textbox column (this is the code you sent me and it works fine)
and a
| custom checkbox column. I have attempted to add some clientside code using
| the same method that you did in the textbox column ie adding a prerender
| function and adding the code hear
|
| This is the code that you sent me to detect changes in a textbox column
| ======================================
| Public Class SCTextBoxColumn
| Inherits DataGridColumn
|
| Private _datafield As String = String.Empty
| Private _txtid As String = "txtValue"
|
| Public Property DataField() As String
| Get
| Return _datafield
| End Get
| Set(ByVal Value As String)
| _datafield = value
| End Set
| End Property
|
| Public Property TextBoxID() As String
| Get
| Return _txtid
| End Get
| Set(ByVal Value As String)
| _txtid = value
| End Set
| End Property
|
| Public Overrides Sub InitializeCell(ByVal cell As TableCell, ByVal
| columnIndex As Integer, ByVal itemType As ListItemType)
| MyBase.InitializeCell(cell, columnIndex, itemType)
|
|
| Select Case itemType
| Case ListItemType.Header
| cell.Text = HeaderText
|
|
| Case ListItemType.Item, ListItemType.AlternatingItem,
| ListItemType.EditItem, ListItemType.SelectedItem
|
| Dim txt As TextBox = New TextBox
| txt.ID = _txtid
| cell.Controls.Add(txt)
|
| 'bind prerender handler for textbox
| AddHandler txt.PreRender, AddressOf txt_PreRender
|
| AddHandler cell.DataBinding, AddressOf
Cell_DataBinding
|
| Case Else
|
|
| End Select
| End Sub
|
|
| Private Sub Cell_DataBinding(ByVal sender As Object, ByVal e As
| EventArgs)
| Dim cell As TableCell = sender
| Dim item As DataGridItem = cell.NamingContainer
| Dim txt As TextBox = item.FindControl(_txtid)
|
| Try
|
| Dim td As PropertyDescriptor
| td =
| TypeDescriptor.GetProperties(item.DataItem).Find(_datafield, False)
|
|
| Dim obj1 As Object = td.GetValue(item.DataItem)
| txt.Text = CType(obj1, String)
| Catch ex As Exception
| Throw New HttpException("Invalid DataField for
| SCTextBoxColumnNot ")
| End Try
| End Sub
|
| Private Sub txt_PreRender(ByVal source As Object, ByVal e As
| EventArgs)
| Dim txt As TextBox = source
|
| Dim script As String
|
| script = "<script language='javascript'>function
| dgtxtcol_onchange(txt){" _
| & "alert(""The textbox's content has been changed to "" +
| txt.value + " _
| & """\r\nWe can add our content formatting here
| now!"");}</script>"
|
|
| Dim key As String = "SCTextBoxColumnScript"
|
| If Not txt.Page.IsClientScriptBlockRegistered(key) Then
| txt.Page.RegisterClientScriptBlock(key, script)
| End If
|
| txt.Attributes.Add("onchange", "dgtxtcol_onchange(this);")
|
| End Sub
|
|
|
| End Class
| ==========================
|
| This works fine. If I put a break on the txt_PreRender function, the code
| jumps into this function each time the control is rendered.
| I am trying to do the same thing for the CheckBox column using the
following
| code, but putting a break on the equivalent function for this control
| (cb_PreRender) has no effect. The application never jumps into this
| function.
|
| ===========================
| Public Class SCCheckBoxColumn
| Inherits DataGridColumn
|
| Private _datafield As String = String.Empty
| Private _cbid As String = "cbValue"
|
|
| Public Property DataField() As String
| Get
| Return _datafield
| End Get
| Set(ByVal Value As String)
| _datafield = value
| End Set
| End Property
|
| Public Property CheckBoxID() As String
| Get
| Return _cbid
| End Get
| Set(ByVal Value As String)
| _cbid = value
| End Set
| End Property
|
| Public Overrides Sub InitializeCell(ByVal cell As TableCell, ByVal
| columnIndex As Integer, ByVal itemType As ListItemType)
| MyBase.InitializeCell(cell, columnIndex, itemType)
|
| Select Case itemType
| Case ListItemType.Header
| Dim cb As CheckBox = New CheckBox
| cb.ID = _cbid
| cell.Controls.Add(cb)
|
| 'FOLLOWING EXAMPLE OF TEXTBOX COLUMN
| AddHandler cb.PreRender, AddressOf cb_PreRender
| Exit Sub
|
| Case ListItemType.Item, ListItemType.AlternatingItem,
| ListItemType.EditItem, ListItemType.SelectedItem
|
| Dim cb As CheckBox = New CheckBox
| cb.ID = _cbid
| cell.Controls.Add(cb)
|
| 'FOLLOWING EXAMPLE OF TEXTBOX COLUMN
| AddHandler cb.PreRender, AddressOf cb_PreRender
|
| AddHandler cell.DataBinding, AddressOf
Cell_DataBinding
| Exit Sub
|
| Case Else
|
| Exit Sub
| End Select
| End Sub
|
|
| 'I PUT A BREAK ON THIS FUNCTION BUT APP NEVER STOPS HERE
|
| Private Sub cb_PreRender(ByVal source As Object, ByVal e As
| EventArgs)
| Dim cb As CheckBox = source
|
| Dim script As String
|
| script = "<script language='javascript'>function
| dgcbcol_onclick(cb){" _
| & "alert(""The checkbox value has been changed to "" +
cb.value
| + " _
| & """\r\nWe can add our code to respond to checkbox onclick
| event here."");}</script>"
|
|
| Dim key As String = "SCTextBoxColumnScript"
|
| If Not cb.Page.IsClientScriptBlockRegistered(key) Then
| cb.Page.RegisterClientScriptBlock(key, script)
| End If
|
| cb.Attributes.Add("onclick", "dgcbcol_onclick(this);")
|
| End Sub
|
| Public Sub Cell_DataBinding(ByVal sender As Object, ByVal e As
| EventArgs)
| ' no databinding needed
| If _datafield = String.Empty Then
| Return
| End If
|
| Dim cell As TableCell = sender
| Dim item As DataGridItem = cell.NamingContainer 'as
| DataGridItem
| Dim cb As CheckBox = item.FindControl(_cbid)
|
| Try
|
| Dim td As PropertyDescriptor
| td =
| TypeDescriptor.GetProperties(item.DataItem).Find(_datafield, False)
|
| Dim obj1 As Object = td.GetValue(item.DataItem)
|
|
| cb.Checked = CType(obj1, Boolean)
| Catch ex As Exception
| Throw New HttpException("Invalid DataField for
| SCCheckBoxColumnNot ")
| End Try
| End Sub
|
| End Class
| ===========================
|
|
|
|
|
| "Steven Cheng[MSFT]" <stcheng@xxxxxxxxxxxxxxxxxxxx> wrote in message
| news:DlthMey6FHA.3496@xxxxxxxxxxxxxxxxxxxxxxxx
| > Hi Terry,
| >
| > Welcome.
| > Seems you're still on your way developing the custom DataGrid Columns
:-).
| > As for the CheckBox postback events problem you mentioned in this post,
I
| > think you assumption about the intializing setting on the Header
| > CheckBox.Checked property(as false) is reasonable, and I've also
| > encountered such problems when developing composite control with
postback
| > and status changes.. (Such control like CheckBox, TextBox's XXXchanged
| > event is based on the comparation between the control instance's
property
| > value and the value retrieved from viewstate....) So have you tried
| > removing the ctl.Checked = False line in the following code to see
| > whether it helps?
| >
| > =============
| > Select Case itemType
| > Case ListItemType.Header
| > Dim ctl As CheckBox = New CheckBox
| > ctl.AutoPostBack = True
| > ctl.Checked = False
| > cell.Controls.Add(ctl)
| > ============
| >
| > Also, as for "checking All", "Unchecking All" functionality, we can also
| > consider use clientside script to make them all complete at clientside
| > without postback...
| >
| > Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| > --------------------
| > | From: "Terry Holland" <terryeholland@xxxxxxxxxxxxxxxx>
| > | Subject: Row Selector Column
| > | Date: Wed, 16 Nov 2005 11:25:46 -0000
| > | Lines: 113
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
| > | Message-ID: <OaeWICq6FHA.2012@xxxxxxxxxxxxxxxxxxxx>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| > | NNTP-Posting-Host: host221.multiserv.com 194.200.135.221
| > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
| > | Xref: TK2MSFTNGXA02.phx.gbl
| > microsoft.public.dotnet.framework.aspnet.datagridcontrol:14721
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| > |
| > | I have created a custom control that dynamically creates a datagrid at
| > | runtime. The datagrid is made up of a collection of custom columns.
| One
| > of
| > | my custom columns is a Row Selecter column. This comprises a Checkbox
| for
| > | each row and one in the header of datagrid.
| > |
| > | My row selection is working fine and I am able to loop through the
| > selected
| > | items in my grid and perform an action on them. What I want to be
able
| to
| > | do now is give the user the ability to select or un-select all rows by
| > | clicking the checkbox in the header.
| > |
| > | The trouble that I am experiencing is that the CheckChanged event of
the
| > | checkbox in the header only fires if the value changes from the value
| that
| > | it is when it is dynamically created.
| > |
| > | Each postback causes my custom control to be built. When it is built
| the
| > | row selector column (clsDGCCheckBoxColumn) is added and the
| InitializeCell
| > | method adds a checkbox to the header column with the checked value =
| > False.
| > | When the user checks this control, the SelectColumn method runs
| > correctly.
| > | If the user unchecks the checkbox in the header, the SelectColumn
method
| > | does not run. Im assuming that this is because the value of this
| control
| > | when it is built is False and when the user unchecks the control it is
| > once
| > | again False, therefore the value has not changed therefore the event
| does
| > | not fire.
| > |
| > | Could someone advise me how to get around this problem - I guess that
I
| > need
| > | to modify InitializeCell so that the value that I set the checkbox to
is
| > as
| > | it was before postback.
| > |
| > | Thanks
| > |
| > | Terry Holland
| > |
| > | ##############################
| > | Custom CheckBox Column
| > | ##############################
| > | Imports System.Web.UI.WebControls
| > | Imports System.Web.UI
| > | Imports System.ComponentModel
| > |
| > | Public Class clsDGCCheckBoxColumn
| > | Inherits clsCustomDGC_BASE
| > |
| > | Public Sub New(ByVal objDGC As clsDataGridColumn)
| > | MyBase.New(objDGC, "chk")
| > | m_clrBackColor = System.Drawing.Color.LightCyan
| > | End Sub
| > |
| > | Public Overrides Sub InitializeCell(ByVal cell As TableCell, ByVal
| > | columnIndex As Integer, ByVal itemType As ListItemType)
| > | MyBase.InitializeCell(cell, columnIndex, itemType)
| > |
| > | Select Case itemType
| > | Case ListItemType.Header
| > | Dim ctl As CheckBox = New CheckBox
| > | ctl.AutoPostBack = True
| > | ctl.Checked = False
| > | cell.Controls.Add(ctl)
| > |
| > | AddHandler ctl.CheckedChanged, AddressOf SelectColumn
| > '(Me,
| > | ctl.Checked)
| > |
| > | Case ListItemType.Item, ListItemType.AlternatingItem,
| > | ListItemType.EditItem, ListItemType.SelectedItem
| > |
| > | Dim ctl As CheckBox = New CheckBox
| > | SetControlProperties(cell, ctl)
| > |
| > | cell.Controls.Add(ctl)
| > |
| > | AddHandler cell.DataBinding, AddressOf
Cell_DataBinding
| > | Case Else
| > |
| > | End Select
| > | End Sub
| > |
| > | Private Sub Cell_DataBinding(ByVal sender As Object, ByVal e As
| > | EventArgs)
| > | Dim cell As TableCell = sender
| > | Dim item As DataGridItem = cell.NamingContainer
| > | Dim ctl As CheckBox = item.FindControl(m_strID)
| > | Dim pd As PropertyDescriptor =
| > | TypeDescriptor.GetProperties(item.DataItem).Find(m_strDataField, True)
| > |
| > | Try
| > | Dim obj1 As Object = pd.GetValue(item.DataItem)
| > |
| > | ctl.Checked = CType(obj1, Boolean)
| > | Catch ex As Exception
| > | Throw New Exception("Invalid dataField for custom column
| > | clsDGCLabel")
| > | End Try
| > | End Sub
| > |
| > |
| > |
| > | Private Sub SelectColumn(ByVal sender As Object, ByVal e As
| > EventArgs) '
| > | ByVal booSelect As Boolean)
| > | Dim ctlHeader As CheckBox = sender
| > | Dim grid As DataGrid =
ctlHeader.NamingContainer.NamingContainer
| > | Dim booSelect As Boolean = ctlHeader.Checked
| > |
| > | Dim o As CheckBox
| > |
| > | For Each dgi As DataGridItem In grid.Items
| > | o = dgi.FindControl(m_strID)
| > | o.Checked = booSelect
| > | Next
| > | End Sub
| > | End Class
| > |
| > |
| > |
| > |
| > |
| >
|
|
|

.



Relevant Pages

  • Re: Row Selector Column
    ... create clientside code on one of my custom columns. ... jumps into this function each time the control is rendered. ... I am trying to do the same thing for the CheckBox column using the following ...
    (microsoft.public.dotnet.framework.aspnet.datagridcontrol)
  • RE: Checkbox does not respect checked value if disabled client side
    ... you're using the ASP.NET CheckBox server control on ... when the checkbox is disabled at clientside, when postback, the checkbox's ... ASP.NET server control's control model and state persistent mechanism. ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: Checkbox does not respect checked value if disabled client side
    ... Disable the checkbox in a postback. ... it's current state in a hidden TextBox that runs on the server (it has to be ... you're using the ASP.NET CheckBox server control on ... > when the checkbox is disabled at clientside, when postback, the checkbox's ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: How to modify label.text in a dynamically generated label in VB.net
    ... hybrid control that contains a checkbox and a label. ... That frees up the tag ... CheckBox control and the value was the Label control. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Checkbox does not respect checked value if disabled client side
    ... if you think it' ok that we makes the checkbox hidden instead ... of disabling it, we can try using clientside script to hide the checkbox ... | it's current state in a hidden TextBox that runs on the server (it has to ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)