Active Server Pages Framework
Active Server Pages Framework (.aspx) is a server side programming language capable of building powerful web applications. Asp.Net has built in capabilities which make easier for programmers to build applications. With Asp.net you can create dynamic web pages, access databases, send emails, manage file system and much more.
Language Selection
Asp.net requires a support language. The .Net Framework has support for several programming languages with the following as the most popular ones:
- VB (Visual Basic)
- C# (C Sharp)
- JScript
To specify the selected language a Page Directive is included on an Asp.Net document:
<%@ Page Language=”VB” %>
Active Server Pages or ASP, as it is more commonly known, is a technology that enables you to make dynamic and interactive web pages.
ASP uses server-side scripting to dynamically produce web pages that are not affected by the type of browser the web site visitor is using.
The default scripting language used for writing ASP is VBScript, although you can use other scripting languages like JScript (Microsoft's version of JavaScript).
ASP pages have the extension .asp instead of .htm, when a page with the extension .asp is requested by a browser the web server knows to interpret any ASP contained within the web page before sending the HTML produced to the browser. This way all the ASP is run on the web server and no ASP will ever be passed to the web browser.
Any web pages containing ASP cannot be run by just simply opening the page in a web browser. The page must be requested through a web server that supports ASP, this is why ASP stands for Active Server Pages, no server, no active pages.
As ASP was first introduced by Microsoft on it's web server, Internet Information Services (IIS), that runs on Windows 2000/XP Pro/NT4, it is this web server that ASP pages usually run best on.
For those of you running Windows and wish to play around with ASP on your own system you will need to install Microsoft's Internet Information Services (IIS). Lucky IIS or its micro version Personal Web Server (PWS) comes free with Windows.
For Windows users you can find Internet Information Services (IIS) or Personal Web Server (PWS) in the following places: -
Windows 2000/XP Pro - IIS can be found in 'Add/Remove Programs' in the 'Control Panel'.
Windows 98 - PWS can be found under 'add-ons' on the Windows 98 CD.
Windows NT4/95 - You can get hold of IIS by downloading the NT4 Option Pack from Microsoft (don't be fooled by the name as it also runs on Windows 95).
Windows ME - IIS and PWS are not supported on this operating system.
Windows XP Home Edition - IIS and PWS are not supported on this operating system.
1.
The process of merging strings to form one text sentence.
-------------------------------------- SAMPLE CODE -------------------------------------------
Dim Message As String = "First Line of text <br>"
'Append lines to first line
Message = Message & "Second Line of text<br>"
Message = Message & "Third Line of text"
Response.Write(Message)
Response.Write("<br>")
'Join left and right text to a variable to form one sentence
Dim Comment As String = "My cat is <b>" & MyCat & "</b> and he is very young"
Response.Write(Comment)
-------------------------------------- OUTPUT ------------------------------------------------
First Line of text
Second Line of text
Third Line of text
2.
INCLUDE is used to include external files
<!-- #include file="test.aspx" -->.
3.
Sub program that can take data called parameters (optional), execute a computation (task) and return a value.
Function is a reusable piece of code. By reusable we mean that you need to make only once but you can use it many times. for example, a function of addition on a calculator. the keywords for making a function are function, return, end function. as functions, sub routines and classes are the part of VB.net, thats why we will use a separat block for it, instead of using asp block. the tag for this block is "< SCRIPT > " Example is below
Function is a reusable piece of code. By reusable we mean that you need to make only once but you can use it many times. for example, a function of addition on a calculator. the keywords for making a function are function, return, end function. as functions, sub routines and classes are the part of VB.net, thats why we will use a separat block for it, instead of using asp block. the tag for this block is "< SCRIPT > " Example is below
<script runat="server">
function add(a as integer, b as integer)as integer
Dim c as integer
c = a + b
return c
End function
</script>
function add(a as integer, b as integer)as integer
Dim c as integer
c = a + b
return c
End function
</script>
<%
response.Write(add(3, 8))
response.Write("<BR>")
response.Write(add(73, 83))
response.Write("<BR>")
response.Write(add(45, 456))
response.Write("<BR>")
%>
response.Write(add(3, 8))
response.Write("<BR>")
response.Write(add(73, 83))
response.Write("<BR>")
response.Write(add(45, 456))
response.Write("<BR>")
%>
<script runat="server">
function vat(price as decimal) as decimal
Dim result as decimal
result = price * 17.5/100
return result
end function
</script>
function vat(price as decimal) as decimal
Dim result as decimal
result = price * 17.5/100
return result
end function
</script>
<%
response.Write(vat(100))
response.Write("<BR>")
response.Write(vat(500))
response.Write("<BR>")
response.Write(vat(1000))
response.Write("<BR>")
%>
response.Write(vat(100))
response.Write("<BR>")
response.Write(vat(500))
response.Write("<BR>")
response.Write(vat(1000))
response.Write("<BR>")
%>
4.
Asp.Net bases on Events. These are actions that occur on objects such a mouse click on a button. Below is an example of some of the events available:
OnClick Execute when a button is clicked
OnSelectedIndexChanged Executes when an option in dropdownlist is selected
OnCheckChanged Executes when a checkbox or radiobutton are checked
OnSelectedIndexChanged Executes when an option in dropdownlist is selected
OnCheckChanged Executes when a checkbox or radiobutton are checked
When an event is triggered it requests a piece of program to be executed. If you want an email to be sent you must click on the send button, this is linked to a program that will send the email over the internet. These programs are called Event Handlers, they execute at an event’s request. Below is an example of pre-defined Event Handlers:
Page_Load Executes when the page load
Page_Unload Executes when the page is close
Page_Unload Executes when the page is close
Subroutine: Sub program that can take data called parameters (optional) and execute a task. It does not return a value. Subroutines are used to define event handlers.
Sub AnyNameHere(info As String)
End Sub
An event handler must be declared as follow:
Sub BTN_Click(sender as Object, e as EventArgs)
End Sub
Note that the event handler passes two arguments; sender as Object represents the object raising the event. And e as EventArgs represents the data specific to the event which is being sent to the server (if any). This is called Event Argument.
To relate an Event to an Event Handler use the following syntax on the object that will raise the event:
<asp:button id=”btn” runat=”server” text=”Enter” OnClick=”BTN_Click()”/>
<script runat="server">
sub Display(sender as object, e as EventArgs)
lbl.text = "some one has clicked on the button to show this message on a label"
End sub
sub Hide(sender as object, e as EventArgs)
lbl.text = " "
End sub
</script>
sub Display(sender as object, e as EventArgs)
lbl.text = "some one has clicked on the button to show this message on a label"
End sub
sub Hide(sender as object, e as EventArgs)
lbl.text = " "
End sub
</script>
<form runat="server">
<asp:Label runat="server" ID="lbl"></asp:Label>
<asp:Button runat="server" ID="BtnDisplay" Text="Show message" OnClick="Display"></asp:Button>
<asp:Button runat="server" ID="BtnHide" Text="Hide Message" OnClick="Hide"></asp:Button>
</form>
<asp:Label runat="server" ID="lbl"></asp:Label>
<asp:Button runat="server" ID="BtnDisplay" Text="Show message" OnClick="Display"></asp:Button>
<asp:Button runat="server" ID="BtnHide" Text="Hide Message" OnClick="Hide"></asp:Button>
</form>
5.
<script runat="server">
class student
public name as string
public age as integer
public function msg as string
return "my name is " & name & "my age is " & age
end function
end class
</script>
public name as string
public age as integer
public function msg as string
return "my name is " & name & "my age is " & age
end function
end class
</script>
<%
Dim obj as new student
obj.name = "shahid"
obj.age = 25
Response.Write(obj.msg)
%>
Dim obj as new student
obj.name = "shahid"
obj.age = 25
Response.Write(obj.msg)
%>
6.
Process of posting data to the server and retrieving values sent.
Retrieve Form Values:
To retrieve the values from a runat server form:
| Control | Retrieve value |
| <asp:textbox id=”txtName” runat=”server” text=””/> | txtName.text |
| <asp:checkbox id=”cbName” runat=”server” text=”HTML”/> | cbName.text |
| <asp:dropdownlist id=”ddlName” runat=”server”> <asp:listitem value=”1” text=”First”/> <asp:listitem value=”2” text=”Second”/> <asp:listitem value=”3” text=”Third”/> </asp:dropdownlist> | ddlName.SelectedItem.Text or ddlName.SelectedItem.Value |
To retrieve form values sent from another page:
| Form Type | Retrieve value |
| <form action=”getvalues.aspx” method=”post”> <input type=”text” name=”txtName”> </form> | Request.Form(“txtName”) |
| <form action=”getvalues.aspx” method=”get”> <input type=”text” name=”txtName”> </form> 7. To write comments in asp.net a single quote(') is used. For mulit line comments /* can be used. Examples are below <% 'this is asp.net comment <%-- thi is multi Line comments Using asp.net --%> %> |
8.
To display messages or output on the website in asp.net, they keyword is Response.Write Response.write can also be used include HTML tags in asp.net. Example is below
<%
Response.Write("This is asp.net message")
Response.Write("<BR>")
Response.Write("<B>This is second line</B>")
<%
Response.Write("This is asp.net message")
Response.Write("<BR>")
Response.Write("<B>This is second line</B>")
%>
9.
Space in the computer memory to store multiple values of the same type. It is identified by a name and its values by an index which starts at zero. Arrays are defined using the highest index to be contained within the collection.
'Arrays are zero based there for 2 indicates three elements (0, 1, 2)
DIM student(5) as string
DIM student(5) as string
'Assign a value to each element using its index
student(0) = "james"
student(1) = "aron"
student(2) = "johanna"
student(3) = "shahid"
student(0) = "james"
student(1) = "aron"
student(2) = "johanna"
student(3) = "shahid"
Response.Write(student(0))
response.Write("<BR>")
Response.Write(student(1))
response.Write("<BR>")
Response.Write(student(2))
response.Write("<BR>")
Response.Write(student(3))
response.Write("<BR>")
Response.Write(student(4))
response.Write("<BR>")
response.Write("<BR>")
Response.Write(student(1))
response.Write("<BR>")
Response.Write(student(2))
response.Write("<BR>")
Response.Write(student(3))
response.Write("<BR>")
Response.Write(student(4))
response.Write("<BR>")
10.
Loop is used to doing something again and again. for example, if you want to type your name 100 times. you dont have to use response.Write 100 times instead you can type once and put it in a loop. There are many types of loop for example, for loop, while loop, for each loop. All these loop perform same action. But for loop is used when we know how many times the loop is going to run. While on the other hand can be used, when we are not sure about the number of execution.
For Loop: This loop requires a start number and an end one. It increments the value of its counter automatically.
'Set the loop to execute 3 times (1, 2, 3)
'Counter variable will have its value incremented by one each time
Dim Counter as Integer
For Counter = 1 To 3
Response.Write("The value of loop Counter is " & Counter & "<br>"
Next
'Set the loop to execute 3 times (1, 2, 3)
'Counter variable will have its value incremented by one each time
Dim Counter as Integer
For Counter = 1 To 3
Response.Write("The value of loop Counter is " & Counter & "<br>"
Next
While Loop: This loop will execute as long as the condition give is true. It does not increment the value of its counter automatically. WE MUST DO.
‘Set counter as 1Counter = 1
'Set the loop to execute 3 times (1, 2, 3)
While Counter <= 3
Response.Write("The value of loop Counter is " & Counter & "<br>")
'Increment the value of variable by one
Counter = Counter + 1
End While
<%
Dim i as integer = 1
While (i<=100)
response.Write(i)
response.Write(". Shahid")
response.Write("<BR>")
i= i + 1
end while
%>
‘Set counter as 1Counter = 1
'Set the loop to execute 3 times (1, 2, 3)
While Counter <= 3
Response.Write("The value of loop Counter is " & Counter & "<br>")
'Increment the value of variable by one
Counter = Counter + 1
End While
<%
Dim i as integer = 1
While (i<=100)
response.Write(i)
response.Write(". Shahid")
response.Write("<BR>")
i= i + 1
end while
%>
For Each Loop: This loop moves through an array collection.
‘Create variable to store the value of each array elements
Dim Cat As String
‘Create array
Dim MyCats(1) As String
MyCats(0) = “Tom”
MyCats(1) = “Jerry”
'Loop through the array collection
For Each Cat In MyCats
Response.Write(Cat & "<br>")
Next
A practical Example:
Dim st as string
For each st in student
response.Write(st & "<BR>")
Next
‘Create variable to store the value of each array elements
Dim Cat As String
‘Create array
Dim MyCats(1) As String
MyCats(0) = “Tom”
MyCats(1) = “Jerry”
'Loop through the array collection
For Each Cat In MyCats
Response.Write(Cat & "<br>")
Next
A practical Example:
Dim st as string
For each st in student
response.Write(st & "<BR>")
Next
11.
Conditional Stements are used when we want to perform an action based on a condition. For instance, we can say it its rainting stay at home else lets go out. Example is below
One Condition:
One Condition:
If condition Then
Execution…
End If
Two Conditions:
If condition Then
Execution…
Else
Execution…
End If
Note: else will always mean the opposite to if
More than Two Conditions:
If condition Then
Execution…
Else If condition Then
Execution…
Else
Execution…
End If
<%
Dim age as integer = 25
if age = 25
response.Write("value of age is 25")
else
response.Write("Do you know the value of age?")
end if
%>
Dim age as integer = 25
if age = 25
response.Write("value of age is 25")
else
response.Write("Do you know the value of age?")
end if
%>
12.
A variable is stored temporary in the computer memory, but sometimes we need to hold data across different pages. Here is when Session Variables come very helpful. They can retain their values as long as the user:
- Does not close the browser
- Does not leave the page inactive for a period longer than 20 minutes
(unless specified otherwise) - Does not end its own session (e.g. logout)
To create a session variables:
Session(“VariableNameHere”) = “VariableValueHere”
To set a different session time out in minutes:
Session.TimeOut = 40
To remove the session variable:
Session.Contents.Remove(“VariableNameHere”)
13.
ASP code can be typed in asp render block. We need to start asp block by using ( < % ) & we can end it by using ( % > ).
Example is below
<%
<%
%>
Or a using the code declaration script block:
<script runat=”server”>
</script>
** Sample document structure **
<%@ Page Language=”VB” %>
<script runat=”server”>
<script runat=”server”>
‘Program goes here
</script>
<html>
<head>
<title>Asp.Net Programming</title>
</head>
<body>
<html>
<head>
<title>Asp.Net Programming</title>
</head>
<body>
Document contents here…
</body>
</html>
</html>
No comments:
Post a Comment