473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Add Event Handler Dynamically, but assigning function name at run-time, not to static function

Hi all
I have a dynamically created button, I can add an event handler with:

AddHandler button.click, AddressOf static_function

This works great, but what I need is to create a function that takes
the control, the event and the function as parameters, something like:

function addevent(control as object, event as string, functionname as
string)
AddHandler control.event, addressof functionname
end function

A solution for this would be to add a single function that handles all
buttons and select the correct action thru commandarguments, that
would work, but it's really not what i'm looking for.
Thanks in advance,

Jose Suero
Nov 18 '05 #1
2 3704
Hi,

you cannot pass the functionname as such as AddressOf expects a delegate
type, not function name as string. You could try to use reflection as this
article explains:

http://www.123aspx.com/redir.aspx?res=31050

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
"Jose Suero" <ms@mstn.com> wrote in message
news:fd**************************@posting.google.c om...
Hi all
I have a dynamically created button, I can add an event handler with:

AddHandler button.click, AddressOf static_function

This works great, but what I need is to create a function that takes
the control, the event and the function as parameters, something like:

function addevent(control as object, event as string, functionname as
string)
AddHandler control.event, addressof functionname
end function

A solution for this would be to add a single function that handles all
buttons and select the correct action thru commandarguments, that
would work, but it's really not what i'm looking for.
Thanks in advance,

Jose Suero

Nov 18 '05 #2
Thanks a lot for your post, It took me on the right track, I had to
change tiny things, since I was getting an error but this works for
imagebuttons, binding an event for a dynamic created object from a
class library

Public Shared Function addevent(ByVal controlname As String, ByVal
type As String, ByVal eventname As String, ByVal procedure As String)
Dim control As System.Web.UI.WebControls.ImageButton
control = page.findcontrol(controlname)
Dim p As Object
Dim DLLName As String = "projectname"
Dim asm As [Assembly] = [Assembly].Load(DLLName)
Dim t As Type = asm.GetType(DLLName & ".WebForm1", True)
p = Activator.CreateInstance(t)
Dim mymethod As MethodInfo = t.GetMethod(procedure)
' get a reference to the EventInfo for this object
Dim pEv As EventInfo = control.GetType.GetEvent(eventname)
' create a delegate to the local procedure
Dim pDel As [Delegate] =
[Delegate].CreateDelegate(pEv.EventHandlerType, mymethod)
' make the local procedure a handler for the event
pEv.AddEventHandler(control, pDel)
End Function

"Teemu Keiski" <jo****@aspalliance.com> wrote in message news:<#$**************@TK2MSFTNGP11.phx.gbl>...
Hi,

you cannot pass the functionname as such as AddressOf expects a delegate
type, not function name as string. You could try to use reflection as this
article explains:

http://www.123aspx.com/redir.aspx?res=31050

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
"Jose Suero" <ms@mstn.com> wrote in message
news:fd**************************@posting.google.c om...
Hi all
I have a dynamically created button, I can add an event handler with:

AddHandler button.click, AddressOf static_function

This works great, but what I need is to create a function that takes
the control, the event and the function as parameters, something like:

function addevent(control as object, event as string, functionname as
string)
AddHandler control.event, addressof functionname
end function

A solution for this would be to add a single function that handles all
buttons and select the correct action thru commandarguments, that
would work, but it's really not what i'm looking for.
Thanks in advance,

Jose Suero

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
7
by: Rob | last post by:
I need a way to determine the name of the control that called an event. I have tested many different code variations but none seem to work. The one that makes the most sense to me is below, but it...
2
by: Donal McWeeney | last post by:
Hi, Just want to get confirmation on the following: I have a custom server control that I implements a postback - I dynamically in my custom control's OnLoad event register the event handler...
7
by: Girish | last post by:
OK.. phew. Playing with data grids for the past few days has been fun and a huge learning experience.. My problem. I have a requirement to display a gird with a gird. Within the embedded grid,...
2
by: Deepesh | last post by:
Good day, I have a specific case of the DataGrid in my solution which is causing the ItemCommand Event Not Firing. So I'm creating a "Skinnable" set of controls. I seperate the actual ASCX file...
4
by: Charles Law | last post by:
Is there a way to dynamically remove an event handler from an event without knowing the name of the handler? For example, how can ClassB remove the handler without knowing the name, or how many...
1
by: keithb | last post by:
A CreateUserWizard with one intermediate step (select role) works OK in the root folder, but fails to fire the Deactivate event handler for the intermediate step when the web page is moved to a...
1
by: Chris | last post by:
I want to create a generic event handler for some controls I am dynamically adding via a class. My class compiles OK but when I change the text in the textboxes it doesn't do anything. What am I...
5
by: =?Utf-8?B?TWFyYyBXb29sZnNvbg==?= | last post by:
Hi, I have a strange issue occurring with LinkButtons that are dynamically added to each (data) row of my DataGrid on that grid's ItemDataBound event. Each LinkButton is assigned its own event...
1
by: euroluxantiques | last post by:
Hi All, Using VB.Net with SQL Server. I have a form that I create a number of Picture Boxes on dynamically, based on a stored procedure that retrieves a listing of images available for a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.