INT422 Internet III Web Programming on Windows - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

INT422 Internet III Web Programming on Windows

Description:

You can combine the two steps of creating the delegate and the event, into only one line as: ... data along with the event, we must create a custom EventArgs ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 27
Provided by: team61
Category:

less

Transcript and Presenter's Notes

Title: INT422 Internet III Web Programming on Windows


1
INT422 Internet III Web Programming on
Windows
  • Creating Events and Event Handlers for web
    user-controls

2
A buzz word...
  • Before we begin we need to define a buzz word
  • Assume there in a class called A and a class call
    B
  • If in any of the methods of A say foo(), an
    instance of B is created and used then A is the
    consumer of B
  • Class A type foo() B b new B()
    b.whatever()
  • Or better to say, if A uses B, then A is the
    consumer of B.

3
What is an event?
  • When in an object, there is a need to execute a
    logic at certain time, and the logic is
    undetermined or unavailable at the moment, an
    event is created and called instead.
  • This gives the consumer of that object (i.e. the
    code that uses that object) a chance to define
    and set the event to the appropriate logic
    depending on the scenario in which the object is
    used.

4
What is an event?
  • Events are always tied to a critical moment of
    the objects execution time and because of that,
    their name should always relate to the specific
    moment they are tied to.
  • Examples
  • Button_Click will be called when button is
    clicked
  • SendingEmail will be called right before the
    email is sent
  • SelectedIndexChenged will be called right after
    the selected index is changed
  • RowDeleting will be called right before the row
    is deleted
  • RowDeleted will be called right after the row is
    deleted

5
What is an event handler?
  • An Event Handler is a function (the logic)
    written in the consumers code to define an
    objects Event.
  • When the Event in the object is called, instead,
    the event handler in the consumer code will be
    called.

6
Where do I use an Event?
  • Events can be used in any object that is expected
    to behave differently depending their consumer.
  • In ASP.net one of the most common places to
    define an event is a web user-control.
  • Naturally the event handlers are written in the
    consumers of the web user-control that are either
    web-forms, or other web user-controls

7
Events and Event Handlers
  • To make a web user-control call an event handler
    in a web-form or in another user-control the
    following must be done
  • In the user-control An event must be created to
    be raised (called) at the proper moment.
  • In the container (either a web-form or another
    web user-control) an event handler must be
    created to handle the event

8
Creating the Event (in user-control)
  • In the user-control come up with a name that
    relates to the event, that is to be handled in
    the consumer of the user-control. (i.e.
    student_selected, deleting_employe, etc) We call
    it EventName here.
  • First create a delegate to establish the
    signature for your event handler in the web user
    control
  • public delegate void EventNameHandler
    (object sender, EventArgs e)
  • Sender is always set to the reference of the web
    user-control as the sender of the event. We
    will learn about the EventArgs in later slides.

9
Creating the Event (in user-control)
  • Create the event itself which presents to the
    consumer, what is the event name to be handled.
    Any call to EventName(sender, e) function in the
    user-control, will call the event handler in the
    consumerpublic event EventNameHandler
    EventName
  • this means Create EventName event-reference of
    type EventNameHandler delegate.

10
Creating the Event (in user-control)
  • There is always the possibility that the Event is
    chosen not to be handled in the consumer. So we
    must make sure the Event is called only if it is
    handled in the consumer.
  • Because of this we should create a function that
    calls the event, only if it is not null (i.e. it
    is handled in the consumer).
  • To do this, in the web user control, we should
    create the following function and call it instead
    of the Event, when the event is needed to be
    raised (called).
  • protected virtual void OnEventName(EventArgs e)
  • if(EventName ! null)
  • EventName(this, e)
  • Since the control may be inherited into
    something more complex, this function must be
    protected virtual to allow the children to
    override it.

11
Creating the Event (in user-control)
  • in the User Controls code beside call the event
    in the appropriate place
  • This place could be in an event handler of a
    web-control
  • private void SomeEventHandler
  • (object sender, EventArgs e)
  • If(EverythingIsOk ItIsTimeToRaiseTheEvent)
  • OnEventName(e)

12
Creating the Event (in user-control) V2.0
  • This completes the creation of an event in a
    web-user control.
  • Although these steps work in all versions of .NET
    framework, but in version 2.0 and later, they
    could be shorten as follows
  • You can combine the two steps of creating the
    delegate and the event, into only one line as
  • public event EventHandler EventName
  • The rest remains the same

13
Creating an Event HandlerMoving to the
User-Controls Consumer
  • To use this user control, drag and drop it in a
    consumer page/control from the solution explorer
  • (note that unlike Windows Forms Applications the
    user controls are not displayed in the tool box
    in web applications).
  • Lets assume the control is called MyControl and
    is dragged and dropped into a page and is renamed
    to MC.
  • To handle the event you just created in the
    consumer page, you must choose either the code
    beside or the xml source of the consumer page.

14
Creating an Event Handler In the Consumer Page
(Code Beside)
  • To handle this event in a web-form or another
    control you must first override the OnInit() of
    the consumer.
  • protected override void OnInit(EventArgs e)
    base.OnInit(e)
  • Since OnInit() is the very first stage of the
    life time of a web-form/control, it is the
    perfect place to initialize the events to their
    event handlers (this is also called to wire up
    the Event Handler to its Event)

15
(Good To Know)
  • The OnInit() event is raised before the page_load
    event handler. Here is the sequence of the
    important events when a page is getting ready to
    be sent to a browser
  • 1- Initialization (On Init)
  • 2- Page Load
  • 3- Validation (if there is any server side
    validations)
  • 4- Event Handlers (if there are controls in the
    page and their events are handled)
  • 5- Pre Render
  • 6- Render

16
Creating an Event Handler In the Consumer Page
(Code Beside)
  • Now we should create a function (event handler)
    called MC_EventName and wire it up to the the
    EventNameHandler of the user-control you just
    created. This can be done either using automatic
    code generation or manually.
  • ManuallyCreate a function with the exact
    signature of the EventNameHandler delegate in the
    user-control
  • protected void MC_EventName(object sender,
    EventArgs e)
  • // do what ever that supposed to be done when
  • // the event happens.

17
Creating an Event Handler In the Consumer Page
(Code Beside)
  • In the function OnInit() initialize the Event
    created in the user-control to the Event Handler
    function you just created. (This is also
    referred as wiring up the event handler function
    to the event created in the user-control.)
  • This initialization of the event of web user
    control can be done as follows
  • protected override void OnInit(EventArgs e)
  • base.OnInit(e)
  • MC.EventName new MyControl.EventNameHandler(
    MC_EventName)

18
Creating an Event Handler In the Consumer Page
(Code Beside)
  • To make Visual Studio code this automatically
  • Do not create the Event Handler function and In
    the OnInit() event handler type MC.EventName
    and then press the tab key twice. If you have
    named the delegate, event and OnInit functions
    correctly, the event handler and initialization
    will be written automatically.

19
Creating an Event Handler In the Consumer Page
(XML code)
  • Note that you must either do the event wire up in
    code beside or XML design code and NOT in both.
  • Create a function as with the exact signature of
    the EventNameHandler delegate in the user
    control
  • protected void MC_EventName(object sender,
    EventArgs e) // do what ever that supposed
    to be done when // the event happens.

20
Creating an Event Handler In the Consumer Page
(XML code)
  • No not overide the OnInit() in code beside but
    instead
  • In the xml tag of the web user-control add the
    name of the OnEventName as a property and set its
    value to the name of the Event Handler in code
    beside MC_EventName
  • ltuc1MyControl ID"MC runat"server"
    OnEventNameMC_EventName" /gt

21
Good To Know
  • An event can either ONLY represent a critical
    moment in the life cycle of the control or it can
    meanwhile send a package of information along
    with the event.
  • This package of information is called EventArgs.
    An EventArgs class by itself is an Empty package.
    To actually pass data along with the event, we
    must create a custom EventArgs class containing
    the data. Therefore this class must be a child of
    EventArgs.
  • If an event is left with simple EventArgs (like
    the delegate in the example) as an argument, it
    means no information is being passed along with
    the event, which was the case then.
  • See next slide to learn how to create custom
    EventArgs to pass information along with your
    event.

22
User Defined Event Arguments
  • If you would like to pass information through
    your event to the consumer, the first step of
    creating event should be creation of a class that
    is child of EventArgs
  • public class MyEventArg EventArgs public
    type somethingToCarry public MyEventArg(type
    val) somethingToCarry val
  • Note that you can design this class in any way
    you like as long as it is a child of EventArgs.
    What you see here is just a very simple example.

23
User Defined Event Arguments
  • Then in V1.1 when you are creating the delegate
    you must change the EventArgs to MyEventArgs
  • public delegate void EventNameHandler
    (object sender, MyEventArgs e)
  • In V2.0 and later when you are creating the Event
    you must add MyEventArgs to the signature of the
    template
  • public event EventHandlerltMyEventArgsgt
    EventName

24
User Defined Event Arguments
  • Naturally the OnEventName() function will change
    to
  • protected virtual void OnEventName
    (MyEventArgs e) if(EventName ! null)
    EventName(this, e)

25
User Defined Event Arguments
  • To actually send the information through the
    event, when calling the OnEventName function you
    must create a new instance of MyEventArgs class
    and pass it along
  • public void SomeEventHandler
    (object sender, EventArgs e)
  • type SomethingToCarry whateverYouWantToCar
    ry
  • OnEventName(new MyEventArg(SomethingToCarry)
    )

26
User Defined Event Arguments
  • So in your consumer code
  • private void MC_EventName(object sender,
    MyEventArgs e) TheValue e.somethingToCarry
    And then do whatever you want with TheValue
Write a Comment
User Comments (0)
About PowerShow.com