Introduction to XML s1
Total Page:16
File Type:pdf, Size:1020Kb
CPAN 660 ASP.NET
Lecture #2: ASP.NET Syntax and Structure
ASP.NET files are identified by the extension .aspx. The following elements can be used with ASP.NET: Directives: <%@ directive %> Code Blocks: The code inside the script tag must be declaration, which means it must be placed either in a function or a subroutine, so we can execute this code later.
Server Controls:
Storing Information in ASP.NET using VB syntax: Variables are labeled memory location for storing information. VB.NET as well as the other .NET languages is strongly typed. This means any variable used in the code must be declared with a specific data type. The keyword Dim is used in VB.NET to declare a variable. VB.NET supports 12 different types, which are: Integer, Byte, Short, Long, Single, Double, Decimal, String , Char, Date, Boolean and Object. For example:
Dim strName As String Dim intMark As Integer
Variables can be initialized using the assignment operator, For example strName=”John Smith” intMark=75
You can also declare and initialize the variable in one statement as shown in the following example:
Dim strName As String =”John Smith” Dim intMark As Integer=75
Arithmetic Operators:
1 Operation Operator Addition + Subtraction - Multiplication * Division / Exponentiation ^ Negation - Modulus MOD
Note:
Strings can be concatenated using &. For example:
Dim strFName As String =”John” Dim strLName As String = “ Smith” Dim strFullName strFullName=strFName & “ , “ & strLName
Arrays Arrays are used to store a sequence of items of the same data type. For example, the following statement declares an array of size 6: Dim intMarks(5) As Integer
Then each element must be initialized using the assignment operator: intMarks(0)= 60 intMarks(1)= 78 intMarks(2)= 70 intMarks(3)= 86 intMarks(4)= 96 intMarks(5)= 69
Each element in the array is accessed via a zero based integer value called index. For the above example, the first item in the array is accessed by the index of zero:
VB.NET also supports multi-dimensional arrays. The following example shows how to create a two dimensional array of 3 rows and 3 columns:
Dim strArray(2,2) As Integer strArray(0,0)=”value1” strArray(0,1)=”value2”
2 strArray(0,2)=”value3” strArray(1,0)=”value4” strArray(1,1)=”value5” strArray(1,2)=”value6” strArray(2,0)=”value7” strArray(2,1)=”value8” strArray(2,2)=”value9”
Constants A Constant is a labelled memory location to store constant data. The keyword Const is used in VB.NET to declare a constant. For example:
Const TAX=0.015
Relational (Comparison) Operators
Operation Operator Equality = Inequality <> Less than < Less than or equal to <= Greater than > Greater than or equal to >=
Logical Operators
NOT AND OR
Decision making: If … Then Structure: For example: If intCount =10 Then ‘ do something End If
If … Then … Else Structure: For example: If intCount =10 Then ‘ do something Else ‘ do something else
3 End If If .. Then .. ElseIf Structure: For example If intCount =1 Then ‘ take action 1 ElseIf intCount=2 Then
‘ take action 2 Else ‘ take other action End If Select Case Structure: For example: Select Case answer Case “yes” ‘Take action 1 Case “no” ‘ Take action 2 Case Else ‘ Take optional default action End Select
Iteration : For .. Next Structure Used when the number of iterations is known in advance. For example For intCount= 0 To 10 ‘ do something Next intCount
Do While Structure Used to loop unknown number of times, as long as the specified condition is true. For example:
Dim intCount As Integer = 0
Do While intCount <= 10 ‘do something Loop
Do… Until Structure Used to loop unknown number of times till a condition is met. Since the condition is placed at the end of the loop, this means that the loop will execute at least once. For example:
Dim intCount As Integer = 0
4 Do ‘do something Loop Until intCount = 10
For Each … Next Structure Operate on all elements of an array or a collection. For example
Dim inttem As Integer For Each intItem In intMarks ‘ do something Next
Subroutines and Functions A Subroutine is a module of code that performs certain action without returning any value. A Function is a module of code that performs certain action and returns a value. Parameters can be passed by value or by reference. The default is to pass the parameter by value. Passing a parameter by value means that we are passing a copy of the original data instead of the original data itself. Passing the parameter by reference will cause changes to affect the original data.
Following is an example of a subroutine:
Sub mySub(ByVal value As String) .. MsgBox (“hello” & value) End Sub
And following is an example of a function:
Function myFunc(ByVal value As Integer) As Integer
Return value*2 End Function
To call a subroutine we use:
mySub(“john”) To call a function, we use:
Dim intResult As Integer intResult=myFunction(10)
Variable Scope
5 A variable scope is the area in the code where the variable is accessible. We can have 3 levels of scopes: Global: Usually defined outside any procedure, function or block of code. They are accessible anywhere inside the page. Local: Usually defined in a subroutine or a function and is accessible only within this function or procedure. Block level: Usually defined in a block such as For .. Next and are accessible only within the specified block.
Examples:
Ex1: inlineCodeEx.aspx The following example uses inline code:
<%@ Page language="VB" %>
<% dim strName As String = "Muthana" %> <%= "hello"& strName %>
<% Response.write ("the date and time is " & Hour(Now) & ":"& Minute(Now) & ":"& Second(Now) & Now()) %>
Placing the code inline, make it harder to maintain. Therefore using the script tag and the server control are considered the best way to embed you ASP.NET code.
Ex2: scriptTagEx.aspx The following example uses the script tag:
<%@ Page language="VB" %>
Ex3:serverControlEx.aspx
The following example uses a Server controls:
Ex4:ExternalCodeEx.aspx
When we use the script tag and or the server controls, the code does not have to be in the same page. We can separate the code from the user interface. Following is a code behind the scene file called externalCodeEx.aspx.vb :
Imports System Imports System.Web.UI Imports system.Data Imports System.Web.UI.WebControls
7 Public Class Test :Inherits Page
Protected WithEvents time As Label
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Date1 As DateTime
time.Text = "hello" & Date1.Now End Sub
End Class
And following is the user interface file which is called exteranlCodeEx.aspx:
<%@ Page Language="vb" AutoEventWireup="false" Src="externalCodeEx .aspx.vb" Inherits="Test"%>
Ex5: Following is an ASP.NET file called login.aspx that processes the user login via an HTML form:
<%@ Page language="VB" %>
And following is an HTML form called login.html that will be submitted to the login.aspx:
Ex6:eventhandle.aspx:
The following examples shows how to use the event handling with the ASP.NET Server Controls:
Sub ClickHandle(Sender As Object, E As EventArgs)
Dim strName As String = "Muthana" userName.Text= "hello" & strName time.Text="the date and time is " & Hour(Now) & ":"& Minute(Now) & ":"& Second(Now) & Now() End Sub
Exercise:
Create an ASP.NET application that uses web form (server control) to validate the user login.
10