East Africa University Qardho Campus Faculty of Information Science & Technology
East Africa University Qardho Campus Faculty of Information Science & Technology
Qardho Campus
Faculty of Information Science & Technology
Semester VIII
ASP.Net
Course Lecturer
Daud Noh Mohamed
COURSE CONTENTS
PART ONE:
PART TWO:
1|Page
CHAPTER ONE
INTRODUCTION
.NET:
.NET is the result of a complete make-over of Microsoft’s software development products, and
forms part of the company’s new strategy for "Delivering software as a service". The key
features that .NET offers include:
1. .NET Framework
2. .NET Products
3. .NET My Services
.NET FRAMEWORK:
Purpose: Similar to JEE platform and like Java’s JVM runtime engine.
Application: .net run time engine must be installed in the computer in order to run .net
application.
Similar to Java because it uses an intermediary byte code language that can be executed on
any hardware platform that has a runtime engine.
.net compiler generates a Microsoft Intermediate Language (MSIL) byte code, which is
executed by the .net Common Language Runtime Engine (CLR).
2|Page
.NET FRAMEWORK COMPONENTS:
.NET LIMITATIONS:
.NET PRODUCTS:
Office.net
Visual Studio.net
Windows.net(Window Server)
ASP.NET:
Released in January 2002 with 1.0 version of the .net framework it’s successor Microsoft ASP
(Active Server Page) technology.
ASP.net is built on the common language runtime (CLR) enables the user to write ASP.net code
using any supported .net language.
DEFINITION:
3|Page
It’s a server-side web application framework designed for web development to provide
dynamic web pages.
ASP.NET FEATURES:
ASP.NET VERSIONS:
ENVIRONMENTS:
Visual Studio
Microsoft Visual Web Developer
PREREQUISITES:-
Designed for web and application developers who want to build dynamic database-
enabled web sites.
Useful programming skills …
o HTML …
o Cascading Style Sheets (CSS) …
o Basic Programming …
o SQL (Structured Query Language)
A Windows-based computer
Windows Vista or Windows XP (SP2 or Higher)
For installing Internet Information Services (IIS) Vista Home Premium or Higher ... XP
Professional or Higher … Windows Server 2003 or 2008.
At least 1 Gigabyte of RAM … two Gigabytes are better.
4|Page
The World Wide Web consists of many clients and servers connected over internet.
Computers communicate with each other using Hyper Text Transfer Protocol (HTTP).
A client sends a request in HTTP format for a web resource … requests
o Web Page …
o Image File …
o JavaScript … CSS File.
A server returns a response.
Each client has a web browser …
o Personal Computers (Internet Explorer, Firefox, Safari).
o Cell phones
o Other devices.
Each server runs software called an HTTP server.
o Internet Information Server (Windows only)
o Apache (All operating systems).
5|Page
RETRIEVING DYNAMIC DATA:-
To view an ASP.NET web page that resides on a web server, we need to request it using a web
browser. The browser sends a request to the web server, The ASP.NET engine processes the
requested page, returns the resulting HTML to the web server, which then sends it back to the
browser, where it is displayed to the user. When you’re developing ASP.NET websites, the
ASP.NET pages you create are saved on your personal computer. To test these pages, then,
your computer must have a web server installed. Fortunately, you do not need to concern
yourself with installing a web server on your computer. Visual Web Developer, the editor we’ll
be using throughout this book to create our ASP.NET websites, includes a lightweight web
server specifically designed for testing ASP.NET pages.
ADVANTAGES OF WEB SERVERS:
Hosting ASP.NET pages locally through the ASP.NET Development Web Server has a number of
advantages:
6|Page
Testing can be done while offline—Because the request from your browser is being
directed to your own personal computer, you don’t need to be connected to the
Internet to test your ASP.NET pages.
It’s fast—Local requests are, naturally, much quicker than requests that must travel over
the Internet.
Advanced debugging features are available—By developing locally, you can use
advanced debugging techniques, such as halting the execution of an ASP.NET page and
stepping through its code line-by-line.
It’s secure—The ASP.NET Development Web Server allows only local connections. With
this lightweight web server, you don’t need to worry about hackers gaining access to
your system through an open website.
<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>
CHAPTER TWO
GETTING STARTED
7|Page
Programming Language to build your Website: C# or vb
Platform : Windows
Project: Web
Click on:
ASP.NET Website.
From Location choose the name of your web and the location you will save it.
From Language choose whether you are using Visual Basic or C Sharp.
Click OK.
8|Page
We choose Web Form
9|Page
The program will create a web: Default.aspx
THE INTERFACE:
10 | P a g e
Source view—You can either type in the HTML and Web control syntax by hand or drag
the desired HTML elements and Web controls from the Toolbox onto the Source view.
Design view—This view presents a What You See Is What You Get (WYSIWYG) interface.
You can add text to the page by typing, but HTML elements and Web controls can be
added only by dragging them from the Toolbox and dropping them onto the design
surface.
Split view—This view shows both the Source and Design views.
File Menu: In this menu there are many commands to manipulate your web files like:
New Web Site … Open … Close … Save.
Edit Menu: In this menu there are commands editing your web … like: Copying … Pasting
…
View Menu: includes choices of adding particular panels into interface. By default you
see: Solution Explorer .. database explorer and property panels.
You can close any panel you want to remove it from the interface.
11 | P a g e
Also your can drag a panel to any where your want
12 | P a g e
Then everything will return to it’s original position.
Tool bar: Most used commands from the menu are in tool bar .. you can easily use it wihout
opening the menu.
For example:
Size: x-large
Style: Bold
Font face: Tunga
Heading 1
13 | P a g e
Click on the source your see the text is entered between head1 or h1 as shown here.
THE TOOLBOX
The Toolbox contains HTML elements and Web controls that can be dragged onto the
WYSIWYG designer. By default, the Toolbox is shown on the left side of Visual Web Developer;
if you do not see the Toolbox on your screen, you can display it by going to the View menu,
expanding the Other Windows submenu, and selecting the Toolbox menu item.
o Standard
o Data
o Validation
o Navigation
o Login
o WebParts
o AJAX Extensions
o Dynamic Data
o HTML
14 | P a g e
o General
Each tab contains a number of elements underneath it. By default, the Standard tab will be
expanded, which includes items such as Button, Label, LinkButton, TextBox, and so on.
PROPERTIES WINDOW
Every control in the toolbox has it’s own properties and those properties can be modified in
the properties window.
SOLUTION EXPLORER:
This window locates in the top right side of the window and holds the web files, also the new
items can be added from here.
A website is a repository of related files and subdirectories. Websites typically contain files of
the following types:
Static web pages—An HTML page is a static web page. Unlike an ASP.NET page, it
contains only HTML content—no Web controls and no source code. As its name implies,
the content of these types of files is static and cannot be altered based on user input,
server-side data, or other criteria.
ASP.NET pages—ASP.NET pages are the dynamic web pages in your site. They are
implemented as two files: PageName.aspx, which contains the HTML portion, and
PageName.aspx.cs, which contains the source code portion.
Image files—Most websites have various images, logos, and clip art. These image files
typically are stored in the website, either in the root directory or in an Images folder.
Configuration files—ASP.NET websites contain a configuration file named web.config
that provides server-side setting information.
15 | P a g e
Style sheet files—Style sheets are files that instruct the browser how to format and
display different types of content. For example, in your site you might want all content
displayed in the Arial font and content within <h1> tags displayed in italics. You can
specify this aesthetic information through cascading style sheet (CSS) rules defined in
style sheet files.
Web Developer is enabling you to run and test your web when IIS is not installed in your
computer.
When you create your first web and debug it, your web will be shown using the browser
installed on your computer like Internet Explorer or Firefox.
Local host: Automatic name for web server that is on your local system.
4913: Numeric value called port number when you requesting a page from internet you use
code number 80 because it is default HTTP port … Secured HTTP port is 443. This is dynamic
port number, you can change it
When you are deploying your ASP.NET web for production that is for use by your users you
want to install and use IIS.
16 | P a g e
WHAT WE MEAN DYNAMIC PAGE:-
Dynamic pages read data from separate page or file … the code or data you are working with is
not on the page shown but the web developer reads it from another file or page and show you
from the page you opened.
1. Add new page into your web by going Project Add New Item
17 | P a g e
4. Web forms are added in your Project Explorer
ASPX files usually will have the UI and will which is usually HTML tags, some ASP.NET server
control embed code (which ultimately produce some HTML markups). ASPX.CS file (usually
called "code-behind") will have server-side coding in C#.
Example:
<div>
</div>
18 | P a g e
o ASP.NET already includes all required drivers (software for communicating with
the database)
o Integrated with Windows security model for easy authentication.
<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>
CHAPTER THREE
A master page provides the layout and functionality to other pages. Creating a master page in
ASP.NET is very easy. Let's start creating the master page step by step.
Go to:
19 | P a g e
WRITING SCRIPTING FILE INSIDE A PAGE:
Add new form page and use the information shown as the page shown below:
20 | P a g e
ADDING CONTROLS TO THE PAGE:-
Add header into a form … Write: “Author Information”. And press enter to go the next
paragraph.
21 | P a g e
Add three labels , write them First Name, Last Name, Date of Birth respectively and
three textbox inside the cells of the table as shown below …
22 | P a g e
Change the text property of the button to: submit
23 | P a g e
The form is submitted to the server and still holds and displaying the data I typed in it …
and that is the main difference between ASP.NET controls and basic HTML controls.
Right click on anywhere on your page and choose: View Page Source.
24 | P a g e
25 | P a g e
HANDLING POST BACK DATA IN WEB FORM PAGE:-
Add three labels inside the panel … After you add a label press Enter to add another.
26 | P a g e
Change the IDs of the labels as follows:
o Label1=fnamelabel
o Label2=lnamelabel
o Label3=doblabel
Double click on the submit button and write this code
fnameLabel.Text = fnameInput.Text
lnameLabel.Text = lnameInput.Text
dobLabel.Text = dobInput.Text
27 | P a g e
The second approach: You can use page load function
This is also used to get data out of the server and displaying it on the screen.
28 | P a g e
Note: Text in front of the labels … like First Name, Last Name, and Date of Birth is not labels it is
written inside the panel.
Click anywhere on the page and write this code for data binding in the page load
29 | P a g e
Run the page and enter data … click on Submit button
<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>
CHAPTER FOUR
PROGRAMMING IN ASP.NET
PROGRAMMING IN ASP.NET
The primary language we use in ASP.Net Visual Basic.net and C# are syntactically very different
from each other but they both use the underline .net class library.
Create a page with two panels, textbox (To make it multiple lines choose TextMode property as
MultipleLines) and two buttons … put the textbox in the first panel and the two buttons in the
second panel.
30 | P a g e
Write a function for jumping the new line when writing the textbox
As shown here:
As shown here:
Run the code and click on the run button many times then click on clear button
31 | P a g e
VARIABLES IN C#
All programming languages support the concept of variable … a place to hold data in memory.
You start variable data type … then variable name then semi colon or the equal sign with
variable value between double quotations and then semi colon.
Use our last program and write this code at run button.
32 | P a g e
output("The value of counter is: "+counter);
}
}
The output:
While loop:
Note: If the counter is declared inside the for loop expression the counter variable will expire
when the for loop is finished .. Therefore the counter variable is to be declared before the for
loop expression.
33 | P a g e
The output:-
FUNCTIONS:
As ASP.net web page become more complex it is useful to add breakout functionality and
separate subroutines for functions. C# supports this feature.
If the function is written in the same page which it is called then declare it as Private.
If the function is available from this page and any other page differ from this page (In
object oriented term=sub classes of this page) declare it as protected.
If you want the function is callable anywhere in the application declare it as public.
34 | P a g e
The output:
COMMENTING CODE:
You can use comment to document features on your code that may not easy to figure
out.
Or temporarily block a certain code from executing
35 | P a g e
First: commenting C# code:
You can use beginning of line comment: that is double slash characters (//) as you
studied in C++ and Java. This prevents a certain line of code to be executed.
Or you can use block comment: the block comment starts with slash and asterisk and
ends with asterisk slash. (/* …………….. */)
Note: Ctrl+K Ctrl+C is commenting from the keyboard … Ctrl+K Ctrl+U is uncomment from the
keyboard.
<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>
CHAPTER FIVE
VALIDATION IN ASP.NET
BACKGROUND:
Whenever we have an application that expects user input, then it becomes important to
ensure the validity of the data input by the user. We might have scenarios when some data is
mandatory for the user to enter. There are scenarios when the user data has to be in some
particular format example email ID. There could be scenarios when we want the data to be in
some range example date input.
36 | P a g e
So for all the above mentioned scenarios, if we take the user input without validation, then
chances are that we will end up having wrong data with us (perhaps in database). If it is a bad
day for us then possibly our application might also end up behaving in an unexpected manner
and even crash on us (like if we try to convert a non numeric string to int). Worst case
scenario, the user will use the input field to perform SQL injection and cause serious damage to
our database. So it is always a good idea to have validation in place whenever we are taking
input from the user.
TYPES OF VALIDATION:-
Client side validation is something that will happen on users' browser. The validation will occur
before the data gets posted back to server. It is a good idea to have client side validation as the
user gets to know what needs to be changed immediately, i.e., no trips to servers are made. So
from the users' point of view, it gives him fast response and from the developers' point of view,
it saves valuable resources of server.
Server side validation occurs at server. The benefit of having server side validation is that if the
user somehow bypasses the client side validation (accidentally or deliberately), then we can
catch the problem on the server side. So having server side validation provides more security
and ensures that no invalid data gets processed by the application.
Server side validation is done by writing our custom logic for validating all the input. ASP.NET
also provides us some controls which will facilitate the server side validation and provides a
framework for the developers to do the same.
NOTE: Web developer may choose to go with any one type of validation but usually it is a good
idea to have client side validation and same validation on server side too. It sure takes some
resources of server to validate the already validated data (on client side) but it ensures security
and that is always good.
RequiredFiledValidator
37 | P a g e
CompareValidator
RangeValidator
RegularExpressionValidator
CustomValidator
ValidationSummary
We can tweak these validation control's properties for how to perform validation but first let us
look at some properties common to all these controls.
The toolbox has a validation part double any validation control you want to add into your page
VALIDATION SUMMARY:
It is a good idea to look at this control first. ValidationSummary control is a control which
displays the error messages for all the validation controls on the page in one place. If we want
to have this control display the error messages for some particular validation controls (possibly
because we have multiple ValidationSummary on page), then we can use ValidationGroup
property of the controls. Let's move ahead and see how validation summary will be useful for
us.
38 | P a g e
REQUIREDFIELDVALIDATOR:
This validation control will be used when we are mandating the user input for any particular
field. Let's say we have a simple form with name field and we don't want this to be empty. so
what we can do is add a RequiredFieldValidator to the page, set the
ControlToValidate to the ID of the name input field, set the error message property.
When we try to do a postback without entering name, then the postback will not happen and
the "*" will be displayed in place to our validation control. The ValidationSummary we
already added will display the full error message.
39 | P a g e
COMPAREVALIDATOR:-
40 | P a g e
RANGEVALIDATOR:-
In scenarios where we want to ensure that the value entered by the user is in some predefined
range, we can use this control. Let us try to add this control on our page and use this to validate
the age of the user. We are saying the valid age is between 18 to 50.
41 | P a g e
If we look at the timestamp we added on the page, we can verify that the validation is
happening on the client side and no postback is happening to the server. If we need to change
this to do this validation in server side, then we will have to set the EnableClientScript property
of the validation control to false.
42 | P a g e
Write this code in the validation expression
\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>
CHAPTER SIX
(CSS)
UNDERSTANDING CSS:-
43 | P a g e
TYPES OF CSS:-
1. Element
2. Class
3. ID
For example: if you want to create a CSS for header of the page
Choose: StyleSheet … name it as you like … select the language … in here my css name is:
MyStyle … and the language is C# .. click Add button.
44 | P a g e
Go to Server Explorer and double click your CSS file (Me: MyStyle.css)
Go to the code window of your page and write this line of code to connect the CSS
Run the page and you will see the changes Insha Allah.
45 | P a g e
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>
CHAPTER SEVEN
Choose “SQL Server Database” give name your database, select language and click “Add”.
46 | P a g e
Go to Server Explorer:
Open your database in Server Explorer by clicking the + sign and Right click on the tables and
choose “Add New Table”
47 | P a g e
Add three columns: No, Name, Faculty
Open “Identity Specification” in column Property by clicking the + sign and choose “Yes” in (Is
Identity) property.
48 | P a g e
Add some Students to the table, to do this right-click on the newly created table and select
“Show Table Data”
When you write the header press enter to move to the next paragraph.
49 | P a g e
Run the page and this is the result
50 | P a g e
To remove “No” column comment or remove the part of your code holding it
51 | P a g e
To sort the names ascending add this SQL command as shown below.
52 | P a g e
The [square bracket] denotes the phrase inside the bracket is column (field).
EDITING GRIDVIEW:
To change the column header: in the top right corner control you can see icon with right point
arrow … when you click it, it opens the GridView tasks: choose “Edit Columns”.
53 | P a g e
This is the result
54 | P a g e
CHANGING THE VIEW OF THE GRIDVIEW:-
Auto Format allows you to add a number of visual properties into your GridView with some
predefined schemes.
Highlight a column and in the properties window set allow paging =True
55 | P a g e
Reduce the page size if you choose 5 for example the page displays only five rows every time.
56 | P a g e
For example: Change the mode to: NextPreviousFirstLast
The GridView enables you a capability to modify the data in the database in the run time.
57 | P a g e
Run the page and click on the Edit button … change anything you like then click Update button.
58 | P a g e
Set the “Read Only” property as true.
Go to the smart tag and select Edit Column from the panel.
Choose field of “Faculty”, set the “Read Only” property as True .. and click “OK”.
59 | P a g e
CREATING DATALIST:-
Go to your database, drag the table, and drop it into your page.
60 | P a g e
Now we want to put the DataSource down the paragraph to insert the DataList control into the
paragraph area.
Like this
Go to the toolbox and add the DataList into the page inside the paragraph.
Choose “SqlDataSource1” from the Choose Data Source in smart tag panel.
61 | P a g e
Run the page
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
CHAPTER EIGHT
USING DETAILSVIEW:
62 | P a g e
This is a GridView we studied before therefore delete the GridView by clicking on it and press
Delete key from the keyboard … and leave SQLDataSource
Double click the DetailsView control on the toolbox to add it into your page.
This is not connected to any data source open: Choose Data Source in Smart Tag and choose
SqlDataSource
63 | P a g e
If you can’t see the columns of your table as shown above, click on “Refresh Schema”.
64 | P a g e
And this is the result
EXERCISE:
Change “Magac” to “Name” and “Cinwaan” to “Address” first from the smart tag and second
from the code window.
65 | P a g e
How to insert data into the table in the server-side
66 | P a g e
You will see the new button is added to your DetailsView
Open your table in the database and check if the new name is added.
67 | P a g e
<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>
68 | P a g e