Event question

Discussion in 'ASP .Net' started by tshad, Oct 15, 2008.

  1. tshad

    tshad Guest

    I am just getting started with events and had a couple of questions on why
    they do what they do.

    If you have a textbox and you want to handle an event you can just do:

    this.TextBox2.TextChanged += new
    EventHandler(TextBox2_TextChanged);
    and then have the function.

    void TextBox2_TextChanged(object sender, EventArgs e)
    {
    Response.Write(this.TextBox2.Text);
    }

    Here the event is created and tied to the control and function.

    But if you do it in a user control, the function doesn't seem to be tied to
    anything.

    For example, in my .ascx page I could have this:
    public event EventHandler BubbleTextChanged;
    //protected void OnBubbleTextChanged(EventArgs e)
    //{
    // //if (BubbleTextChanged != null)
    // //BubbleTextChanged(this, e);
    //}

    Where I normally wouldn't comment out the function, so it isn't tied to
    anything (or doesn't seem to be). And the function seems to always need the
    word "on" in front of it.

    If I had 3 events (and no functions) they would show up in intellisense with
    "on" in front of them:

    public event EventHandler A1;

    public event EventHandler B1;

    public event EventHandler C1;

    public event EventHandler D1;

    public event EventHandler E1;

    If I only had the above and nothing more, in my aspx page my intellisense
    would show:

    onA1
    onB1
    onC1
    onD1
    onE1

    Yet in my first example, the event was TextBox2.TextChanged no
    TextBox2.onTextChanged or the event that was tied to that:
    TextBox2_TextChanged wasn't onTextBox2_TextChanged.

    Why is it different?

    Thanks,

    Tom
    tshad, Oct 15, 2008
    #1
    1. Advertising

  2. tshad

    bruce barker Guest

    events are pretty simple.

    an event is just a collection of delegates (functions to call).
    if you have an object that wants to raise events, that object defines a
    public event (any name you want). when the object wants to raise events it
    calls the delegates stored in its event object. the event will be null if no
    delegates have been added. by convention, the routine that raises the event
    is called OnEventName, and is code like:

    public EventHandler MyEvent;
    private void OnRaiseMyEvent(EventArgs e)
    {
    EventHandler handler = MyEvent;
    if (handler != null)
    handler(this, e);
    }

    some point in your code when you want to fire the event, it would call
    OnRaiseMyEvent.

    to subscribe to an event, your code addes a delegate (with the correct
    signature) to the event, via the syntax of the language. in c# its one of the
    following:

    myObject.MyEvent += new EventHandler(myFunction);
    myObject.MyEvent += delegate(object o, EventArgs e)
    {
    // code goes here
    }
    myObject.MyEvent += (o,e) =>
    {
    // code goes here
    }


    -- bruce (sqlwork.com)


    "tshad" wrote:

    > I am just getting started with events and had a couple of questions on why
    > they do what they do.
    >
    > If you have a textbox and you want to handle an event you can just do:
    >
    > this.TextBox2.TextChanged += new
    > EventHandler(TextBox2_TextChanged);
    > and then have the function.
    >
    > void TextBox2_TextChanged(object sender, EventArgs e)
    > {
    > Response.Write(this.TextBox2.Text);
    > }
    >
    > Here the event is created and tied to the control and function.
    >
    > But if you do it in a user control, the function doesn't seem to be tied to
    > anything.
    >
    > For example, in my .ascx page I could have this:
    > public event EventHandler BubbleTextChanged;
    > //protected void OnBubbleTextChanged(EventArgs e)
    > //{
    > // //if (BubbleTextChanged != null)
    > // //BubbleTextChanged(this, e);
    > //}
    >
    > Where I normally wouldn't comment out the function, so it isn't tied to
    > anything (or doesn't seem to be). And the function seems to always need the
    > word "on" in front of it.
    >
    > If I had 3 events (and no functions) they would show up in intellisense with
    > "on" in front of them:
    >
    > public event EventHandler A1;
    >
    > public event EventHandler B1;
    >
    > public event EventHandler C1;
    >
    > public event EventHandler D1;
    >
    > public event EventHandler E1;
    >
    > If I only had the above and nothing more, in my aspx page my intellisense
    > would show:
    >
    > onA1
    > onB1
    > onC1
    > onD1
    > onE1
    >
    > Yet in my first example, the event was TextBox2.TextChanged no
    > TextBox2.onTextChanged or the event that was tied to that:
    > TextBox2_TextChanged wasn't onTextBox2_TextChanged.
    >
    > Why is it different?
    >
    > Thanks,
    >
    > Tom
    >
    >
    >
    bruce barker, Oct 15, 2008
    #2
    1. Advertising

  3. tshad

    tshad Guest

    "bruce barker" <> wrote in message
    news:...
    > events are pretty simple.
    >
    > an event is just a collection of delegates (functions to call).
    > if you have an object that wants to raise events, that object defines a
    > public event (any name you want). when the object wants to raise events it
    > calls the delegates stored in its event object. the event will be null if
    > no
    > delegates have been added. by convention, the routine that raises the
    > event
    > is called OnEventName, and is code like:
    >
    > public EventHandler MyEvent;
    > private void OnRaiseMyEvent(EventArgs e)
    > {
    > EventHandler handler = MyEvent;
    > if (handler != null)
    > handler(this, e);
    > }
    >


    But in your example you use something different. How does MyEvent know it
    is tied to OnRaiseMyEvent? The only place I can see that is in the
    OnRaiseMyEvent function:

    EventHandler handler = MyEvent;

    Whe is that executed to connect the two?

    In my code:

    public event EventHandler A1;
    public event EventHandler B1;
    public event EventHandler C1;

    Even with out setting up the onA1, onB1 and onC1 - the system seemed to use
    those names as defaults since in my other page that used the control,
    intellisense was using them.

    In you example, you use OnRaiseMyEvent and not OnMyEvent. Does it matter?
    You said it was a convention.

    I am just trying to understand it here. I don't see anywhere where it says
    something like: MyEvent uses OnRaiseMyEvent.

    > some point in your code when you want to fire the event, it would call
    > OnRaiseMyEvent.


    In the case of a user control, I assume it would be on the User control on
    the aspx page (or another ascx page).
    >
    > to subscribe to an event, your code addes a delegate (with the correct
    > signature) to the event, via the syntax of the language. in c# its one of
    > the
    > following:
    >
    > myObject.MyEvent += new EventHandler(myFunction);
    > myObject.MyEvent += delegate(object o, EventArgs e)


    Do you use both of these? or just one?

    Thanks,

    Tom
    > {
    > // code goes here
    > }
    > myObject.MyEvent += (o,e) =>
    > {
    > // code goes here
    > }
    >
    >
    > -- bruce (sqlwork.com)
    >
    >
    > "tshad" wrote:
    >
    >> I am just getting started with events and had a couple of questions on
    >> why
    >> they do what they do.
    >>
    >> If you have a textbox and you want to handle an event you can just do:
    >>
    >> this.TextBox2.TextChanged += new
    >> EventHandler(TextBox2_TextChanged);
    >> and then have the function.
    >>
    >> void TextBox2_TextChanged(object sender, EventArgs e)
    >> {
    >> Response.Write(this.TextBox2.Text);
    >> }
    >>
    >> Here the event is created and tied to the control and function.
    >>
    >> But if you do it in a user control, the function doesn't seem to be tied
    >> to
    >> anything.
    >>
    >> For example, in my .ascx page I could have this:
    >> public event EventHandler BubbleTextChanged;
    >> //protected void OnBubbleTextChanged(EventArgs e)
    >> //{
    >> // //if (BubbleTextChanged != null)
    >> // //BubbleTextChanged(this, e);
    >> //}
    >>
    >> Where I normally wouldn't comment out the function, so it isn't tied to
    >> anything (or doesn't seem to be). And the function seems to always need
    >> the
    >> word "on" in front of it.
    >>
    >> If I had 3 events (and no functions) they would show up in intellisense
    >> with
    >> "on" in front of them:
    >>
    >> public event EventHandler A1;
    >>
    >> public event EventHandler B1;
    >>
    >> public event EventHandler C1;
    >>
    >> public event EventHandler D1;
    >>
    >> public event EventHandler E1;
    >>
    >> If I only had the above and nothing more, in my aspx page my intellisense
    >> would show:
    >>
    >> onA1
    >> onB1
    >> onC1
    >> onD1
    >> onE1
    >>
    >> Yet in my first example, the event was TextBox2.TextChanged no
    >> TextBox2.onTextChanged or the event that was tied to that:
    >> TextBox2_TextChanged wasn't onTextBox2_TextChanged.
    >>
    >> Why is it different?
    >>
    >> Thanks,
    >>
    >> Tom
    >>
    >>
    >>
    tshad, Oct 15, 2008
    #3
  4. tshad

    bruce barker Guest

    it doesn't. MyEvent is just a collection of delegates. if exposed as as
    public property, intellisence knows how to add code to create the
    delegate. it names a method OnEventName and uses:

    EventName += new EventHandler(OnEventName);


    this is all with subscribing to an event. this is just a naming
    convention that vs uses. i generally use a lambda function or create an
    anonymous delegate as I think this is often cleaner if the amount of
    code in the delegate is small (couple lines).

    at some point the code (often a control) must actually raise the event,
    so the delegate(s) will be called. this is when OnRaiseEvent is called.

    if you write asp.net controls, the control has interfaces you can
    implement, that will be called when it time to raise an event (like
    OnChange or OnClick).

    -- bruce (sqlwork.com)


    tshad wrote:
    > "bruce barker" <> wrote in message
    > news:...
    >> events are pretty simple.
    >>
    >> an event is just a collection of delegates (functions to call).
    >> if you have an object that wants to raise events, that object defines a
    >> public event (any name you want). when the object wants to raise events it
    >> calls the delegates stored in its event object. the event will be null if
    >> no
    >> delegates have been added. by convention, the routine that raises the
    >> event
    >> is called OnEventName, and is code like:
    >>
    >> public EventHandler MyEvent;
    >> private void OnRaiseMyEvent(EventArgs e)
    >> {
    >> EventHandler handler = MyEvent;
    >> if (handler != null)
    >> handler(this, e);
    >> }
    >>

    >
    > But in your example you use something different. How does MyEvent know it
    > is tied to OnRaiseMyEvent? The only place I can see that is in the
    > OnRaiseMyEvent function:
    >
    > EventHandler handler = MyEvent;
    >
    > Whe is that executed to connect the two?
    >
    > In my code:
    >
    > public event EventHandler A1;
    > public event EventHandler B1;
    > public event EventHandler C1;
    >
    > Even with out setting up the onA1, onB1 and onC1 - the system seemed to use
    > those names as defaults since in my other page that used the control,
    > intellisense was using them.
    >
    > In you example, you use OnRaiseMyEvent and not OnMyEvent. Does it matter?
    > You said it was a convention.
    >
    > I am just trying to understand it here. I don't see anywhere where it says
    > something like: MyEvent uses OnRaiseMyEvent.
    >
    >> some point in your code when you want to fire the event, it would call
    >> OnRaiseMyEvent.

    >
    > In the case of a user control, I assume it would be on the User control on
    > the aspx page (or another ascx page).
    >> to subscribe to an event, your code addes a delegate (with the correct
    >> signature) to the event, via the syntax of the language. in c# its one of
    >> the
    >> following:
    >>
    >> myObject.MyEvent += new EventHandler(myFunction);
    >> myObject.MyEvent += delegate(object o, EventArgs e)

    >
    > Do you use both of these? or just one?
    >
    > Thanks,
    >
    > Tom
    >> {
    >> // code goes here
    >> }
    >> myObject.MyEvent += (o,e) =>
    >> {
    >> // code goes here
    >> }
    >>
    >>
    >> -- bruce (sqlwork.com)
    >>
    >>
    >> "tshad" wrote:
    >>
    >>> I am just getting started with events and had a couple of questions on
    >>> why
    >>> they do what they do.
    >>>
    >>> If you have a textbox and you want to handle an event you can just do:
    >>>
    >>> this.TextBox2.TextChanged += new
    >>> EventHandler(TextBox2_TextChanged);
    >>> and then have the function.
    >>>
    >>> void TextBox2_TextChanged(object sender, EventArgs e)
    >>> {
    >>> Response.Write(this.TextBox2.Text);
    >>> }
    >>>
    >>> Here the event is created and tied to the control and function.
    >>>
    >>> But if you do it in a user control, the function doesn't seem to be tied
    >>> to
    >>> anything.
    >>>
    >>> For example, in my .ascx page I could have this:
    >>> public event EventHandler BubbleTextChanged;
    >>> //protected void OnBubbleTextChanged(EventArgs e)
    >>> //{
    >>> // //if (BubbleTextChanged != null)
    >>> // //BubbleTextChanged(this, e);
    >>> //}
    >>>
    >>> Where I normally wouldn't comment out the function, so it isn't tied to
    >>> anything (or doesn't seem to be). And the function seems to always need
    >>> the
    >>> word "on" in front of it.
    >>>
    >>> If I had 3 events (and no functions) they would show up in intellisense
    >>> with
    >>> "on" in front of them:
    >>>
    >>> public event EventHandler A1;
    >>>
    >>> public event EventHandler B1;
    >>>
    >>> public event EventHandler C1;
    >>>
    >>> public event EventHandler D1;
    >>>
    >>> public event EventHandler E1;
    >>>
    >>> If I only had the above and nothing more, in my aspx page my intellisense
    >>> would show:
    >>>
    >>> onA1
    >>> onB1
    >>> onC1
    >>> onD1
    >>> onE1
    >>>
    >>> Yet in my first example, the event was TextBox2.TextChanged no
    >>> TextBox2.onTextChanged or the event that was tied to that:
    >>> TextBox2_TextChanged wasn't onTextBox2_TextChanged.
    >>>
    >>> Why is it different?
    >>>
    >>> Thanks,
    >>>
    >>> Tom
    >>>
    >>>
    >>>

    >
    >
    bruce barker, Oct 17, 2008
    #4
  5. tshad

    tshad Guest

    But that was my question. I am not trying to be difficult here, just trying
    to understand what you HAVE to do versus what CAN you do.

    Convention, means that you don't have to do something - just that everyone
    agrees that this is the way we are going to do it. Required means that you
    have to do it this way.

    If I have a control to put on my page like so:
    **********************************************
    public partial class TextBox : System.Web.UI.UserControl
    {
    public string Text
    {
    get { return this.TextBox1.Text; }
    set { this.TextBox1.Text = value; }
    }
    public bool AutoPostBack
    {
    get { return TextBox1.AutoPostBack; }
    set { TextBox1.AutoPostBack = value; }
    }

    protected override void OnInit(EventArgs e)
    {
    this.TextBox1.TextChanged += new
    EventHandler(TextBox1_TextChanged);
    base.OnInit(e);
    }
    void TextBox1_TextChanged(object sender, EventArgs e)
    {
    OnBubbleTextChanged(e);
    }
    public event EventHandler BubbleTextChanged;
    protected void OnBubbleTextChanged(EventArgs e)
    {
    if (BubbleTextChanged != null)
    BubbleTextChanged(this, e);
    }
    }
    *************************************

    And on my .aspx page I have the control as:

    <uc1:TextBox ID="TextBox1" runat="server"
    OnBubbleTextChanged="TextChanged" AutoPostBack="true" />

    And the .aspx.cs page is:
    ******************************************
    namespace UserControlEvents
    {
    public partial class _Default : System.Web.UI.Page
    {
    protected void TextChanged(object sender, EventArgs e)
    {
    Response.Write("In .aspx page: " + this.TextBox1.Text);
    }
    }
    }
    *********************************************

    This works fine.

    But if I change the first set of code to:

    *********************************************
    void TextBox1_TextChanged(object sender, EventArgs e)
    {
    OnRaiseBubbleTextChanged(e);
    }
    public event EventHandler BubbleTextChanged;
    protected void OnRaiseBubbleTextChanged(EventArgs e)
    {
    if (BubbleTextChanged != null)
    BubbleTextChanged(this, e);
    }
    **********************************************

    Now there is NO OnBubbleChanged function. But the page STILL works as if it
    is. If you look at the text box on the .aspx page, intellisense still shows
    OnBubbleChanged and when I put a break on the first line of the
    OnRaiseBubbleTextChanged function it stops there ??????

    If I change to the TextBox to:

    <uc1:TextBox ID="TextBox1" runat="server"
    OnRaiseBubbleTextChanged="TextChanged" AutoPostBack="true" />

    It does a post back, but nothing happens.

    Why is that?

    It now goes to a non-existant method. It is forcing the "On" to precede the
    event name, which is fine. But I am trying to understand why and why it
    goes to the new function name.

    Thanks,

    Tom

    "bruce barker" <> wrote in message
    news:ebaOX1$...
    > it doesn't. MyEvent is just a collection of delegates. if exposed as as
    > public property, intellisence knows how to add code to create the
    > delegate. it names a method OnEventName and uses:
    >
    > EventName += new EventHandler(OnEventName);
    >
    >
    > this is all with subscribing to an event. this is just a naming convention
    > that vs uses. i generally use a lambda function or create an anonymous
    > delegate as I think this is often cleaner if the amount of code in the
    > delegate is small (couple lines).
    >
    > at some point the code (often a control) must actually raise the event, so
    > the delegate(s) will be called. this is when OnRaiseEvent is called.
    >
    > if you write asp.net controls, the control has interfaces you can
    > implement, that will be called when it time to raise an event (like
    > OnChange or OnClick).
    >
    > -- bruce (sqlwork.com)
    >
    >
    > tshad wrote:
    >> "bruce barker" <> wrote in message
    >> news:...
    >>> events are pretty simple.
    >>>
    >>> an event is just a collection of delegates (functions to call).
    >>> if you have an object that wants to raise events, that object defines a
    >>> public event (any name you want). when the object wants to raise events
    >>> it
    >>> calls the delegates stored in its event object. the event will be null
    >>> if no
    >>> delegates have been added. by convention, the routine that raises the
    >>> event
    >>> is called OnEventName, and is code like:
    >>>
    >>> public EventHandler MyEvent;
    >>> private void OnRaiseMyEvent(EventArgs e)
    >>> {
    >>> EventHandler handler = MyEvent;
    >>> if (handler != null)
    >>> handler(this, e);
    >>> }
    >>>

    >>
    >> But in your example you use something different. How does MyEvent know
    >> it is tied to OnRaiseMyEvent? The only place I can see that is in the
    >> OnRaiseMyEvent function:
    >>
    >> EventHandler handler = MyEvent;
    >>
    >> Whe is that executed to connect the two?
    >>
    >> In my code:
    >>
    >> public event EventHandler A1;
    >> public event EventHandler B1;
    >> public event EventHandler C1;
    >>
    >> Even with out setting up the onA1, onB1 and onC1 - the system seemed to
    >> use those names as defaults since in my other page that used the control,
    >> intellisense was using them.
    >>
    >> In you example, you use OnRaiseMyEvent and not OnMyEvent. Does it
    >> matter? You said it was a convention.
    >>
    >> I am just trying to understand it here. I don't see anywhere where it
    >> says something like: MyEvent uses OnRaiseMyEvent.
    >>
    >>> some point in your code when you want to fire the event, it would call
    >>> OnRaiseMyEvent.

    >>
    >> In the case of a user control, I assume it would be on the User control
    >> on the aspx page (or another ascx page).
    >>> to subscribe to an event, your code addes a delegate (with the correct
    >>> signature) to the event, via the syntax of the language. in c# its one
    >>> of the
    >>> following:
    >>>
    >>> myObject.MyEvent += new EventHandler(myFunction);
    >>> myObject.MyEvent += delegate(object o, EventArgs e)

    >>
    >> Do you use both of these? or just one?
    >>
    >> Thanks,
    >>
    >> Tom
    >>> {
    >>> // code goes here
    >>> }
    >>> myObject.MyEvent += (o,e) =>
    >>> {
    >>> // code goes here
    >>> }
    >>>
    >>>
    >>> -- bruce (sqlwork.com)
    >>>
    >>>
    >>> "tshad" wrote:
    >>>
    >>>> I am just getting started with events and had a couple of questions on
    >>>> why
    >>>> they do what they do.
    >>>>
    >>>> If you have a textbox and you want to handle an event you can just do:
    >>>>
    >>>> this.TextBox2.TextChanged += new
    >>>> EventHandler(TextBox2_TextChanged);
    >>>> and then have the function.
    >>>>
    >>>> void TextBox2_TextChanged(object sender, EventArgs e)
    >>>> {
    >>>> Response.Write(this.TextBox2.Text);
    >>>> }
    >>>>
    >>>> Here the event is created and tied to the control and function.
    >>>>
    >>>> But if you do it in a user control, the function doesn't seem to be
    >>>> tied to
    >>>> anything.
    >>>>
    >>>> For example, in my .ascx page I could have this:
    >>>> public event EventHandler BubbleTextChanged;
    >>>> //protected void OnBubbleTextChanged(EventArgs e)
    >>>> //{
    >>>> // //if (BubbleTextChanged != null)
    >>>> // //BubbleTextChanged(this, e);
    >>>> //}
    >>>>
    >>>> Where I normally wouldn't comment out the function, so it isn't tied to
    >>>> anything (or doesn't seem to be). And the function seems to always
    >>>> need the
    >>>> word "on" in front of it.
    >>>>
    >>>> If I had 3 events (and no functions) they would show up in intellisense
    >>>> with
    >>>> "on" in front of them:
    >>>>
    >>>> public event EventHandler A1;
    >>>>
    >>>> public event EventHandler B1;
    >>>>
    >>>> public event EventHandler C1;
    >>>>
    >>>> public event EventHandler D1;
    >>>>
    >>>> public event EventHandler E1;
    >>>>
    >>>> If I only had the above and nothing more, in my aspx page my
    >>>> intellisense
    >>>> would show:
    >>>>
    >>>> onA1
    >>>> onB1
    >>>> onC1
    >>>> onD1
    >>>> onE1
    >>>>
    >>>> Yet in my first example, the event was TextBox2.TextChanged no
    >>>> TextBox2.onTextChanged or the event that was tied to that:
    >>>> TextBox2_TextChanged wasn't onTextBox2_TextChanged.
    >>>>
    >>>> Why is it different?
    >>>>
    >>>> Thanks,
    >>>>
    >>>> Tom
    >>>>
    >>>>
    >>>>

    >>
    tshad, Oct 17, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Purvi T
    Replies:
    0
    Views:
    831
    Purvi T
    Oct 19, 2004
  2. Dave Wurtz

    Event On Form From Control Event

    Dave Wurtz, Dec 9, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    350
    Kevin Spencer
    Dec 9, 2003
  3. =?Utf-8?B?QWxpcmV6YSBaaWFp?=

    PreRender Event occurs immediatley after Load event

    =?Utf-8?B?QWxpcmV6YSBaaWFp?=, Feb 9, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,113
    Hans Kesting
    Feb 9, 2004
  4. jorge
    Replies:
    2
    Views:
    1,263
    jorge
    May 26, 2004
  5. Lars Netzel
    Replies:
    3
    Views:
    438
    Greg Burns
    Jul 22, 2004
Loading...

Share This Page