Re: Row Selector Column
- From: "Terry Holland" <terryeholland@xxxxxxxxxxxxxxxx>
- Date: Thu, 17 Nov 2005 10:56:53 -0000
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
> |
> |
> |
> |
> |
>
.
- Follow-Ups:
- Re: Row Selector Column
- From: Steven Cheng[MSFT]
- Re: Row Selector Column
- References:
- Row Selector Column
- From: Terry Holland
- RE: Row Selector Column
- From: Steven Cheng[MSFT]
- Row Selector Column
- Prev by Date: RE: Row Selector Column
- Next by Date: Validation Between Controls
- Previous by thread: RE: Row Selector Column
- Next by thread: Re: Row Selector Column
- Index(es):
Relevant Pages
|
Loading