Theo Posted December 6, 2006 Posted December 6, 2006 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: 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 Quote
Administrators PlausiblyDamp Posted December 7, 2006 Administrators Posted December 7, 2006 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Theo Posted December 12, 2006 Author Posted December 12, 2006 (edited) 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: 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: 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 Edited December 12, 2006 by Theo Quote
Administrators PlausiblyDamp Posted December 12, 2006 Administrators Posted December 12, 2006 You will need to recreate the buttons each time the page is refreshed. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Theo Posted December 12, 2006 Author Posted December 12, 2006 You will need to recreate the buttons each time the page is refreshed. Could you give me an example of that? Quote
Administrators PlausiblyDamp Posted December 13, 2006 Administrators Posted December 13, 2006 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.