Clankiller.com

page graphics page graphics
Introduction


This tutorial will be going deeper into the logic available within C#. Logic is really what makes a programming language into a programming language. It's what lets the language make decisions based on various criteria. A scripting language like HTML lacks the ability to make any decisions based on various inputs (at least so far) and therefore a pure HTML page will always render the same for everyone. With C# you can customize your output based on any number of inputs.

The primary method of decision making is the if - then - else construct. There are then various extensions on this basic construct that make programming long or intricate statements easier. This tutorial builds off the examples used in the previous two tutorials. We'll be beginning with the following code:

0using System;
1
2public partial class _Default : System.Web.UI.Page
3{
4    protected void Page_Load(object sender, EventArgs e)
5    {
6        string contentString = null;
7        string name = Request.Form.Get("name");
8        string birthdate = Request.Form.Get("birthdate");
9        string occupation = Request.Form.Get("occupation");
10
11        try
12        {
13            if (name.Length > 0)
14            {
15                contentString += "Hello " + name + ", thanks for filling out the form.<br>";
16            }
17        }
18        catch { }
19        try
20        {
21            if (birthdate.Length > 0)
22            {
23                contentString += "I see you were born " + birthdate + ".<br>";
24            }
25        }
26        catch { }
27        try
28        {
29            if (occupation.Length > 0)
30            {
31                contentString += "How do you like being a " + occupation + "?<br>";
32            }
33        }
34        catch { }
35
36        contentString += @"<form method='POST' action='default.aspx'>
37        <table>
38        <tr>
39            <td>Name</td>
40            <td><input name='name' value='" + name + @"'></td>
41        </tr><tr>
42            <td>Birth Date</td>
43            <td><input name='birthdate' value='" + birthdate + @"'></td>
44        </tr><tr>
45            <td>Occupation</td>
46            <td><input name='occupation' value='" + occupation + @"'></td>
47        </tr><tr>
48            <td colspan=2><input type='submit' value='Submit'></td>
49        </tr>
50        </table>
51        </form>";
52        content.InnerHtml = contentString;
53    }
54}


So let us begin by diving right into the if / then / else statement.

Next ... If - Then - Else Index

If you have any comments, questions, suggestions, or feedback, please voice it on the Clankiller Forums.
page graphic page graphic
Copyright 2002-2008 Satis