Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Building Controls / September 2006

Tip: Looking for answers? Try searching our database.

Exposing an event handler as a control property

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
paul.hester@gmail.com - 28 Aug 2006 06:26 GMT
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
Teemu Keiski - 28 Aug 2006 17:14 GMT
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

Signature

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

> Hi all,
>
[quoted text clipped - 27 lines]
>
> Paul
Teemu Keiski - 28 Aug 2006 17:17 GMT
And line

handler(this, Error);

should be

handler(this, e);

Sorry for the typo.

> Hi,
>
[quoted text clipped - 74 lines]
>>
>> Paul
studen771 - 30 Sep 2006 03:03 GMT
>     /// 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?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.