Developing Conditionally Driven Event Handlers in ASP.NET 3.5

What is a conditionally-driven event handler, and why would you want to use one? Basically, such an event handler will perform its assigned action -- such as displaying text on the screen -- if and only if a certain specified condition is met. As you might imagine, such event handlers have a range of uses. This article will walk you through some examples in ASP.NET 3.5.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
March 15, 2010
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

You should have gained a basic understanding and formulation of event handlers in ASP.NET from this tutorial. Now if you need to go more deeply into this aspect of developing ASP.NET 3.5 web applications, one of the most important tools you can use is "conditionally driven" event handlers.

Let's consider one of the examples given in a previous tutorial, namely the web application to compute the area of the circle. Suppose you want to alter the project and say the application can only compute the areas of circles with radii greater than 100 and rejects values below that. You will need to employ some kind of conditional server side scripting which will analyze user inputs. And then it will fire the event depending on the user input. This is an example of a conditionally-driven "click" event handler.

Other types of event handlers discussed are the "page load" type of handlers. This will fire the event at any time the page is requested to the server. To make conditionally-driven page load handlers, server side scripting will analyze user inputs are needed, in a way that is similar to what is done in click event processing.

For example, say you are going to make an application which will display "Good morning" in the browser if the server time is 6 AM or "Good evening" if the server time is 8 PM. Things like this need to have some conditional statements added to your ASP.NET server side scripts.

Choice of Programming Language

In this tutorial, as in other ASP.NET tutorials for beginners or intermediate developers using the .NET framework, the recommended language to use for server side scripting is Visual Basic. There are other options, such as Visual C#, but that language is not recommended for first time readers or beginning learners of ASP.NET.

Below is an important distinction between these two programming languages that are used in ASP.NET:

The conditional programming statements in this tutorial will be developed using Visual Basic.

Developing IQ Test Online Using ASP.NET 3.5

To aid the learning process, let's have an example. Say you would like to develop an ASP.NET 3.5 web application equivalent to the online PHP IQ test featured in another tutorial. To recap, it is a 10-item IQ quiz that will measure a person's intelligence in terms of IQ (Intelligence Quotient). The formula for computing IQ is:

IQ = (mental age / chronological age) * 100

First, the form should asked for the user's chronological age, that is, the age of the person. And then the user can proceed to answer the quiz's questions. Finally, once the answers are submitted to ASP.NET, the script will assign scores depending on the difficulty of the question, and then compute the mental age, which will then be used to compute the person's IQ.

Step 1: Creation of the Web Form

You will need to create a web form that will be very similar to the PHP version. By design, it should like the screen shot below:

The principles and skills needed to construct this web form along with its variables are discussed in this tutorial. Please refer to it for details. The only important thing that you need to note is the ID of those variables. In this example, you should let:

age= id representing the age

test1= id representing the answer of test1

test2= id representing the answer of test2

and so on...

submitbutton = id representing the form submit button.

iqresult = id representing the text label (for displaying the results).

Default.aspx = ASP.NET web page containing the web form

The text box web control is easy to create. You will only need to click and drag the web control elements from the Visual Web Developer toolbox to the Default.aspx source code.

In addition, the text label web control (for displaying the results to the browser) should be placed after the </form> tag since it is not part of the web form.

The final web form HTML, after you've completed the click-and-drag procedure, should look like this, with all fonts in Verdana:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

</head>

<body>

 

<h3 style="font-family: Verdana">

Simple Online IQ Test Using ASP.NET 3.5</h3>

 

<form id="form1" runat="server">

<div style="font-family: Verdana; font-size: small">

First, you need to enter your "<b>chronological age</b>", this your age today:

<asp:TextBox ID="age" runat="server"></asp:TextBox>

<br /><br />

Thanks for that, now answer the ten questions below as accurately as possible.

<br /><br />

Test question 1: What is the approximate shape of orange fruit, it starts with "r".

<asp:TextBox ID="test1" runat="server"></asp:TextBox>

<br /><br />

Test question 2: What is the product of 5 and 3?

<asp:TextBox ID="test2" runat="server"></asp:TextBox>

<br /><br />

Test question 3: If I have 3 apples and 12 mangoes, how many fruits do I have?

<asp:TextBox ID="test3" runat="server"></asp:TextBox>

<br /><br />

Test question 4: -5 + (+9)-(-8) is how much?

<asp:TextBox ID="test4" runat="server"></asp:TextBox>

<br /><br />

Test question 5: The result of 2 to the power of 6 divided by eight squared is how much?

<asp:TextBox ID="test5" runat="server"></asp:TextBox>

<br /><br />

Test question 6: If I have a right triangle whose two shorter sides are 5 and 12 respectively, how long is the longest side?

<asp:TextBox ID="test6" runat="server"></asp:TextBox>

<br /><br />

Test question 7: What is the name of a popular TV news network with two "Ns" comprised of 3 letters as initials?

<asp:TextBox ID="test7" runat="server"></asp:TextBox>

<br /><br />

Test question 8: In 2008, John is 2 times older than Jerry. In 2040, Jerry will be 46 years old. Find John's year of birth.

<asp:TextBox ID="test8" runat="server"></asp:TextBox>

<br /><br />

Test question 9: If you are using Excel, which function offers the most convenient and most popular approach for extracting data from other spreadsheets or workbooks without using the manual copy and paste method. This is a one word answer.

<asp:TextBox ID="test9" runat="server"></asp:TextBox>

<br /><br />

Test question 10: If a 65 year old couple retires today how much may they need today to cover their future health care cost? This answer is only a figure and does not include a dollar sign.

<asp:TextBox ID="test10" runat="server"></asp:TextBox>

<br /><br />

<asp:Button ID="submitbutton" runat="server" Text="Compute my IQ Now!"

style="font-family: Verdana; font-size: medium" />

</div>

</form>

<br /><br />

<br /><br />

<asp:Label ID="iqresult" runat="server" Text=""

style="font-family: Verdana; font-size: large; color: #0000FF"></asp:Label>

</body>

</html>

Step 2: Creating the Click Event Handler

Creation of click event handlers is thoroughly discussed in this tutorial

You will need to go into the Design view of Visual Web Developer and then double click "Compute my IQ now."

The editor will then open Default.aspx.vb. Because the ID of the button is "submitbutton," the click event handler will be shown like this:

Partial Class _Default

Inherits System.Web.UI.Page

Protected Sub submitbutton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submitbutton.Click

{insert the rest of the script here}

End Sub

End Class

Once you see the script above, you need to start writing your Visual Basic script which will accomplish the following objectives:

  1. Get user input values from the web form.

  2. Assign the input values to a Visual Basic Variable.

  3. Declare all variables used in the Visual Basic program.

  4. Analyze the user inputs and assign scores.

  5. Compute the total score, which is the mental age.

  6. Compute the person's Intelligence quotient.

  7. Return the results to the web browser.

You will learn the details of how to attain these objectives in the second part of this tutorial.

blog comments powered by Disqus
ASP.NET ARTICLES

- Implementing ASP.NET 4.0 Page.MetaDescriptio...
- ASP.Net Development Tips
- Intro to Sessions in ASP.Net
- Google Maps API Introduction in ASP.NET usin...
- Creating an ASP.NET 3.5 Gridview Image Galle...
- Encrypt QueryString in ASP.NET 3.5 using VB....
- ASP.NET 3.5 Drop Down List Controls
- Connect to Access Database with ASP.Net
- Secure Audio Streaming with ASP.Net and Flash
- Dynamic Sitemap and Navigation in ASP.Net
- Implement Gzip and Deflate Compression in AS...
- Run ASP.Net in Ubuntu with Apache
- ASP.Net Mono Website Contact Forms
- ASP.Net URL Rewriting Methods
- Murach`s ASP.NET 4 Web Programming with C# 2...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials