Same event handler for multiple link buttons

  • Thread starter Thread starter akki
  • Start date Start date
A

akki

I create link buttons dynamically in the code and give them IDs through
which I can distinguish them.I want to know how to create a single
event handler for all the link buttons that I create The event handler
should also be able to identify which link button was clicked....(I am
using VB as the code behind language). Please reply....
 
Hi Akshay,

When you create the controls, add an event handler that points to the common
routine. Here's a little demo. This is for ASP.NET 2.0.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>
<script runat="server">
Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim lnkbutton1 As New LinkButton
lnkbutton1.ID = "LinkButton1"
lnkbutton1.Text = "LinkButton1"
Dim lnkbutton2 As New LinkButton
lnkbutton2.ID = "LinkButton2"
lnkbutton2.Text = "LinkButton2"
Dim lit As New Literal
lit.Text = "<br />"
Dim evnthandler As New _
EventHandler(AddressOf Me.LinkButton_Handler)
AddHandler lnkbutton1.Click, evnthandler
AddHandler lnkbutton2.Click, evnthandler
PlaceHolder1.Controls.Add(lnkbutton1)
PlaceHolder1.Controls.Add(lit)
PlaceHolder1.Controls.Add(lnkbutton2)
End Sub

Protected Sub LinkButton_Handler _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim lnkbtn As LinkButton
lnkbtn = sender
Response.Write(lnkbtn.ID)
End Sub


</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>LinkButton_Handler</title>
</head>
<body>
<form id="form1" runat="server">
<asp:placeholder id="PlaceHolder1" runat="server"></asp:placeholder>
</form>
</body>
</html>
 

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

Back
Top