Jbond
asked on
DataList Dynamic Controls Header Event
Does anyone know how to raise an event in a DataList Header? I am dynamically creating linkbutton controls and placing them in a panel in a DataList Header Template. I want to raise an event when the LinkButton in the Header is clicked.
I can do this for a DataList Item by raising the DataList.ItemCommand and conditionally checking the DataListCommandEventArgs.C ommandName but the events for Header items appear to be handled differently.
I can do this for a DataList Item by raising the DataList.ItemCommand and conditionally checking the DataListCommandEventArgs.C
Regarding raising an event, you can use delegates to create a custom event linked to your linkbutton click event inside your template.
ASKER
ItemCommand and ItemDataBound events do not fire when clicking the LinkButtons in the Template Header.(I've tried them both) From what I gather delegates pass one procedure to another and sense ItemCommand and ItemDataBound events aren't firing I'm not sure this will help.
At this point I'm considering using a parameterised URL but would rather find a way to keep from poluting the URL.
Any other ideas?
At this point I'm considering using a parameterised URL but would rather find a way to keep from poluting the URL.
Any other ideas?
OK. What I meant by delegates is used AddHandler.
I have a sample here just to give you an idea. The sample below is about a checkbox placed in the Header of a DataGridView. The concept is just the same as your LinkButton in a DataList header. Take note of AddHandler. You might need a FindControlRecursive function to find the linkbutton control in your DataList.
In the Form Load
Dim ckBox As CheckBox = New CheckBox()
'Get the column header cell bounds
Dim rect As Rectangle = Me.DataGridView1.GetCellDi splayRecta ngle(0, -1, True)
ckBox.Size = New Size(18, 18)
'Change the location of the CheckBox to make it stay on the header
ckBox.Location = rect.Location
AddHandler ckBox.CheckedChanged, AddressOf ckBox_CheckedChanged
The Event:
Private Sub ckBox_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
For j As Integer = 0 To Me.DataGridView1.RowCount - 1
DataGridView1(0, j).Value = CType(sender, CheckBox).Checked
Next
Me.DataGridView1.EndEdit()
End Sub
FindControlRecursive:
ex. Dim lnk as linkbutton = CType(FindControlRecursive (DataList1 ,"lnkbutto n1"),linkb utton)
Public Shared Function FindControlRecursive(ByVal root As Control, ByVal id As String) As Control
If (root.ID = id) Then
Return root
End If
For Each c As Control In root.Controls
Dim t As Control = FindControlRecursive(c, id)
If (Not (t) Is Nothing) Then
Return t
End If
Next
Return Nothing
End Function
I have a sample here just to give you an idea. The sample below is about a checkbox placed in the Header of a DataGridView. The concept is just the same as your LinkButton in a DataList header. Take note of AddHandler. You might need a FindControlRecursive function to find the linkbutton control in your DataList.
In the Form Load
Dim ckBox As CheckBox = New CheckBox()
'Get the column header cell bounds
Dim rect As Rectangle = Me.DataGridView1.GetCellDi
ckBox.Size = New Size(18, 18)
'Change the location of the CheckBox to make it stay on the header
ckBox.Location = rect.Location
AddHandler ckBox.CheckedChanged, AddressOf ckBox_CheckedChanged
The Event:
Private Sub ckBox_CheckedChanged(ByVal
For j As Integer = 0 To Me.DataGridView1.RowCount - 1
DataGridView1(0, j).Value = CType(sender, CheckBox).Checked
Next
Me.DataGridView1.EndEdit()
End Sub
FindControlRecursive:
ex. Dim lnk as linkbutton = CType(FindControlRecursive
Public Shared Function FindControlRecursive(ByVal
If (root.ID = id) Then
Return root
End If
For Each c As Control In root.Controls
Dim t As Control = FindControlRecursive(c, id)
If (Not (t) Is Nothing) Then
Return t
End If
Next
Return Nothing
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Private Sub DataList1_ItemCommand(ByVa
If e.Item.ItemType = ListItemType.Header Then
'Do something with header
End If
End Sub
Or,
Private Sub DataList1_ItemDataBound(By
If e.Item.ItemType = ListItemType.Header Then
'Do something with header
End If
End Sub