Binding events to dynamic controls

  • Thread starter Thread starter Jeremy Ames
  • Start date Start date
J

Jeremy Ames

I am trying to bind a click event to a check box control
that I create during page load. I have tried using the
this.controlname += new System.EventHandler
(this.controlname_Click); but it tells me that my form
does not contain a definition for that control. I create
this control for infomation that I pull from a database
so there are multiple instances of this control, all with
a different id property. How can I tie an event handler
to these controls.
 
Jeremy,

You can use the GetEvent method on the Type to get the EventInfo that
represents the event. Once you have that, you can call the AddEventHandler
method on the EventInfo instance to add the event to the control.

Hope this helps.
 
I am not having much luck figuring this out. I am looking
at the MSDN library and I see an example but I am not
really understanding the example. Could you show me an
example?

-----Original Message-----
Jeremy,

You can use the GetEvent method on the Type to get the EventInfo that
represents the event. Once you have that, you can call the AddEventHandler
method on the EventInfo instance to add the event to the control.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I am trying to bind a click event to a check box control
that I create during page load. I have tried using the
this.controlname += new System.EventHandler
(this.controlname_Click); but it tells me that my form
does not contain a definition for that control. I create
this control for infomation that I pull from a database
so there are multiple instances of this control, all with
a different id property. How can I tie an event handler
to these controls.


.
 
Jeremy,

Say you have a button with the Click event stored in an object. You can
get the event like this:

// Get the EventInfo. The button is in the mobjControl variable and is of
type object.
EventInfo pobjClickEvent = mobjControl.GetType().GetEvent("Click");

Then you can add the event handler:

// Add the event handler.
pobjClickEvent.AddEventHandler(mobjControl, new
EventHandler(this.controlname_Click));


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jeremy Ames said:
I am not having much luck figuring this out. I am looking
at the MSDN library and I see an example but I am not
really understanding the example. Could you show me an
example?

-----Original Message-----
Jeremy,

You can use the GetEvent method on the Type to get the EventInfo that
represents the event. Once you have that, you can call the AddEventHandler
method on the EventInfo instance to add the event to the control.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I am trying to bind a click event to a check box control
that I create during page load. I have tried using the
this.controlname += new System.EventHandler
(this.controlname_Click); but it tells me that my form
does not contain a definition for that control. I create
this control for infomation that I pull from a database
so there are multiple instances of this control, all with
a different id property. How can I tie an event handler
to these controls.


.
 
Thanks for the example, I really appreciate it. However,
I am getting errors on the second line of that code. This
is what I have:

EventInfo oCheckEvent = chkComplete.GetType
().GetEvent("Click");
oCheckEvent.AddEventHandler(chkComplete, new
EventHandler( this.chkComplete_Click));

The error message that I am receiving is "Object
reference not set to an instance of an object." This does
not make sense to me. I would think that oCheckEvent
would have been instantiated in the first line. Do you
have any suggestions?
-----Original Message-----
Jeremy,

Say you have a button with the Click event stored in an object. You can
get the event like this:

// Get the EventInfo. The button is in the mobjControl variable and is of
type object.
EventInfo pobjClickEvent = mobjControl.GetType().GetEvent ("Click");

Then you can add the event handler:

// Add the event handler.
pobjClickEvent.AddEventHandler(mobjControl, new
EventHandler(this.controlname_Click));


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I am not having much luck figuring this out. I am looking
at the MSDN library and I see an example but I am not
really understanding the example. Could you show me an
example?

-----Original Message-----
Jeremy,

You can use the GetEvent method on the Type to get the EventInfo that
represents the event. Once you have that, you can
call
the AddEventHandler
method on the EventInfo instance to add the event to
the
control.
Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I am trying to bind a click event to a check box control
that I create during page load. I have tried using the
this.controlname += new System.EventHandler
(this.controlname_Click); but it tells me that my form
does not contain a definition for that control. I create
this control for infomation that I pull from a database
so there are multiple instances of this control, all with
a different id property. How can I tie an event handler
to these controls.


.


.
 
Back
Top