Exposing an event handler as a control property

P

paul.hester

Hi all,

I have a custom server control that can contain one or more buttons.
I'm trying to expose an event handler as a property so that the buttons
can fire a click event when clicked. However, I'm getting the following
error:

Cannot create an object of type 'System.EventHandler' from its string
representation 'OnSubmit' for the 'OnSubmit' property.

My control's tag looks like this:

<stuff:MyControl OnSubmit="OnSubmit" Width="430"
runat="server">...</stuff:MyControl>

The property looks like this:

public EventHandler OnSubmit
{
get { return _onSubmit; }
set { _onSubmit = value; }
}

I've tried the property with and without a type converter without
success.

Any help would be much appreciated.

Thanks,

Paul
 
T

Teemu Keiski

Hi,

you need to expose it as full-blown event, not as property. Page parser in
ASP.NET is then able to wire syntax On<EventName>="handler_method_name" into
your event as listener. Therefore the code would be pretty much like asa
follows. Read also the comments I've made into the code.

/// <summary>
/// Static key of all the event handlers
/// </summary>
private static readonly object EventClick = new object();

/// <summary>
/// This is the event where event handlers are collected into a
EventHandlerList
/// Page parser parses them from declarative syntax or you add them in
code
/// </summary>
public event EventHandler Submit
{
add
{
Events.AddHandler(EventClick, value);
}
remove
{
Events.RemoveHandler(EventClick, value);

}
}

/// <summary>
/// This method is used to raise Submit event. Do not *confuse* with
aspx's OnSubmit="method_name" syntax
/// </summary>
/// <param name="e"></param>
protected virtual void OnSubmit(EventArgs e)
{
EventHandler handler = Events[EventClick] as EventHandler;
if (handler != null)
handler(this, Error);
}

/// When handling Button's click inside the control, you'd call
this.OnSubmit(EventArgs.Empty);
/// when you want the event to be raised
 
T

Teemu Keiski

And line

handler(this, Error);

should be

handler(this, e);

Sorry for the typo.

Teemu Keiski said:
Hi,

you need to expose it as full-blown event, not as property. Page parser in
ASP.NET is then able to wire syntax On<EventName>="handler_method_name"
into your event as listener. Therefore the code would be pretty much like
asa follows. Read also the comments I've made into the code.

/// <summary>
/// Static key of all the event handlers
/// </summary>
private static readonly object EventClick = new object();

/// <summary>
/// This is the event where event handlers are collected into a
EventHandlerList
/// Page parser parses them from declarative syntax or you add them in
code
/// </summary>
public event EventHandler Submit
{
add
{
Events.AddHandler(EventClick, value);
}
remove
{
Events.RemoveHandler(EventClick, value);

}
}

/// <summary>
/// This method is used to raise Submit event. Do not *confuse* with
aspx's OnSubmit="method_name" syntax
/// </summary>
/// <param name="e"></param>
protected virtual void OnSubmit(EventArgs e)
{
EventHandler handler = Events[EventClick] as EventHandler;
if (handler != null)
handler(this, Error);
}

/// When handling Button's click inside the control, you'd call
this.OnSubmit(EventArgs.Empty);
/// when you want the event to be raised


--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke


Hi all,

I have a custom server control that can contain one or more buttons.
I'm trying to expose an event handler as a property so that the buttons
can fire a click event when clicked. However, I'm getting the following
error:

Cannot create an object of type 'System.EventHandler' from its string
representation 'OnSubmit' for the 'OnSubmit' property.

My control's tag looks like this:

<stuff:MyControl OnSubmit="OnSubmit" Width="430"
runat="server">...</stuff:MyControl>

The property looks like this:

public EventHandler OnSubmit
{
get { return _onSubmit; }
set { _onSubmit = value; }
}

I've tried the property with and without a type converter without
success.

Any help would be much appreciated.

Thanks,

Paul
 
S

studen771

Teemu Keiski said:
/// When handling Button's click inside the control, you'd call
this.OnSubmit(EventArgs.Empty);
/// when you want the event to be raised
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke


When you gave the instructions, "when handling Button's click inside the
control," how is this done? where in the control's class do you assign
register its click event?
 

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,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top