unit 27 - web server scriptingwiki.hct.ac.uk/_media/computing/btec/level3/2011_2... · unit 27 -...
TRANSCRIPT
1
2
Unit 27 - Web Server Scripting
Lesson 5 - Data Access with ASP.NET
3
Last Week
• Capturing User Information
• Processing User Input
• Validation
• Server Side Processing
4
This week
• Data display
• Storing Data
• Accessing Database data
5
Multiline display
6
7
Using Arrays to store data
8
An array is created and
loaded with data (colours) at
page load time into a list
box control. It will only be
loaded the first time the
page is displayed –
ispostback property!
When the button is clicked
the text is concatenated
with the colour selected and
the forecolor property of the
label control is changed to
the selected colour.
When the program is run, a
colour is selected from the
drop down lost box and the
button is clicked. The text is
then displayed in the correct
colour in the text box.
9
When the
user selects
a colour –
the text is
displayed
using the
colour
selected.
ArrayLists
10
Using ArrayLists which are a
special kind or array that can store
a collection of elements and add or
remove elements easily. They do
have performance considerations
as are they require more
processing power. So if it just a
simple list of elements then use an
ordinary array.
In this example- additional controls
have been added to the form so
that the user can select the font for
display as well as the colour.
The font names are added to the
fontlist on page load and the
datasource (fontlist) is identified as
the source of data and bound to
the dropdownlistbox.
Notice that the databinding is done
in one statement – you do not have
to iterate through the whole
collection to add it to the list. You
can do this ordinary arrays as
well!!
The messages output are changed
to reflect the font.
11
Adding Array items
12
13
Hash Tables
14
Hashtables
15
Hash tables are used to associate keys with values that can identify them.
An array has an index number which we can refer to but you would need to
know the index number to find the list entry you wanted. A hash table
replaces the index number system with a key that is specified for each list
item which relates to a corresponding value.
A good use of hash tables is with country codes where the abbreviations
would be UK for example and then when selected the whole country name
would be displayed.
Have amended the example to allow the user to select a value from a list
and then to display the associated text item.
Notice that the databind this time is for the hashtable keys!!
There is a sortedlist collection that works in exactly the same way as the
hashtable but the keys are automatically sorted into alphabetical order as
they are entered. In this example we would just need to change the
declaration of quotelist to sortedlist rather than hashtable
16
Using Database Data
• SQL or Access database connections available.
• DataSource Controls
• Gridview (and other controls) available.
17
CAM04 Database
18
AccessDataSource
19
Drag an
AccessDataSource
Control onto the
form and use
Configure Data
Source (from its
associated task list)
to point to the
Access database,
the relevant table
and fields.
AccessDataSource
20
AccessDataSource
21
AccessDataSource
22
If the table is
updatable you will
have the option of
generating the
necessary Insert,
Update and Delete
SQL statements.
AccessDataSource
23
The query
can be
tested to
show that
the database
connection
has been
established.
Gridview control
24
From the
Gridview
tasklist we can
choose Data
Source which
will then link to
the
AccessDataSo
urce we have
created.
Gridview control
25
Gridview control
26
Gridview control in action
27
Formating Gridview Columns
28
The Columns property
of the GridView is a
collection of editable
items. We want to alter
the format of the
Release Date column.
Change it to {0:dd MMM
yyyy} – note may need
to set HTML Encode
property for this item to
False.
Formating Gridview Columns
29
We now have the
date column
looking much tidier.
Formating Gridview Columns
30
To get the album
images we need
to include an
image field in the
GridView table
Formating Gridview Columns
31
It must be
bound to the
correct
datafield
Formating Gridview Columns
32
And here is
the latest
result.
Formating Gridview Columns
33
And by
adding a final
hyperlink
column we
have the final
result.
DetailsView Control
34
DetailsView Control
35
FormsView Control
36
FormsView Control
37
Tutorial
38
In the tutorial this week
you will create this page.
It contains a GridView
showing all the records
from a database. When
the user clicks on the
album cover picture they
will be able to display
more details from
another page.
Do not implement other
page.!!