Link to home
Start Free TrialLog in
Avatar of Jbond
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.CommandName but the events for Header items appear to be handled differently.
Avatar of Alfred A.
Alfred A.
Flag of Australia image

You can still do this within a DataList.ItemCommand or even a DataList.ItemDataBound.  Something like this.

Private Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.ItemCommand
        If e.Item.ItemType = ListItemType.Header Then
               'Do something with header
        End If
    End Sub

Or,

    Private Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
        If e.Item.ItemType = ListItemType.Header Then
               'Do something with header
        End If
    End Sub
Regarding raising an event, you can use delegates to create a custom event linked to your linkbutton click event inside your template.
Avatar of Jbond
Jbond

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?
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.GetCellDisplayRectangle(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,"lnkbutton1"),linkbutton)

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
ASKER CERTIFIED SOLUTION
Avatar of Jbond
Jbond

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial