Dynamic event handlers and code

C

Chris

Is there any way you can dynamically add an event and then some code. I get
it that you could, in your code, add a control and dynamically add an event
handler for e.g. the click event. However the code within the event handler,
could that be pulled from a database and ran.

I would like to create a form in which I could populate it's controls and
events based on config information in either a database or xml file e.g. how
many textboxes amd dropdowns on the page. I know I can do this but what
would really make it useful would be to dynamically add the code within the
events. Is that possible? Regards, Chris.
 
M

Marina Levit [MVP]

You would have to pull the code from a database, compile it. Then
instantiate the object that code is defining using reflection , call a
method on it using reflection.

This is all very complicated, however, and does not perform well. I don't
know what problem you are trying to solve, but I would recommend trying to
revisit your requirements and seeing if there is another way to accomplish
what you want.
 
C

Chris

Yes I had a feeling that it wouldn't be straight forward hence the early
post. The idea would be to have a composite control which pulls in an XML
config file which would contain instructions on how to create a form e.g.
text boxs drop down. The idea being you could point the control at different
XML files and use it to dynamically generate your forms rather than hand
creating them all the time. It's just some of the controls may well need
events and event code which is different for each form. It's just an idea
I've been toying with, as we have a system which has LOTS of versions of a
similar form and I was looking at ways of generating the forms which are
less code intensive. Does anyone ever use self generating form? Regards,
Chris.
 
C

Chris

It looks interesting but my solution needs to be fully accessible to people
with disabilities. :)
 
M

Marina Levit [MVP]

Well, I think you can still do what you describe. You can parse an XML file,
and based on that, create the appropriate controls.

What you can do, is have all the methods you will ever need compiled into a
class. Then, using reflection, find the appropriate method you need (as
specified in the XML file), and attach that as the event handler. Or, have
your own generic event handler for everything, that based on the object that
raised the event, uses reflection to find the appropriate method, and invoke
it (I sort of like this one better).
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top