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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alfred A.Commented:
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


    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
Alfred A.Commented:
Regarding raising an event, you can use delegates to create a custom event linked to your linkbutton click event inside your template.
JbondAuthor Commented:
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?
Alfred A.Commented:
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
    End Sub


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
        Return Nothing
    End Function
JbondAuthor Commented:
My Question may not have be completely clear. I've created a search page, it uses a paged datasource, across the top of the datalist there is a list of dynamically created linkbuttons used to navigate through the paged datasource.

Use CreateLIstButtonArray to create the linkbuttons, the
                AddHandler lb_Pages(i).Click, AddressOf ButtonArray_ClickHandler
Tells the Dynamic Button thats being created what to use for a handler.

In the ButtonArray_ClickHandler you access any portion of the linkbutton control. i.e. Name, ID, CommandName, etc.

Protected Function CreateListButtonArray(ByVal iCount As Integer)

            'Total Number of records devided by 10, gives the total number of buttons in the Array
            Dim iTenBaseCount As Integer = iCount / 10

            'Declare the linkbutton array
            Dim lb_Pages As LinkButton() = New LinkButton(iTenBaseCount) {}

            'loop thru the array setting applicable properties, subtract two from the length of array.
            For i As Integer = 0 To lb_Pages.Length - 2
                'Add 1 to the count so you don't have a linkbutton labeled 0
                Dim iCountPlusOne As Integer = i + 1

                'make a new button
                lb_Pages(i) = New LinkButton
                'Set the ID equal to the lb_page + count, this creates a unique ID for each button
                lb_Pages(i).ID = "lb_Page" + iCountPlusOne.ToString
                'Set the text of the botton so it represents the page number
                lb_Pages(i).Text = iCountPlusOne.ToString

                AddHandler lb_Pages(i).Click, AddressOf ButtonArray_ClickHandler

                Dim pnl_ButtonArray As Panel = dl_SearchResults.Controls(0).FindControl("pnl_ButtonArray")

                Dim lbl As New Label
                lbl.Text = "  "
        Catch ex As Exception

        End Try
    End Function

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.