<p> CPAN 660 ASP.NET</p><p>Lecture #2: ASP.NET Syntax and Structure</p><p>ASP.NET files are identified by the extension .aspx. The following elements can be used with ASP.NET: Directives: <%@ directive %> Code Blocks: <script runat=”server”> … </script> 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.</p><p> Server Controls: <tag runat=”server”> </tag> or <tag runat=”server”/> Data binding expressions: <%# %> Render code (inline): <% %> and <%= %> Server Side Comments: <%-- --%></p><p>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:</p><p>Dim strName As String Dim intMark As Integer</p><p>Variables can be initialized using the assignment operator, For example strName=”John Smith” intMark=75</p><p>You can also declare and initialize the variable in one statement as shown in the following example:</p><p>Dim strName As String =”John Smith” Dim intMark As Integer=75</p><p>Arithmetic Operators:</p><p>1 Operation Operator Addition + Subtraction - Multiplication * Division / Exponentiation ^ Negation - Modulus MOD</p><p>Note:</p><p>Strings can be concatenated using &. For example:</p><p>Dim strFName As String =”John” Dim strLName As String = “ Smith” Dim strFullName strFullName=strFName & “ , “ & strLName</p><p>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</p><p>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</p><p>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:</p><p>VB.NET also supports multi-dimensional arrays. The following example shows how to create a two dimensional array of 3 rows and 3 columns:</p><p>Dim strArray(2,2) As Integer strArray(0,0)=”value1” strArray(0,1)=”value2”</p><p>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”</p><p>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:</p><p>Const TAX=0.015</p><p>Relational (Comparison) Operators</p><p>Operation Operator Equality = Inequality <> Less than < Less than or equal to <= Greater than > Greater than or equal to >=</p><p>Logical Operators</p><p> NOT AND OR</p><p>Decision making: If … Then Structure: For example: If intCount =10 Then ‘ do something End If</p><p> If … Then … Else Structure: For example: If intCount =10 Then ‘ do something Else ‘ do something else</p><p>3 End If If .. Then .. ElseIf Structure: For example If intCount =1 Then ‘ take action 1 ElseIf intCount=2 Then</p><p>‘ 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 </p><p>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</p><p> Do While Structure Used to loop unknown number of times, as long as the specified condition is true. For example:</p><p>Dim intCount As Integer = 0</p><p>Do While intCount <= 10 ‘do something Loop</p><p> 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:</p><p>Dim intCount As Integer = 0</p><p>4 Do ‘do something Loop Until intCount = 10</p><p> For Each … Next Structure Operate on all elements of an array or a collection. For example</p><p>Dim inttem As Integer For Each intItem In intMarks ‘ do something Next</p><p>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. </p><p>Following is an example of a subroutine:</p><p>Sub mySub(ByVal value As String) .. MsgBox (“hello” & value) End Sub </p><p>And following is an example of a function:</p><p>Function myFunc(ByVal value As Integer) As Integer</p><p>Return value*2 End Function</p><p>To call a subroutine we use:</p><p> mySub(“john”) To call a function, we use:</p><p>Dim intResult As Integer intResult=myFunction(10)</p><p>Variable Scope</p><p>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.</p><p>Examples:</p><p>Ex1: inlineCodeEx.aspx The following example uses inline code:</p><p><%@ Page language="VB" %></p><p><html> <body> <% dim strName As String = "Muthana" %> <%= "hello"& strName %> <br/> <% Response.write ("the date and time is " & Hour(Now) & ":"& Minute(Now) & ":"& Second(Now) & Now()) %></p><p></body> </html></p><p>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.</p><p>Ex2: scriptTagEx.aspx The following example uses the script tag:</p><p><%@ Page language="VB" %></p><p><html> <body> <script language="vb" runat="server"> Sub Page_Load() Dim strName As String = "muthana"</p><p>6 Response.write ("hello " & strName)</p><p>Response.write (" <br/> the date and time is " & Hour(Now) & ":"& Minute(Now) & ":"& Second(Now) & Now()) End Sub </script> </body> </html></p><p>Ex3:serverControlEx.aspx</p><p>The following example uses a Server controls:</p><p><script language="vb" runat="server"> Sub Page_Load() 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 </script> <html> <body> <asp:label id="userName" runat="server" /> <br/> <asp:label id="time" runat="server" /> </body> </html></p><p>Ex4:ExternalCodeEx.aspx</p><p>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 :</p><p>Imports System Imports System.Web.UI Imports system.Data Imports System.Web.UI.WebControls</p><p>7 Public Class Test :Inherits Page</p><p>Protected WithEvents time As Label</p><p>Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Date1 As DateTime</p><p> time.Text = "hello" & Date1.Now End Sub</p><p>End Class</p><p>And following is the user interface file which is called exteranlCodeEx.aspx:</p><p><%@ Page Language="vb" AutoEventWireup="false" Src="externalCodeEx .aspx.vb" Inherits="Test"%></p><p><html> <body> <form id="Form1" method="post" runat="server"> <asp:label id="time" runat="server" /> </form> </body> </html></p><p>Ex5: Following is an ASP.NET file called login.aspx that processes the user login via an HTML form:</p><p><%@ Page language="VB" %></p><p><html> <body> <script language="vb" runat="server"> Dim arrID(2) As String Dim arrPassword(2) As String Dim i As Integer Dim blnFound AS Boolean </p><p>8 DIm id, pass As String</p><p>Sub Page_Load() arrID(0)="user1" arrPassword(0)="pass1" arrID(1)="user2" arrPassword(1)="pass2" arrID(2)="user3" arrPassword(2)="pass3" id=Request.Form("id") pass=Request.Form("pass")</p><p>For i=0 to arrID.Length -1 If arrID(i) = id And arrPassword(i) = pass Then blnFound= true Exit For End If Next i If blnFound Then Response.write("Login Successful") Else Response.write("Login Unsuccessful") End If</p><p>End Sub </script> </body> </html></p><p>And following is an HTML form called login.html that will be submitted to the login.aspx:</p><p><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Login Form</title> </head> <body> <form action="login.aspx" method="post">User ID<input type="text" name="id" /><br /></p><p>9 user Password:<input type="password" name="pass" /><br /> <input type="submit" /> <input type="reset" /></form> </body> </html></p><p>Ex6:eventhandle.aspx:</p><p>The following examples shows how to use the event handling with the ASP.NET Server Controls:</p><p><script language="vb" runat="server"></p><p>Sub ClickHandle(Sender As Object, E As EventArgs)</p><p>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</p><p></script> <html> <body> <form id="form1" runat="server"></p><p><asp:button id="Button1" Text="Click" onclick="ClickHandle" runat="server" /> <br/> <asp:label id="userName" Text="label 2" runat="server" /> <br/> <asp:label id="time" Text="label 1" runat="server" /> </form> </body> </html></p><p>Exercise: </p><p>Create an ASP.NET application that uses web form (server control) to validate the user login. </p><p>10</p>
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages10 Page
-
File Size-