Not as easy as it seems: Webform Dynamic Buttons
page 1 of 2
Published: 21 Oct 2003
Unedited - Community Contributed
Solutions to two problems developing dynamic buttons for those switching to VB.NET from VB and/or C++: binding an event to a subroutine or function and generating buttons in a subroutine.
by Matthew Small
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 34834/ 44

The Newbie Problem, Part 1

This article was contributed by Matthew Small and he can be reached at

The Newbie Problem Part 1

OK, I am a newbie to Visual Basic.NET. However, I am an experienced programmer, and have used a few languages in my day, including VB 6.0 and C++. I therefore should be able to pick up the syntax of VB.NET pretty easily. Take this for example:

          text="Men’s Clothing" />

The above is the explicit placement of a linkbutton on the webform. Logically, it should be equivalent to this code below:

dim lbLink as new LinkButton
lbLink.Text = "Men’s Clothing"
lbLink.CommandName = "CategoryId"
lbLink.CommandArgument = "1"
lbLink.OnCommand = "lbLink_Click"

However, it is not. When run, this code will generate an error:

BC30390: 'System.Web.UI.WebControls.LinkButton.Protected Overridable Overloads Sub OnCommand(e As System.Web.UI.WebControls.CommandEventArgs)' is not accessible in this context because it is 'Protected'

Line 92:     lbLink.Text = "Men’s Clothing"
Line 93:     lbLink.CommandArgument = "1"
Line 94:     lbLink.OnCommand="lbLink_Click"
Line 95:
Line 96:

Ok, what does that mean? I really don’t know, but I do know that my code won’t work like that. The solution is simple but isn’t found in any of my ASP.NET books.

For reasons beyond my limited knowledge, Microsoft decided that instead of the logical dot-notation for adding a OnClick or OnCommand attribute to our Button, a separate statement was to be required: addhandler

dim lbLink as new LinkButton
lbLink.Text = "Men’s Clothing"
lbLink.CommandName = "CategoryId"
lbLink.CommandArgument = "1"
AddHandler lbLink.Command, AddressOf lbLink_Click

The addhandler statement binds an event to a subroutine or function of your choice. The generic syntax is:

AddHandler event, AddressOf subroutine

For more detailed information on it, go to

OK, that wasn’t so hard. We now have the correct syntax for binding an event to a subroutine or function.

View Entire Article

User Comments

Title: Thank you   
Name: Dimitris Kouzoudis
Date: 2010-10-18 4:30:48 AM
Great article, solved my problem! I was working on asp.table with dynamic link buttons and faced the same (both) problems with the buttons
Title: Good, but...   
Name: Helvin
Date: 2009-02-10 3:41:44 AM
Hey, thanks for pointing out this problem. I thought something was wrong with my code, and this is it. However, I am trying to display the results of a search, which would depend on the search string. So I cant just call my submit_search sub in page_load. (The results of the search include links, which calls another sub, so my situation is similar to yours.)
Title: Grand!   
Name: ElToro
Date: 2007-03-22 5:14:09 PM
Finally an article on Panel worth reading! THX!
Title: I get an error   
Name: Andrew
Date: 2007-01-31 7:16:32 AM
Testing the code, I get an error saying "'Command' is not an event of System.Object" for the codeline "AddHandler lbLink.Command, AddressOf lbLink_Click". Any clues to what's wrong? My test code is as below:

Dim i As Integer
For i = 0 To 20

Dim tr As New TableRow
Dim tc = New TableCell
Dim lbLink = New LinkButton
lbLink.Text = i
lbLink.CommandName = "CategoryId"
lbLink.CommandArgument = i
AddHandler lbLink.Command, AddressOf lbLink_Click
Next i
Title: thanks   
Name: Kristof Heyrman
Date: 2007-01-19 4:55:00 AM
Thanks man, Your article was very usefull to me.
I didn't know how to give an extra parameter (argument) to the button_click function, eg. the tableRow number in a dataset.
piece of cake with the Button.commandArgument option.

kind regards
Title: Grateful   
Name: Diane
Date: 2006-10-23 6:42:21 PM
Have been searching for days on how to do this... I'm too much of a newbie to understand most of the things I saw (which I suspect didn't actually tell me this anyway). This was hard, too, but you're CLEAR so I could follow. Thankyou Thankyou Thankyou.
Title: Thank you!   
Name: Phil Strachan
Date: 2006-09-16 11:30:09 PM
Thanks a bunch - part 1 was a life saver...late night, deadline looming - you know the drill. Thanks again!
Title: Thanks   
Name: Alex
Date: 2006-08-22 2:55:59 PM
Thanks so much man. I decided to just dive into .net by building my own wed control and I couldn’t find a simple solution to do a post back to the control since all the examples use c# which seems to accept the syntax of linkbutton.command. Seeing the line AddHandler lbLink.Command, AddressOf lbLink_Click here saved me hours of banging my head against the desk
Title: Some what confuse   
Name: Sudhir Kumar Jena
Date: 2006-04-03 1:49:31 AM
I am some what confuse with this artical.Can you please explain me clearly.
Sub ChooseCategory (Sender As Object, e as EventArgs)
pnlSelectCategory.visible = true
pnlSelectItem.visible = false
pnlViewItem.visible = false
pnlViewCart.visible = false
pnlCheckout.visible = false
pnlOrderPlaced.visible = false
End Sub

This code is bit confuse.How we get that choosegatagory.If possible pl send me complete code bcoz i want immediatly.
thanking You

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-05-28 6:18:23 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search