Exposing an event handler as a control property

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Exposing an event handler as a control property

    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

    paul.hester@gmail.com Guest

  2. Similar Questions and Discussions

    1. exposing a style property
      I've built a custom control which basically includes a textbox and a validatorcontol as child controls. (Code included below) I've exposted...
    2. Exposing Font Name Property for Custom Control
      Hi, I've created a custom web control for using in my web application. I want to expose some Font-Name Properties of the control like i did for...
    3. open pop up window from asp server control event handler
      Check out this article, this will help you http://www.microsoft.com/india/msdn/articles/PopupCalendarinASP.aspx In this article, i used window.open...
    4. Event Handler not Working in Composite Control
      peter@workingmachines.com (Peter Chang) wrote in message news:<ce2e3f1c.0306201035.36a03bd5@posting.google.com>... Perhaps it is a bug in ASP.NET...
    5. How to Create a user control with a event property?
      I want to create a usercontrol and can set Click Event Function to it at desined-time,just as <myprefix:myLinkButton ... OnClick="ClickFunction"...
  3. #2

    Default Re: Exposing an event handler as a control property

    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
    [url]http://blogs.aspadvice.com/joteke[/url]


    <paul.hester@gmail.com> wrote in message
    news:1156742811.599520.168410@m73g2000cwd.googlegr oups.com...
    > 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 Guest

  4. #3

    Default Re: Exposing an event handler as a control property

    And line

    handler(this, Error);

    should be

    handler(this, e);

    Sorry for the typo.

    "Teemu Keiski" <joteke@aspalliance.com> wrote in message
    news:%23ahwS0ryGHA.3632@TK2MSFTNGP04.phx.gbl...
    > 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
    > [url]http://blogs.aspadvice.com/joteke[/url]
    >
    >
    > <paul.hester@gmail.com> wrote in message
    > news:1156742811.599520.168410@m73g2000cwd.googlegr oups.com...
    >> 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 Guest

  5. #4

    Default Re: Exposing an event handler as a control property

    "Teemu Keiski" wrote:
    > /// 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
    > [url]http://blogs.aspadvice.com/joteke[/url]

    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?
    studen771 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139