Coding an IQ Test with Conditionally Driven Event Handlers in ASP.NET 3.5
(Page 1 of 4 )
This is the second part of a tutorial series on developing conditionally-driven event handlers in ASP.NET 3.5. In part one, you started learning how to build an online IQ test with ASP.NET 3.5; the creation of web forms and setting of objectives were discussed. In this second part, we'll really sink our teeth into using conditionally-driven event handlers to make the test work.
Specifically, in this part you will learn how to create the Visual Basic program that will serve as a server side script to handle conditional decisions of the web application. A typical decision making task includes determining whether the user has the correct answer to the question and then assigning scores.
If you read part one and have fully created the web form (Default.aspx) in your own Visual Web Developer Express 2008, then let’s get started. All scripts mentioned in this article will be added to the Visual Basic file of Default.aspx in Visual Web Developer Express (Default.aspx.vb)
Variable Declaration and Grabbing of Form Inputs
Visual Basic is a “strong type” programming language. This means that you need to declare the type of all variables used in the script. In Visual Basic, the following are the important variable types and their application:
Also, the Visual Basic server side script cannot do anything without having form values to be processed. These act as inputs to the scripts, which will then be processed and output the IQ of the person to the web browser. In Visual Basic, to grab the user inputs from the web form which is submitted to the ASP.NET server, the following is the syntax:
Dim VisualBasicVariable As VisualBasicVariableType = FormIDused.Text
The bolded words are the ones that need to be changed depending on the variable name you used and the type you need to assign to each of those variables. FormIDused is the text box web control ID declared in Part 1, like test1, test2, etc.
To simplify the program, you will need to name those variables related to the form imputs as simply as possible. For example:
Dim Answer1 As Object = test1.Text
Dim Answer2 As Object = test2.Text
Dim Answer3 As Object = test3.Text
Dim Answer4 As Object = test4.Text
Dim Answer5 As Object = test5.Text
Dim Answer6 As Object = test6.Text
Dim Answer7 As Object = test7.Text
Dim Answer8 As Object = test8.Text
Dim Answer9 As Object = test9.Text
Dim Answer10 As Object = test10.Text
Dim Point1 As Integer
Dim Point2 As Integer
Dim Point3 As Integer
Dim Point4 As Integer
Dim Point5 As Integer
Dim Point6 As Integer
Dim Point7 As Integer
Dim Point8 As Integer
Dim Point9 As Integer
Dim Point10 As Integer
Dim chronologicalage As Integer = age.Text
Dim mentalage As Integer
Dim IQ As Decimal
The user's answers from the web form (variable Answer1 to Answer10) use the Object variable type because they are loosely defined (the answers can take the form of numbers, text, etc).
Dim Answer1 As Object = test1.Text
This above variable declaration line means “get the value of Textbox form ID name as test1 and then assign it to Answer1 as object.” So the answer for test question 1 using test1 form ID will be assigned as Answer1 in Visual Basic.
What about the score? The score assigned to Answer1 will simply be called Point1, and so on and so forth. The scoring system is as shown below:
Both correct and wrong answer points are integer values, therefore the variable type used in Visual Basic for those values is of the Integer type.
Chronological age and mental age also use the Integer type, since human age in years is commonly expressed as integers.
Lastly, the IQ can be expressed as a decimal, although during presentation of results back to the web browser, we can truncate or round off the values to be more user-friendly.
Next: Conditional Visual Basic Statements to Check Answers >>
More ASP.NET Articles
More By Codex-M