multiple link buttons for one sub question

G

Guest

I have about 20 link buttons that i need handled by one sub. I don't want to
have 20 onClick_xxx subs or onCommand subs.
I'm not having much luck calling a single sub for all the button or figuring
out which button caused the event?
I'm sure this is a simple one for most of you.
thanks
kes
 
P

Peter Rilling

What have you tried. Basically, the LinkButton would just call an event
handler, right? Could you not have all event hanlders point to the same
method?
 
G

Guest

Define your event handler to match the signature for System.EventHandler.
Specify that your sub will handle the click event for each of your buttons.
CType() sender to LinkButton and retrieve the ID or the Text (if you haven't
defined an ID for each button). Use if blocks or select case to perform the
processing specific to a particular button.

example (sorry if my VB syntax is a little off):
protected sub linkButton_Click(Sender as Object, e as EventArgs)
if Not (Sender is LinkButton)
return

Dim lb as LinkButton = CType(Sender, LinkButton)
if lb.ID = "Button1" Then
'Processing for Button1
elseif lb.ID = "Button2" Then
'Processing for Button2
end if

' Any other code
end sub

If your linkbuttons are defined at design time, you could also just compare
sender to each linkbutton:

if Sender.Equals(Button1) Then
' Processing for button 1
elseif Sender.Equals(Button2) Then
' Processing for button 2
end if

HTH
 
G

Guest

Thanks for responding,
i can get the on click to go to a single sub, but i can't get the name of
the linkbutton that caused the event.
example
<TD class="md"><asp:linkbutton id="cc4" OnClick="cc_onClick"
runat="server">04</asp:linkbutton></TD>
<TD class="md"><asp:linkbutton id="cc5" OnClick="cc_onClick"
runat="server">05</asp:linkbutton></TD>

will each call :
Sub cc_onClick(ByVal sender As Object, ByVal e As EventArgs) Handles
Me.lblDiag1.Text = "ok"
End Sub
But i'd like to get some information from the link button like ID, command
argues, or what ever...
thanks
kes

--
I figure if you can''t ask a question honestly, even if it suggests
blasphemy and disturbs the sensibilities of the grand masters of programming
you should quite and join a cult. kes


Peter Rilling said:
What have you tried. Basically, the LinkButton would just call an event
handler, right? Could you not have all event hanlders point to the same
method?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top