Home Page

Click here to download the sample source code for this if statement lab exercise  If you forgot how to open the files using Visual Web Developer, click here to learn How to open C# ASP.NET Website files.  Please use these files only as a reference.  You should go through the exercises yourself and create your own project according to the instructions in the exercise. 

C# programs contain different types of operators or symbols that perform operations on variables of various types.  Some operators can perform mathematical operations such as addition or subtraction, whereas other operators can take two string variables and combine them into one.  One operator that we have seen in use as we experimented with data types is the assignment operator =.  The assignment operator is the most basic and widely used operator in the C# language.  When used in a C# statement, the assignment operator takes whatever expression is on the right hand side of the = symbol, evaluates it, and then puts the resulting value into the variable on the left hand side of the =.  Let’s take a look at some more commonly used operators.

Create a new website using Visual Web Developer and call the project Lab1_ext.  Go to the Design view for the file Default.aspx.  Drag a new Button onto the form and change the Text property to say “Addition operator”.  Drag a new TextBox to the right of the Button control.  Double click the Button to create the event handler method and put the following code for the button:


int i = 30;
int j = 10;
//Addition operator
int result = i + j;
TextBox1.Text = result.ToString();

Drag another Button onto the form and change the Text property to say “Subtraction operator”.  Drag a new TextBox to the right of the Button control.  Double click the Button to create the event handler method and put the following code for the button:


int i = 30;
int j = 10;
//Subtraction operator
int result = i - j;
TextBox2.Text = result.ToString();

Drag another Button onto the form and change the Text property to say “Concatenation operator”.  Drag a new TextBox to the right of the Button control.  Double click the Button to create the event handler method and put the following code for the button:


string s1 = "Hello student,";
string s2 = " how are you?";
//string Concatenation operator
string result = s1 + s2;
TextBox3.Text = result;

Drag another Button onto the form and change the Text property to say “Increment operator”.  Drag a new TextBox to the right of the Button control.  Double click the Button to create the event handler method and put the following code for the button:


int i = 50;
//Increment operator
i++;
TextBox4.Text = i.ToString();

Drag another Button onto the form and change the Text property to say “Decrement operator”.  Drag a new TextBox to the right of the Button control.  Double click the Button to create the event handler method and put the following code for the button:


int i = 50;
//Decrement operator
i--;
TextBox5.Text = i.ToString();

Drag another Button onto the form and change the Text property to say “Addition assignment operator”.  Drag a new TextBox to the right of the Button control.  Double click the Button to create the event handler method and put the following code for the button:


int i = 50;
//Addition assignment operator
i += 5;
TextBox6.Text = i.ToString();

Drag another Button onto the form and change the Text property to say ”Concatenation assignment operator”.  Drag a new TextBox to the right of the Button control.  Double click the Button to create the event handler method and put the following code for the button:


string s = "Hi ";
//Concatenation assignment operator
s += " there!";
TextBox7.Text = s;

Run the project using the green play button at the top of the IDE.  Click on the various buttons and see their outputs on the web page.  Notice that some of the operators such as the + symbol act as different types of operators depending on the data types that are used in the expression.  If the + symbol determines that a string type is being used in the expression, the C# language will perform a string concatenation or combination of two strings together.  Whereas, when numeric types are being used, the C# language will perform mathematical addition operations.  You should also note that the different operators accept variables instead of hard coded values so the code example above for the addition assignment operator could have been alternatively written as:


int i = 50;
int j = 5;
//Addition assignment operator
i += j;
TextBox6.Text = i.ToString();

Notice that a variable j is used instead of the hard coded value of 5.  This is important to understand because frequently in your coding, you will encounter situations where you don’t know the values of variables ahead of time so you will need to perform these types of dynamic expression operations.  Spend some time looking over other types of C# operators as well on the MSDN website and experiment with those that you find interesting.

Click here to download the sample source code for this lesson

In order to effectively build web applications rich with user interactive server controls, you have to know how to properly solicit information from the application users.  The first step in achieving that is to develop a screen design with the appropriate server controls for your business application.  Each type of server control has its purpose and I describe some common uses below to help you determine which control to use for various situations.

To solicit text from the user that should be typed (e.g. a registration form Last Name field) use the TextBox control.  If you want the user to be able to enter multiple lines of input such as a Notes field, there is a property of the TextBox called TextMode that you can set to MultiLine.

If you want the user to choose an item from a simple list you have a couple of options: the DropDownList and the ListBox.  The DropDownList is better in situations where you don’t want to show all of the available options on the screen at one time; the user has to click on the control to see all of the options.  The ListBox however is sizeable and shows multiple options on the screen without requiring the user to click; if the options are more than the size of the ListBox, a scroll bar will appear.  Also the ListBox allows for multiple concurrent selections so that user can choose more than one item at a time.

If you want the user to be able choose one option versus another use the RadioButton control.  A similar server control for options is the CheckBox control.  Both of these controls have a property called Checked which you can reference in the source code.  We will experiment with both of these controls and the Checked property in this lesson.

The RadioButton and CheckBox controls also are available as lists: RadioButtonList and CheckBoxList.  These are practical in situations when you want to display several options. 

If you want the user to execute a server side action such as saving data that was typed in a form, performing a search, or navigating to another page, you should use the Button control.  As far as buttons go, you have a few different choices: the standard Button, the LinkButton and the ImageButton.  The only difference between these is how they look.  All of them have the Click event and work just like a regular ASP.NET Button.  The LinkButton looks like a traditional Internet hyperlink.  The ImageButton is a bit more fancy in that you can reference an image file that you want to be clickable.  By clicking on the image, the user can trigger the Click event.

Create a new web form called Default5.aspx and go to the Design view.  Now let’s build our first real user input form.  We are going to build a job search registration form, one that may look similar to what you would see if you were registering on a job search website.  Click here to watch an example video for what we are going to do in the next several steps of this lesson.  *You will notice that as I drag server controls onto the web form, when I want to place a control on the next line, I click to the right of the control and then press the ENTER key.

Drag a Label onto the form.  Change the Text property to “Job Registration”.  Change the Font properties to Bold and the size Large.  Add two new lines.

Drag a Label onto the form.  Change the Text property to “First Name”.  Drag a TextBox next to the Label and change the ID to txtFirstName.  Add a new line.

Drag a Label onto the form.  Change the Text property to “Last Name”.  Drag a TextBox next to the Label and change the ID to txtLastName.  Add a new line.

Drag a Label onto the form.  Change the Text property to “State”.  Drag a DropDownList next to the Label and change the ID to ddlState.  Add three items into the control: California, Texas and Virginia.  To add items put your cursor over the control and a little button with > symbol appears.  Click that button and then click Edit Items…  Add a new line.

Drag a Label onto the form.  Change the Text property to “Industry”.  Drag a ListBox next to the Label and change the ID to lstIndustry.  Add three items into the control: Healthcare, Information Technology and Financial.  Adding items into the ListBox is similar to adding items into the DropDownList as you did above.  Add a new line.

Drag a Label onto the form.  Change the Text property to “Availability”.  Drag a RadioButton next to the Label and change the ID to rbnImmediately.  Change the GroupName property to “Availability” and the Text property to “Immediately”.  Drag another RadioButton and change the ID to rbnTwoweeks.  Change the GroupName property to “Availability” and the Text property to “Two Weeks Notice”.  Add a new line.  By making the GroupName the same for both of these RadioButtons, you ensure that the user can only click on one of them at a time.

Drag a CheckBox onto the form.  Change the ID to chkOver18.  Change the Text property to “Click here to certify that you are over 18″.  Add a couple of new lines.

Drag a Button onto the form.  Change the ID to btnRegister.  Change the Text property to “Register”.  Add a couple of new lines.

Drag a Label onto the form.  Change the Text property to “Output”.  Drag a TextBox next to the Label and change the ID to txtOutput.  Change the TextMode property to MultiLine.

When you are finished adding all the controls and editing their properties, your Default5.aspx form should look like this.
ASP.NET Job registration form

Double click on the btnRegister button to create the even handler method btnRegister_Click and paste the following code:


string NEWLINE = "\r\n";
string FirstName = txtFirstName.Text;
string LastName = txtLastName.Text;
string State = ddlState.SelectedValue;
string Industry = lstIndustry.SelectedValue;

txtOutput.Text += FirstName;
txtOutput.Text += " ";
txtOutput.Text += LastName;
txtOutput.Text += NEWLINE;

txtOutput.Text += State;
txtOutput.Text += NEWLINE;

txtOutput.Text += Industry;
txtOutput.Text += NEWLINE;

if (rbnImmediately.Checked == true)
    txtOutput.Text += "Immediately";
if (rbnTwoweeks.Checked == true)
    txtOutput.Text += "In Two Weeks";
txtOutput.Text += NEWLINE;

if (chkOver18.Checked == true)
    txtOutput.Text += "User is over 18";

At the top of the code, I declared some variables to hold the values of the user input controls.  For TextBox controls you can find out what the user typed into the control by using the Text property (e.g. txtFirstName.Text).  For the list controls you can use the SelectedValue property (e.g. ddlState.SelectedValue). 

Following the variables declarations, I display all the values for the different controls in the TextBox txtOutput by using the concatenation operator += which will append text into the control.  Notice that I use the Text property of the txtOutput control to display the values.  Finally for the RadioButton and CheckBox controls, I examine the Checked property to determine if the user selected the control by clicking on it.

Next Page »