Understanding Event Handlers in ASP.NET 3.5
(Page 1 of 4 )
ASP.NET uses event handlers to control certain behaviors. They're responsible for programs acting in certain predictable ways. If you want to master ASP.NET programming, you need to learn more about event handlers. This article will introduce you to the basics.
Did you notice why the last web form you used acted the way it did when you clicked the submit button? Or did you notice that the time changes every time you reload an .aspx page? Or that it greets you with “Good morning” when the time is 6 AM or “Good afternoon” when the time is 2 PM?
These are happening because of the usage of “event handlers” in ASP.NET. “Event handlers” are called programmable “triggers” synonymous to “switch on” button of a fluorescent lamp. Once activated, it “fires” the event and causes it to execute something.
You wouldn’t notice it, but every time you see an ASP.NET page (with .aspx extension) event handlers are activated in the process. This article explores the ASP.NET event handlers and illustrates basic coding examples of their actual use and application.
ASP.NET “Click” event handler
Most ASP.NET applications utilize two important types of event handlers with different applications. These are the “page load” and “click” event handlers. The “page load” event handler is triggered every time the web page is requested and loaded by the server, while the “click” event handler is triggered every time a button is clicked.
Let's start with the more common “click” event handler. A typical working example is a web form with a submit button that utilizes the “click” event in such a way that when the submit button is clicked, ASP.NET executes a server side code associated with it. This code might be used to process form information, do form validation, submit form values to a database, and so forth.
One of the previously covered examples utilizing “click" event handlers is in this tutorial: http://www.aspfree.com/c/a/ASP.NET/Design-and-Create-Web-Forms-in-ASPNET/. It utilizes the web form below to ask for a user input and then engages a submit button (“Compute area of the circle”):
When the form is submitted, that is by clicking the “Compute area of the circle” button, the “click” event triggers the execution of the Visual Basic server side script to get user input (the circle's radius in this example) and then compute the area of the circle using the code below:
'Get the user input radius from the text box field
'Assigned it as userradius
Dim Userradius As Integer = radius.Text
'Calculate Area of the circle
'Initiate Area as a decimal
'Formula is 3.1416 x (r^2)
Dim Area As Decimal
Area = (3.1416) * ((Userradius) ^ 2)
'Output the area of the circle back to the browser
displayarea.Text = "The area of the circle is: " & Area
If the form is not submitted, the button is not clicked. Nothing will happen to the form unless it is triggered by a click event.
Next: ASP.NET page load event handler >>
More ASP.NET Articles
More By Codex-M