To Lecture Notes
IT 230 -- 4/22/09
Review Questions
- What is wrong with this tag if you are placing all of your C# code
in the .aspx file?
<%@ Page Language=C# AutoEventWireup=true
CodeBehind=Default.aspx.cs
Inherits=WebApplication1._Default %>
Ans: This is a tag for use only with code-behind files.
Use this tag instead:
<%@ Page Language=C# %>
- How do you gray out a control?
Ans: Set its Enabled property to true.
- What is an overloaded method?
Ans: A method that has the same name as another method, but with
a different signature, that is different parameter classs that
are passed in.
- Identify the overloaded methods in the
DateTime and
Random
UML diagrams.
Ans: In DateTime, the three constructors have the signatures
( ), (int, int, int), and (int, int, int, int, int, int).
- What is a default event?
Ans: The default event is the event for which the event handler
header is produced in Visual Studio when one double clicks on the
control.
- What are the default events for these ASP.Net controls?
Button CheckBox CheckBoxList DropDownList
TextBox
Ans: Click, TextChanged, CheckedChanged, SelectedItemChanged,
SelectedItemChanged.
- Create a Visual Studio Project that tests the default event
for a TextBox control.
Ans: Application
Source Code
Posting Pages to the Server
The get method appends data to the end of a URL when posting data.
Here is an example URL with appended data
http://ectweb2.cs.depaul.edu/ssmith/
destination.aspx?txtName=Stan&txtAge=45
Example 30 uses method=get to post data to the verify1.aspx page.
Here is the form tag for posting data with method=post:
<form action=destination.aspx method=post>
The post method submits data in the HTTP header, so there is no
appended data to the URL:
http://ectweb2.cs.depaul.edu/ssmith/destination.aspx
Example 31 uses method=post to post data to the verify1.aspx page.
File IO
Never use an http URL when opening a StreamReader or StreamWriter
object.
Look at Examples 35 and 36.
To replace the fully qualified names System.IO.StreamReader and
System.IO.StreamWriter with the shorter names StreamReader and StreamWriter,
use this include statement:
<%@ Import Namespace=System.IO %>
Look at Examples 37.
To append to an existing file on the server,
create a StreamWriter object with the signature (string, bool).
For example
StreamWriter sw = new StreamWriter(e:/ectserver/ssmith/text.txt);
Always close a file when finished reading from or writing to it.
Look at Examples 38, 39 and 40.
ASP.Net Validation Controls
- ASP.Net offers validation controls to validate (check the correctness)
of a field in a control before it is posted back to the server.
- ASP.Net generates JavaScript code to perform this validation.
- Here are four validation controls and their use:
 
RequiredFieldValidator | Checks that the field is not blank. |
CompareValidator | Checks that a
field is greater than or less then a given value. |
RangeValidator | Checks that a field is within a given range. |
RegularExpressionValidator | Checks a field using a regular expression. |
- A table of
Common Regular Expression Symbols
- Some
Common Regular Expressions shown by the Visual Studio Regular Expression Editor.
- Practice Problems on Regular Expressions:
write a regular expression that validates each of the following
- Five digit zip code. Example: 64538
- Five digit zip code with optional 4 digit extension. Example: 64538-7346
- One of the letters F, f, M, or m, for male or female. Example: F
- Social security number. Example: 436-43-4399
- Legal C# variable name. Example: entry5
- Telephone number. Example: (312)728-5487
- Illinois drivers license number.
Use Example 34 to check your answers to the practice problems.
Example 32 uses validation controls to validate the data that is posted back to
the same page.
Example 33 uses validation controls, but posts the data to a different page
(verify2.aspx).
Projects 3 and 4
Arrays in C#
Declare an unitinialized array of int of size 5:
int[ ] a = new int[5];
Declare an initalized array of int:
int[ ] a = {5, 3, 7, 5, 3, 5, 4};
Declare an unitinialized array of int of size 4:
int[ ] x = new double[4];
Declare an initalized array of int:
int[ ] x = {3.32, 1.69, 7.83};
Declare an unitinialized array of int of size 6:
int[ ] s = new string[6];
Declare an initalized array of int:
int[ ] s = {dog, cat, mouse};
The indices and values of an array:
Expression | Index | Value |
a[0] | 0 | 5 |
a[1] | 1 | 3 |
a[2] | 2 | 7 |
a[3] | 3 | 3 |
a[4] | 4 | 3 |
a[5] | 5 | 5 |
a[6] | 6 | 4 |
Obtaining the value of the fourth item in the int array a:
value = a[4];
Changing the value of the third item in the int array a:
a[3] = 17;
Examples 18 through 21 show how to use arrays to simplify
C# code.