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.
LVL 3
JbondAsked:
Who is Participating?
 
JbondConnect With a Mentor Author 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)
        Try


            '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")
                pnl_ButtonArray.Controls.Add(lb_Pages(i))

                Dim lbl As New Label
                lbl.Text = "  "
                pnl_ButtonArray.Controls.Add(lbl)
            Next
        Catch ex As Exception

        End Try
    End Function

Open in new window

0
 
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

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
0
 
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.
0
 
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?
0
 
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
        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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.