Use click event from random generated button

Theo

Newcomer
Joined
Aug 11, 2006
Messages
14
Location
Netherlands
Hi all,

I've got a procedure that generates some buttons. The information needed for the buttons comes out of a database.

The buttons are generated fine, but how can I use the click event from the buttons?

This is what I have so far:
Code:
    Private Sub GetButtons()
        Dim cmd As New SqlClient.SqlCommand
        Dim da As New SqlClient.SqlDataAdapter
        Dim ds As New DataSet
        Dim RowCnt As Long

        cmd.Connection = myPublics.conn
        cmd.CommandText = "SELECT [id], text FROM folders WHERE parent IS NULL"

        ds.Clear()
        If ds.Tables.Count > 0 Then ds.Tables(0).Columns.Clear()

        da.SelectCommand = cmd
        da.Fill(ds)

        RowCnt = ds.Tables(0).Rows.Count

        If RowCnt > 0 Then
            Dim i As Long

            For i = 1 To RowCnt
                Dim btn As New Button

                btn.ID = ds.Tables(0).Rows(i - 1)(0).ToString
                btn.Text = ds.Tables(0).Rows(i - 1)(1).ToString
                btn.Width = Unit.Pixel(175)
                btn.Font.Bold = False

                pRootControls.Controls.Add(btn)

                btn = Nothing
            Next
        End If

        cmd = Nothing
        da = Nothing
        ds = Nothing
    End Sub
 
You can use the AddHandler command to attach an event handler at runtime, if you search these forums you should find a few examples of how AddHandler works.
 
Thanks PlausiblyDamp. I'm now using the AddHandler command but it still doesn't work the way I want. Let me explain what I want to build. I've got a database with a table named ITEMS. The table lookes like this:

Code:
    ID          TEXT            TYPE            PARENT          LINK
    100000      Button1         folder          <NULL>          <NULL>
    100001      Button2         folder          <NULL>          <NULL>
    100002      Button11        folder          100000          <NULL>
    100003      Button12        folder          100000          <NULL>
    100004      Document111     item            100002          document111.pdf
    100005      Document112     item            100002          document112.pdf
    100006      Document113     item            100002          document113.pdf

On the left side of my webpage I want to make a menu out of buttons. These buttons should be generated in runtime from the data out of the table above. When I load the page the buttons with no parent are shown correctly, but when I click one of those buttons all buttons disapear.

What am I doing wrong? I've put a breakpoint at the procedure ButtonClick and discovered that the procedure isn't even triggered. Does this have something to do with the postback property?

This is my code:

Visual Basic:
    Private Sub GetControls(ByVal pParent As String)
        Dim cmd As New SqlClient.SqlCommand
        Dim da As New SqlClient.SqlDataAdapter
        Dim ds As New DataSet
        Dim RowCnt As Long
        Dim i As Long

        cmd.Connection = conn

        If UCase(pParent) = "NULL" Then
            cmd.CommandText = "SELECT [id], [text] FROM items WHERE parent IS NULL ORDER BY [id]"
        Else
            cmd.CommandText = "SELECT [id], [text] FROM items WHERE parent = '" & pParent & "' ORDER BY [id]"
        End If

        ds.Clear()
        If ds.Tables.Count > 0 Then ds.Tables(0).Columns.Clear()

        da.SelectCommand = cmd
        da.Fill(ds)

        RowCnt = ds.Tables(0).Rows.Count

        pControls.Controls.Clear()

        For i = 1 To RowCnt
            Dim btn As New Button

            btn.ID = ds.Tables(0).Rows(i - 1)(0).ToString
            btn.Text = ds.Tables(0).Rows(i - 1)(1).ToString
            btn.Width = Unit.Pixel(179)

            AddHandler btn.Click, AddressOf ButtonClick

            pControls.Controls.Add(btn)

            btn = Nothing
        Next

        If UCase(pParent) <> "NULL" Then
            Dim btn As New Button

            btn.ID = pParent
            btn.Text = "Previous"
            btn.Width = Unit.Pixel(179)

            AddHandler btn.Click, AddressOf ButtonClick

            pControls.Controls.Add(btn)

            btn = Nothing
        End If

        cmd = Nothing
        da = Nothing
        ds = Nothing
    End Sub

    Private Sub ButtonClick(ByVal sender As Object, ByVal e As System.EventArgs)
        GetControls(sender.id)
    End Sub

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then
            SetConnection()
            GetControls("NULL")
        End If
    End Sub
 
Last edited:
You would basically need to use the same code to recreate the buttons on each page refresh including postbacks, try moving the GetControls("NULL") call to after the check for IsPostback.
 
Back
Top