ASP.NET Events for Beginners (Free!)
As you are aware in real life an event is when something notable occurs, such an earthquake or somebody walking on the moon, even you getting out of bed this morning could be considered an event! Within an ASP.NET page events are just the same; they mark something notable happening during the page's execution lifecycle and let you know about it when it happens. A prime example of this is Page_Load, this is raised early in the pages lifecycle and you can place your own code into the Page_Load event procedure.
Overview
We'll cover the following:
- What is an event? - Briefly explains what an event is in the context of an ASP.NET page
-
So what’s cool about events? - Explains why events are cool and demonstrates using a simple example.
-
What order do events get executed? - Gives a breakdown of the order events are processed in a typical ASP.NET page.
-
Dissect and explain an event procedure. - Takes a closer look at some event procedure methods and explains what each component of the procedure does or cane be used for.
-
Some simple examples - demonstrate with some simple examples how to best utilize events in your page using Dreamweaver.
ASP.NET events and why they are cool.
Events are cool, events are brilliant and events helped shape the world…
as you can tell I like events, hopefully after reading this tutorial you will
too. I am going to dispense with the usual technical jargon normally associated
with tutorials of this nature and use a new language known as "kev speak".
This language is so simple to understand that even beginners will be able
to follow what's going on! Heck even I understand it.
Overview of this tutorial.
This tutorial will hopefully make you as passionate about the events in an
ASP.NET as I am. We'll cover the following:
- What is an event?
Briefly explains what an event is in the context of an ASP.NET page
- So what's cool about events?
Explains why events are cool and demonstrates using a simple example.
- What order do events get executed?
Gives a breakdown of the order events are processed in a typical ASP.NET
page.
- Dissect and explain an event procedure
Takes a closer look at some event procedure methods and explains what each
component of the procedure does or cane be used for.
- Some simple examples
demonstrate with some simple examples how to best utilize events in your
page using Dreamweaver.
What is an event?
As you are aware, in real life an event is when something notable occurs,
such an earthquake or somebody walking on the moon. Even you getting out of
bed this morning could be considered an event! Within an ASP.NET page events
are just the same; they mark something notable happening during the page's
execution lifecycle and let you know about it when it happens. A prime example
of this is Page_Load, this is raised early in the pages lifecycle and you
can place your own code into the Page_Load event procedure.
So what's cool about events?
Everything…
Traditionally web pages with server side code are executed in a linear flow.
What this means is that each line is evaluated and executed from the top to
the bottom, line 1 of the code is executed before line 3 and line 3 is executed
before line 25. When you need to control whether certain lines of code should
be executed you're required to perform some kind of conditional check within
the code such as using if and else blocks or select case statements.
A prime example of this is a page with multiple buttons for submitting a
form and the page needs to know what button was pressed. Using "classic"
ASP(and many other server side programming languages), it's necessary to check
the request form values to determine which button was clicked to submit the
form and then use conditional checks within the code to execute the section
of code specific to that button.
ASP.NET does things a little differently, and the previously mentioned buttons
example can be achieved using ASP.NET with no conditional checks required;
you can attach an event procedure to each button that only contains the code
you want executed for that button.
Here is a simple example of a button click event procedure:
C#
<script runat="server">
void Button1_Click(object sender, System.EventArgs
e)
{
Response.Write("You clicked
the button!");
}
</script>
VB
<script runat="server">
Sub Button1_Click(sender As Object, e
As System.EventArgs)
Response.Write("You clicked
the button!")
End Sub
</script>
Button control tag:
<asp:button OnClick="Button1_Click"
ID="Button1" runat="server" Text="OK" />
We will dissect and explain each part of this code a little later in the
tutorial. Hopefully you can by now see how cool events are, if not - keep
reading and you will!
I live in Perth, Scotland and am senior programmer with WebXeL.com Ltd. I have been involved in computer programming since 1990, web based programming using ASP since 1997 and have been building ASP.NET applications since ASP.NET Beta 2 was released.
I have been an avid Dreamweaver user since Version 1 was launched and with the release of MX have moved into developing extensions. WebXeL run a website dedicated to the development of free and commercial Dreamweaver MX extensions for ASP.NET, the site can be found at http://www.webxel-dw.co.uk.
See All Postings From Kevin Marshall >>