Sei sulla pagina 1di 41

ASP.NET Tutorial - with ASP.NET 2.

0

ASP.NET is the next generation ASP, but it's not an upgraded version of ASP. ASP.NET is an entirely new
technology for serverside scripting.
ASP.NET is a part of the !icrosoft .NET fra"ewor#, and a powerful tool for creating dyna"ic and interactive
web pages.
ASP.NET Introduction

$hat %ou Should Already &now
'efore you continue you should have a basic understanding of the following(
$$$, )T!*, +!* and the basics of building $eb pages
Scripting languages li#e ,avaScript or -'Script
The basics of server side scripting li#e ASP or P)P
What is Classic ASP?
!icrosoft's previous server side scripting technology ASP .Active Server Pages/ is now often called classic
ASP.
ASP 0.1 was the last version of classic ASP.
ASP.NET is NOT ASP
ASP.NET is the next generation ASP, but it's not an upgraded version of ASP.
ASP.NET is an entirely new technology for serverside scripting. 2t was written fro" the ground up
and is not bac#ward co"patible with classic ASP.
ASP.NET is the "a3or part of the !icrosoft's .NET 4ra"ewor#.
What is ASP.NET?
ASP.NET is a server side scripting technology that enables scripts .e"bedded in web pages/ to be
executed by an 2nternet server.
ASP.NET is a !icrosoft Technology
ASP stands for Active Server Pages
ASP.NET is a progra" that runs inside 22S
22S .2nternet 2nfor"ation Services/ is !icrosoft's 2nternet server
22S co"es as a free co"ponent with $indows servers
22S is also a part of $indows 5111 and +P Professional
$hat is an ASP.NET 4ile6
An ASP.NET file is 3ust the sa"e as an )T!* file
An ASP.NET file can contain )T!*, +!*, and scripts
Scripts in an ASP.NET file are executed on the server
An ASP.NET file has the file extension 7.aspx7
)ow 8oes ASP.NET $or#6
$hen a browser re9uests an )T!* file, the server returns the file
$hen a browser re9uests an ASP.NET file, 22S passes the re9uest to the ASP.NET engine on the
server
The ASP.NET engine reads the file, line by line, and executes the scripts in the file
4inally, the ASP.NET file is returned to the browser as plain )T!*
What is ASP+?
ASP: is the sa"e as ASP.NET.
ASP: is 3ust an early na"e used by !icrosoft when they developed ASP.NET.
The icroso!t .NET "ra#ewor$
The .NET 4ra"ewor# is the infrastructure for the !icrosoft .NET platfor".
1
The .NET 4ra"ewor# is an environ"ent for building, deploying, and running $eb applications and $eb
Services.
!icrosoft's first server technology ASP .Active Server Pages/, was a powerful and flexible 7progra""ing
language7. 'ut it was too code oriented. 2t was not an application fra"ewor# and not an enterprise
develop"ent tool.
The !icrosoft .NET 4ra"ewor# was developed to solve this proble".
.NET "ra#ewor$s $e%words&
Easier and 9uic#er progra""ing
;educed a"ount of code
8eclarative progra""ing "odel
;icher server control hierarchy with events
*arger class library
'etter support for develop"ent tools
The .NET "ra#ewor$ consists o! ' #ain (arts&
Progra""ing languages(
<= .Pronounced < sharp/
-isual 'asic .-' .NET/
,= .Pronounced , sharp/
Server technologies and client technologies(
ASP .NET .Active Server Pages/
$indows 4or"s .$indows des#top solutions/
<o"pact 4ra"ewor# .P8A > !obile solutions/
8evelop"ent environ"ents(
-isual Studio .NET .-S .NET/
-isual $eb 8eveloper
This tutorial is about ASP.NET.
ASP.NET 2.0
ASP.NET 5.1 i"proves upon ASP.NET by adding support for several new features.
%ou can read "ore about the differences between ASP.NET 5.1 and ASP.NET in the next chapter of this
tutorial.
ASP.NET '.0
ASP.NET 0.1 is not a new version of ASP.NET. 2t's 3ust the na"e for a new ASP.NET 5.1 fra"ewor# library
with support for $indows Presentation 4oundation, $indows <o""unication 4oundation, $indows
$or#flow 4oundation? and $indows <ardSpace.
ASP.NET 0.1 is not covered in this tutorial.
ASP.NET )s. ASP

ASP.NET has better language support, a large set of new controls, +!*based co"ponents, and better user
authentication.
ASP.NET provides increased perfor"ance by running co"piled code.
ASP.NET code is not fully bac#ward co"patible with ASP.
New in ASP.NET
'etter language support
Progra""able controls
Eventdriven progra""ing
2
+!*based co"ponents
@ser authentication, with accounts and roles
)igher scalability
2ncreased perfor"ance <o"piled code
Easier configuration and deploy"ent
Not fully ASP co"patible
*anguage Support
ASP.NET uses A*O.NET.
ASP.NET supports full -isual 'asic, not -'Script.
ASP.NET supports <= .< sharp/ and <::.
ASP.NET supports ,Script.
ASP.NET <ontrols
ASP.NET contains a large set of )T!* controls. Al"ost all )T!* ele"ents on a page can be
defined as ASP.NET control ob3ects that can be controlled by scripts.
ASP.NET also contains a new set of ob3ectoriented input controls, li#e progra""able listboxes
and validation controls.
A new data grid control supports sorting, data paging, and everything you can expect fro" a
dataset control.
Event Aware <ontrols
All ASP.NET ob3ects on a $eb page can expose events that can be processed by ASP.NET code.
*oad, <lic# and <hange events handled by code "a#es coding "uch si"pler and "uch better
organiAed.
ASP.NET Co#(onents
ASP.NET co"ponents are heavily based on +!*. *i#e the new A8 ;otator, that uses +!* to store
advertise"ent infor"ation and configuration.
+ser Authentication
ASP.NET supports for"based user authentication, coo#ie "anage"ent, and auto"atic redirecting of
unauthoriAed logins.
+ser Accounts and ,oles
ASP.NET allows user accounts and roles, to give each user .with a given role/ access to different server
code and executables.
-i.h Scala/ilit%
!uch has been done with ASP.NET to provide greater scalability.
Servertoserver co""unication has been greatly enhanced, "a#ing it possible to scale an application over
several servers. Bne exa"ple of this is the ability to run +!* parsers, +S* transfor"ations and even
resource hungry session ob3ects on other servers.
Co#(iled Code
The first re9uest for an ASP.NET page on the server will co"pile the ASP.NET code and #eep a cached
copy in "e"ory. The result of this is greatly increased perfor"ance.
Eas% Con!i.uration
<onfiguration of ASP.NET is done with plain text files.
<onfiguration files can be uploaded or changed while the application is running. No need to restart the
server. No "ore "etabase or registry puAAle.
Eas% *e(lo%#ent
No "ore serverrestart to deploy or replace co"piled code. ASP.NET si"ply redirects all new re9uests to
the new code.
Co#(ati/ilit%
ASP.NET is not fully co"patible with earlier versions of ASP, so "ost of the old ASP code will need so"e
changes to run under ASP.NET.
To overco"e this proble", ASP.NET uses a new file extension 7.aspx7. This will "a#e ASP.NET applications
able to run side by side with standard ASP applications on the sa"e server.
3
ASP.NET Installin.

ASP.NET is easy to install. ,ust follow the instructions below.
What 0ou Need
A Windows Co#(uter
ASP.NET is a !icrosoft technology. To run ASP.NET you need a co"puter capable of running $indows.
$indows 5111 or +P
2f you are serious about developing ASP.NET applications you should install $indows 5111 Professional or
$indows +P Professional.
2n both cases, "a#e sure you install the 2nternet 2nfor"ation Services .22S/ fro" the Add>;e"ove $indows
co"ponents dialog.
Ser)ice Pac$s and +(dates
'efore ASP.NET can be installed on your co"puter, it is necessary to have all relevant service pac#s and
security updates installed.
The easiest way to do this is to activate your $indows 2nternet @pdate. $hen you access the $indows
@pdate page, you will be instructed to install the latest service pac#s and all critical security updates. 4or
$indows 5111, "a#e sure you install Service Pac# 5. 2 will also reco""end that you install 2nternet Explorer
C.
,e#o)e 0our 1eta 2ersion
2f you have a 'eta version of ASP.NET installed, we reco""end that you co"pletely uninstall it. Br even
better( start with a fresh $indows 5111 or +P installation.
Install .NET
4ro" your $indows @pdate you can now select to install the !icrosoft .NET 4ra"ewor#.
After download, the .NET fra"ewor# will install itself on your co"puter there are no options to select for
installation.
%ou should now be ready to develop your first ASP.NET applicationD
The .NET So!tware *e)elo(#ent 3it
2f you have the necessary bandwidth to download over E01 !', you "ight consider downloading the full
!icrosoft .NET Software 8evelop"ent &it .S8&/.
$e fully reco""end getting the S8& for learning "ore about .NET and for the docu"entation, sa"ples, and
tools included.
Connection S(eed and *ownload Ti#e
2f you have a slow 2nternet connection, you "ight have proble"s downloading large files li#e the service
pac#s, the S8& and the latest version of 2nternet Explorer.
2f download speed is a proble", our best suggestion is to get the latest files fro" so"eone else, fro" a
colleague, fro" a friend, or fro" one of the <8s that co"es with "any popular co"puter "agaAines. *oo#
for $indows 5111 Service Pac# 5, 2nternet Explorer C, and the !icrosoft .NET 4ra"ewor#.
ASP.NET - We/ Pa.es

A si"ple ASP.NET page loo#s 3ust li#e an ordinary )T!* page.
To start learning ASP.NET, we will construct a very si"ple )T!* page that will display 7)ello $0Schools7 in
an 2nternet browser li#e this()ello $0SchoolsD
)ello $0Schools in )T!*
This code dis(la%s the e4a#(le as an -T5 (a.e&
Fht"lG
Fbody bgcolorH7yellow7G
FcenterG
4
Fh5G)ello $0SchoolsDF>h5G
F>centerG
F>bodyG
F>ht"lG
2f you want to try it yourself, save the code in a file called 7firstpage.ht"7, and create a lin# to the file li#e this(
firstpage.ht"
)ello $0Schools in ASP.NET
The si"plest way to convert an )T!* page into an ASP.NET page is to copy the )T!* file to a new file with
an .aspx extension.
This code dis(la%s our e4a#(le as an ASP.NET (a.e&
Fht"lG
Fbody bgcolorH7yellow7G
FcenterG
Fh5G)ello $0SchoolsDF>h5G
F>centerG
F>bodyG
F>ht"lG
2f you want to try it yourself, save the code in a file called 7firstpage.aspx7, and create a lin# to the file li#e
this(
firstpage.aspx
-ow *oes it Wor$?
4unda"entally an ASP.NET page is 3ust the sa"e as an )T!* page.
An )T!* page has the extension .ht". 2f a browser re9uests an )T!* page fro" the server, the server
sends the page to the browser without any "odifications.
An ASP.NET page has the extension .aspx. 2f a browser re9uests an ASP.NET page, the server processes
any executable code in the page, before the result is sent bac# to the browser.
The ASP.NET page above does not contain any executable code, so nothing is executed. 2n the next
exa"ples we will add so"e executable code to the page to de"onstrate the difference between static )T!*
pages and dyna"ic ASP pages.
Classic ASP
Active Server Pages .ASP/ has been around for several years. $ith ASP, executable code can be placed
inside )T!* pages.
Previous versions of ASP .before ASP .NET/ are often called <lassic ASP.
ASP.NET is not fully co"patible with <lassic ASP, but "ost <lassic ASP pages will wor# fine as ASP.NET
pages, with only "inor changes.
2f you want to learn "ore about <lassic ASP, please visit our ASP Tutorial.
*%na#ic Pa.e in Classic ASP
To de"onstrate how ASP can display pages with dyna"ic content, we have added so"e executable code
.in red/ to the previous exa"ple(Fht"lG
Fbody bgcolorH7yellow7G
FcenterG
Fh5G)ello $0SchoolsDF>h5G
FpGFI;esponse.$rite.now.//IGF>pG
F>centerG
F>bodyG
F>ht"lG
The code inside the FI IG tags is executed on the server.
;esponse.$rite is ASP code for writing so"ething to the )T!* output strea".
Now./ is a function returning the servers current date and ti"e.
2f you want to try it yourself, save the code in a file called 7dynpage.asp7, and create a lin# to the file li#e this(
dynpage.asp
*%na#ic Pa.e in ASP .NET
This following code displays our exa"ple as an ASP.NET page(
Fht"lG
Fbody bgcolorH7yellow7G
5
FcenterG
Fh5G)ello $0SchoolsDF>h5G
FpGFI;esponse.$rite.now.//IGF>pG
F>centerG
F>bodyG
F>ht"lG
2f you want to try it yourself, save the code in a file called 7dynpage.aspx7, and create a lin# to the file li#e
this( dynpage.aspx
ASP.NET )s Classic ASP
The previous exa"ples didn't de"onstrate any differences between ASP.NET and <lassic ASP.
As you can see fro" the two latest exa"ples there are no differences between the two ASP and ASP.NET
pages.
2n the next chapters you will see how server controls "a#e ASP.NET "ore powerful than <lassic ASP.
ASP.NET - Ser)er Controls

Server controls are tags that are understood by the server.
5i#itations in Classic ASP
The listing below was copied fro" the previous chapter(
Fht"lG
Fbody bgcolorH7yellow7G
FcenterG
Fh5G)ello $0SchoolsDF>h5G
FpGFI;esponse.$rite.now.//IGF>pG
F>centerG
F>bodyG
F>ht"lG
The code above illustrates a li"itation in <lassic ASP( The code bloc# has to be placed where you want the
output to appear.
$ith <lassic ASP it is i"possible to separate executable code fro" the )T!* itself. This "a#es the page
difficult to read, and difficult to "aintain.
ASP.NET - Ser)er Controls
ASP.NET has solved the 7spaghetticode7 proble" described above with server controls.
Server controls are tags that are understood by the server.
There are three $inds o! ser)er controls&
)T!* Server <ontrols Traditional )T!* tags
$eb Server <ontrols New ASP.NET tags
-alidation Server <ontrols 4or input validation
ASP.NET - -T5 Ser)er Controls
)T!* server controls are )T!* tags understood by the server.
)T!* ele"ents in ASP.NET files are, by default, treated as text. To "a#e these ele"ents progra""able,
add a runatH7server7 attribute to the )T!* ele"ent. This attribute indicates that the ele"ent should be
treated as a server control. The id attribute is added to identify the server control. The id reference can be
used to "anipulate the server control at run ti"e.
Note: All HTML server controls must be within a <form> tag with the runat="server" attribute. The
runat="server" attribute inicates that the form shoul be !rocesse on the server. "t also inicates that the
enclose controls can be accesse b# server scri!ts.
2n the following exa"ple we declare an )t"lAnchor server control in an .aspx file. Then we "anipulate the
);ef attribute of the )t"lAnchor control in an event handler .an event handler is a subroutine that executes
6
code for a given event/. The PageJ*oad event is one of "any events that ASP.NET understands(Fscript
runatH7server7G
Sub PageJ*oad
lin#E.);efH7http(>>www.w0schools.co"7
End Sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fa idH7lin#E7 runatH7server7G-isit $0SchoolsDF>aG
F>for"G
F>bodyG
F>ht"lG
The executable code itself has been "oved outside the )T!*.
ASP.NET $eb Server <ontrols
$eb server controls are special ASP.NET tags understood by the server.
*i#e )T!* server controls, $eb server controls are also created on the server and they re9uire a
runatH7server7 attribute to wor#. )owever, $eb server controls do not necessarily "ap to any existing )T!*
ele"ents and they "ay represent "ore co"plex ele"ents.
The syntax for creating a $eb server control is(Fasp(controlJna"e idH7so"eJid7 runatH7server7 >G
2n the following exa"ple we declare a 'utton server control in an .aspx file. Then we create an event
handler for the <lic# event which changes the text on the button(
Fscript runatH7server7G
Sub sub"it.Source As Bb3ect, e As EventArgs/
buttonE.TextH7%ou clic#ed "eD7
End Sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp('utton idH7buttonE7 TextH7<lic# "eD7
runatH7server7 Bn<lic#H7sub"it7>G
F>for"G
F>bodyG
F>ht"lG
ASP.NET - 2alidation Ser)er Controls
-alidation server controls are used to validate userinput. 2f the userinput does not pass validation, it will
display an error "essage to the user.
Each validation control perfor"s a specific type of validation .li#e validating against a specific value or a
range of values/.
'y default, page validation is perfor"ed when a 'utton, 2"age'utton, or *in#'utton control is clic#ed. %ou
can prevent validation when a button control is clic#ed by setting the <auses-alidation property to false.
The syntax for creating a -alidation server control is(Fasp(controlJna"e idH7so"eJid7 runatH7server7 >G
2n the following exa"ple we declare one Text'ox control, one 'utton control, and one ;ange-alidator
control in an .aspx file. 2f validation fails, the text 7The value "ust be fro" E to E11D7 will be displayed in the
,an.e2alidator control&
E4a#(le
Fht"lG
FbodyG
Ffor" runatH7server7G
FpGEnter a nu"ber fro" E to E11(
7
Fasp(Text'ox idH7tboxE7 runatH7server7 >G
Fbr >GFbr >G
Fasp('utton TextH7Sub"it7 runatH7server7 >G
F>pG
FpG
Fasp(;ange-alidator
<ontrolTo-alidateH7tboxE7
!ini"u"-alueH7E7
!axi"u"-alueH7E117
TypeH72nteger7
TextH7The value "ust be fro" E to E11D7
runatH7server7 >G
F>pG
F>for"G
F>bodyGF>ht"lG
ASP.NET - E)ents
An Event )andler is a subroutine that executes code for a given event.
ASP.NET - E)ent -andlers
*oo# at the following code(FI
lblE.TextH7The date and ti"e is 7 K now./
IG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fh0GFasp(label idH7lblE7 runatH7server7 >GF>h0G
F>for"G
F>bodyG
F>ht"lG
$hen will the code above be executed6 The answer is( 7%ou don't #now...7
The Pa.e65oad E)ent
The PageJ*oad event is one of "any events that ASP.NET understands. The PageJ*oad event is triggered
when a page loads, and ASP.NET will auto"atically call the subroutine PageJ*oad, and execute the code
inside it(Exa"pleFscript runatH7server7G
Sub PageJ*oad
lblE.TextH7The date and ti"e is 7 K now./
End Sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fh0GFasp(label idH7lblE7 runatH7server7 >GF>h0G
F>for"G
F>bodyG
F>ht"lG
Note: The $age%Loa event contains no ob&ect references or event arguments'
The Pa.e.IsPost1ac$ Pro(ert%
The PageJ*oad subroutine runs E-E;% ti"e the page is loaded. 2f you want to execute the code in the
PageJ*oad subroutine only the 42;ST ti"e the page is loaded, you can use the Page.2sPost'ac# property.
2f the Page.2sPost'ac# property is false, the page is loaded for the first ti"e, if it is true, the page is posted
bac# to the server .i.e. fro" a button clic# on a for"/(Exa"pleFscript runatH7server7G
Sub PageJ*oad
if Not Page.2sPost'ac# then
lblE.TextH7The date and ti"e is 7 K now./
end if
End Sub
8
Sub sub"it.s As Bb3ect, e As EventArgs/
lbl5.TextH7)ello $orldD7
End Sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fh0GFasp(label idH7lblE7 runatH7server7 >GF>h0G
Fh0GFasp(label idH7lbl57 runatH7server7 >GF>h0G
Fasp(button textH7Sub"it7 onclic#H7sub"it7 runatH7server7 >G
F>for"G
F>bodyG
F>ht"lG
The exa"ple above will write the 7The date and ti"e is....7 "essage only the first ti"e the page is loaded.
$hen a user clic#s on the Sub"it button, the sub"it subroutine will write 7)ello $orldD7 to the second label,
but the date and ti"e in the first label will not change.
ASP.NET We/ "or#s
All server controls "ust appear within a Ffor"G tag, and the Ffor"G tag "ust contain the runatH7server7
attribute.
ASP.NET We/ "or#s
All server controls "ust appear within a Ffor"G tag, and the Ffor"G tag "ust contain the runatH7server7
attribute. The runatH7server7 attribute indicates that the for" should be processed on the server. 2t also
indicates that the enclosed controls can be accessed by server scripts(Ffor" runatH7server7G
...)T!* : server controls
F>for"G
Note: The form is alwa#s submitte to the !age itself. "f #ou s!ecif# an action attribute( it is ignore. "f #ou
omit the metho attribute( it will be set to metho="!ost" b# efault. Also( if #ou o not s!ecif# the name an
i attributes( the# are automaticall# assigne b# A)$.N*T.
Note: An .as!+ !age can onl# contain ,N* <form runat="server"> control'
2f you select view source in an .aspx page containing a for" with no na"e, "ethod, action, or id attribute
specified, you will see that ASP.NET has added these attributes to the for". 2t loo#s so"ething li#e
this(Ffor" na"eH7Jctl17 "ethodH7post7 actionH7page.aspx7 idH7Jctl17G
...so"e code
F>for"G
Su/#ittin. a "or#
A for" is "ost often sub"itted by clic#ing on a button. The 'utton server control in ASP.NET has the
following for"at(Fasp('utton idH7id7 textH7label7 Bn<lic#H7sub7 runatH7server7 >G
The id attribute defines a uni9ue na"e for the button and the text attribute assigns a label to the button. The
on<lic# event handler specifies a na"ed subroutine to execute.
2n the following exa"ple we declare a 'utton control in an .aspx file. A button clic# runs a subroutine which
changes the text on the button(
E4a#(le&
Fscript runatH7server7G
Sub sub"it.Source As Bb3ect, e As EventArgs/
buttonE.TextH7%ou clic#ed "eD7
End Sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp('utton idH7buttonE7 TextH7<lic# "eD7 runatH7server7 Bn<lic#H7sub"it7 >G
F>for"G
F>bodyG
9
F>ht"lG
ASP .NET aintainin. the 2iewState
%ou "ay save a lot of coding by "aintaining the -iewState of the ob3ects in your $eb 4or".
aintainin. the 2iewState
$hen a for" is sub"itted in classic ASP, all for" values are cleared. Suppose you have sub"itted a for"
with a lot of infor"ation and the server co"es bac# with an error. %ou will have to go bac# to the for" and
correct the infor"ation. %ou clic# the bac# button, and what happens.......A** for" values are <*EA;E8,
and you will have to start all over againD The site did not "aintain your -iewState.
$hen a for" is sub"itted in ASP .NET, the for" reappears in the browser window together with all for"
values. )ow co"e6 This is because ASP .NET "aintains your -iewState. The -iewState indicates the
status of the page when sub"itted to the server. The status is defined through a hidden field placed on each
page with a Ffor" runatH7server7G control. The source could loo# so"ething li#e this(Ffor" na"eH7Jctl17
"ethodH7post7 actionH7page.aspx7 idH7Jctl17G
Finput typeH7hidden7 na"eH7JJ-2E$STATE7
valueH7d8wtNT21B8@L!8EEBAs:M'<45ry3!pe-g@r%5eT3NO)NlPQH7 >G
.....so"e code
F>for"G
!aintaining the -iewState is the default setting for ASP.NET $eb 4or"s. 2f you want to NBT "aintain the
-iewState, include the directive
FIR Page Enable-iewStateH7false7 IG at the top of an .aspx page or add the attribute
Enable-iewStateH7false7 to any control.
*oo# at the following .aspx file. 2t de"onstrates the 7old7 way to do it. $hen you clic# on the sub"it button,
the for" value will disappear(
E4a#(le
Fht"lG
FbodyG
Ffor" actionH7de"oJclassicasp.aspx7 "ethodH7post7G
%our na"e( Finput typeH7text7 na"eH7fna"e7 siAeH7517G
Finput typeH7sub"it7 valueH7Sub"it7G
F>for"G
FI
di" fna"e
fna"eH;e9uest.4or".7fna"e7/
2f fna"eFG77 Then
;esponse.$rite.7)ello 7 K fna"e K 7D7/
End 2f
IG
F>bodyG
F>ht"lG
)ere is the new ASP .NET way. $hen you clic# on the sub"it button, the for" value will NBT
disappear(Exa"ple
<lic# view source in the right fra"e of the exa"ple to see that ASP .NET has added a hidden field in the
for" to "aintain the -iewStateFscript runatH7server7G
Sub sub"it.sender As Bb3ect, e As EventArgs/
lblE.TextH7)ello 7 K txtE.Text K 7D7
End Sub
F>scriptG
Fht"lG
FbodyG
10
Ffor" runatH7server7G
%our na"e( Fasp(Text'ox idH7txtE7 runatH7server7 >G
Fasp('utton Bn<lic#H7sub"it7 TextH7Sub"it7 runatH7server7 >G
FpGFasp(*abel idH7lblE7 runatH7server7 >GF>pG
F>for"G
F>bodyG
F>ht"lG
ASP .NET - The Te4t1o4 Control
The Text'ox control is used to create a text box where the user can input text.
The Te4t1o4 Control
The Text'ox control is used to create a text box where the user can input text.
The Text'ox control's attributes and properties are listed in our web controls reference page.
The exa"ple below de"onstrates so"e of the attributes you "ay use with the Text'ox control(
E4a#(le
Fht"lG
FbodyG
Ffor" runatH7server7G
A /asic Te4t1o4&
Fasp(Text'ox idH7tbE7 runatH7server7 >G
Fbr >GFbr >G
A (assword Te4t1o4&
Fasp(Text'ox idH7tb57 Text!odeH7password7 runatH7server7 >G
Fbr >GFbr >G
A Te4t1o4 with te4t&
Fasp(Text'ox idH7tbP7 TextH7)ello $orldD7 runatH7server7 >G
Fbr >GFbr >G
A #ultiline Te4t1o4&
Fasp(Text'ox idH7tb07 Text!odeH7"ultiline7 runatH7server7 >G
Fbr >GFbr >G
A Te4t1o4 with hei.ht&
Fasp(Text'ox idH7tbC7 rowsH7L7 Text!odeH7"ultiline7
runatH7server7 >G
Fbr >GFbr >G
A Te4t1o4 with width&
Fasp(Text'ox idH7tbL7 colu"nsH7017 runatH7server7 >G
F>for"G
F>bodyG
F>ht"lG
Add a Scri(t
The contents and settings of a Text'ox control "ay be changed by server scripts when a for" is sub"itted.
A for" can be sub"itted by clic#ing on a button or when a user changes the value in the Text'ox control.
2n the following exa"ple we declare one Text'ox control, one 'utton control, and one *abel control in an
.aspx file. $hen the sub"it button is triggered, the sub"it subroutine is executed. The sub"it subroutine
writes a text to the *abel control(
E4a#(le
Fscript runatH7server7G
Sub sub"it.sender As Bb3ect, e As EventArgs/
lblE.TextH7%our na"e is 7 K txtE.Text
End Sub
F>scriptG
11
Fht"lG
FbodyG
Ffor" runatH7server7G
Enter your na"e(
Fasp(Text'ox idH7txtE7 runatH7server7 >G
Fasp('utton Bn<lic#H7sub"it7 TextH7Sub"it7 runatH7server7 >G
FpGFasp(*abel idH7lblE7 runatH7server7 >GF>pG
F>for"G
F>bodyG
F>ht"lG
2n the following exa"ple we declare one Text'ox control and one *abel control in an .aspx file. $hen you
change the value in the Text'ox and either clic# outside the Text'ox or press the Tab #ey, the change
subroutine is executed. The sub"it subroutine writes a text to the *abel control
E4a#(le
Fscript runatH7server7G
Sub change.sender As Bb3ect, e As EventArgs/
lblE.TextH7%ou changed text to 7 K txtE.Text
End Sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Enter your na"e(
Fasp(Text'ox idH7txtE7 runatH7server7
textH7)ello $orldD7
ontextchangedH7change7 autopostbac#H7true7>G
FpGFasp(*abel idH7lblE7 runatH7server7 >GF>pG
F>for"G
F>bodyG
F>ht"lG
ASP.NET - The 1utton Control
The 'utton control is used to display a push button.
The 1utton Control
The 'utton control is used to display a push button. The push button "ay be a sub"it button or a co""and
button. 'y default, this control is a sub"it button.
A sub"it button does not have a co""and na"e and it posts the page bac# to the server when it is clic#ed.
2t is possible to write an event handler to control the actions perfor"ed when the sub"it button is clic#ed.
A co""and button has a co""and na"e and allows you to create "ultiple 'utton controls on a page. 2t is
possible to write an event handler to control the actions perfor"ed when the co""and button is clic#ed.
The 'utton control's attributes and properties are listed in our web controls reference page.
The e4a#(le /elow de#onstrates a si#(le 1utton control&
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp('utton idH7bE7 TextH7Sub"it7 runatH7server7 >G
F>for"G
F>bodyG
F>ht"lG
Add a Scri(t
A for" is "ost often sub"itted by clic#ing on a button.
2n the following exa"ple we declare one Text'ox control, one 'utton control, and one *abel control in an
.aspx file. $hen the sub"it button is triggered, the sub"it subroutine is executed. The sub"it subroutine
writes a text to the *abel control(
E4a#(le
12
Fscript runatH7server7G
Sub sub"it.sender As Bb3ect, e As EventArgs/
lblE.TextH7%our na"e is 7 K txtE.Text
End Sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Enter your na"e(
Fasp(Text'ox idH7txtE7 runatH7server7 >G
Fasp('utton Bn<lic#H7sub"it7 TextH7Sub"it7 runatH7server7 >G
FpGFasp(*abel idH7lblE7 runatH7server7 >GF>pG
F>for"G
F>bodyG
F>ht"lG
ASP.NET - *ata 1indin.
$e "ay use data binding to fill lists with selectable ite"s fro" an i"ported data source, li#e a database, an
+!* file, or a script.
*ata 1indin.
The following controls are list controls which support data binding(
asp(;adio'utton*ist
asp(<hec#'ox*ist
asp(8rop8own*ist
asp(*istbox
The selecta/le ite#s in each o! the a/o)e controls are usuall% de!ined /% one or #ore as(&5istIte#
controls7 li$e this&
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(;adio'utton*ist idH7countrylist7 runatH7server7G
Fasp(*ist2te" valueH7N7 textH7Norway7 >G
Fasp(*ist2te" valueH7S7 textH7Sweden7 >G
Fasp(*ist2te" valueH747 textH74rance7 >G
Fasp(*ist2te" valueH727 textH72taly7 >G
F>asp(;adio'utton*istG
F>for"G
F>bodyG
F>ht"lG
)owever, with data binding we "ay use a separate source, li#e a database, an +!* file, or a script to fill the
list with selectable ite"s.
'y using an i"ported source, the data is separated fro" the )T!*, and any changes to the ite"s are "ade
in the separate data source.
2n the next three chapters, we will describe how to bind data fro" a scripted data source.
ASP.NET - The Arra%5ist O/8ect
Array*ist 8rop8own*ist
Array*ist ;adio'utton*ist
Create an Arra%5ist
The Array*ist ob3ect is a collection of ite"s containing a single data value.
2te"s are added to the Array*ist with the Add./ "ethod.
The following code creates a new Array*ist ob3ect na"ed "ycountries and four ite"s are added(
Fscript runatH7server7G
Sub PageJ*oad
13
if Not Page.2sPost'ac# then
di" "ycountriesHNew Array*ist
"ycountries.Add.7Norway7/
"ycountries.Add.7Sweden7/
"ycountries.Add.74rance7/
"ycountries.Add.72taly7/
end if
end sub
F>scriptG
1% de!ault7 an Arra%5ist o/8ect contains 9: entries. An Arra%5ist can /e si;ed to its !inal si;e with the
Tri#ToSi;e<= #ethod&
Fscript runatH7server7G
Sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycountriesHNew Array*ist
"ycountries.Add.7Norway7/
"ycountries.Add.7Sweden7/
"ycountries.Add.74rance7/
"ycountries.Add.72taly7/
"ycountries.Tri"ToSiAe./
end if
end sub
F>scriptG
An Arra%5ist can also /e sorted al(ha/eticall% or nu#ericall% with the Sort<= #ethod&
Fscript runatH7server7G
Sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycountriesHNew Array*ist
"ycountries.Add.7Norway7/
"ycountries.Add.7Sweden7/
"ycountries.Add.74rance7/
"ycountries.Add.72taly7/
"ycountries.Tri"ToSiAe./
"ycountries.Sort./
end if
end sub
F>scriptG
To sort in re)erse order7 a((l% the ,e)erse<= #ethod a!ter the Sort<= #ethod&
Fscript runatH7server7G
Sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycountriesHNew Array*ist
"ycountries.Add.7Norway7/
"ycountries.Add.7Sweden7/
"ycountries.Add.74rance7/
"ycountries.Add.72taly7/
"ycountries.Tri"ToSiAe./
"ycountries.Sort./
"ycountries.;everse./
end if
end sub
F>scriptG
*ata 1indin. to an Arra%5ist
An Array*ist ob3ect "ay auto"atically generate the text and values to the following controls(
asp(;adio'utton*ist
asp(<hec#'ox*ist
asp(8rop8own*ist
14
asp(*istbox
To bind data to a ;adio'utton*ist control, first create a ;adio'utton*ist control .without any asp(*ist2te"
ele"ents/ in an .aspx page(Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(;adio'utton*ist idH7rb7 runatH7server7 >G
F>for"G
F>bodyG
F>ht"lG
Then add the script that builds the list and binds the values in the list to the ;adio'utton*ist control(
E4a#(le
Fscript runatH7server7G
Sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycountriesHNew Array*ist
"ycountries.Add.7Norway7/
"ycountries.Add.7Sweden7/
"ycountries.Add.74rance7/
"ycountries.Add.72taly7/
"ycountries.Tri"ToSiAe./
"ycountries.Sort./
rb.8ataSourceH"ycountries
rb.8ata'ind./
end if
end sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(;adio'utton*ist idH7rb7 runatH7server7 >G
F>for"G
F>bodyG
F>ht"lG
The 8ataSource property of the ;adio'utton*ist control is set to the Array*ist and it defines the data source
of the ;adio'utton*ist control. The 8ata'ind./ "ethod of the ;adio'utton*ist control binds the data source
with the ;adio'utton*ist control.
Note: The ata values are use as both the Te+t an -alue !ro!erties for the control. To a -alues that are
ifferent from the Te+t( use either the Hashtable ob&ect or the )orteList ob&ect.
ASP.NET - The -ashta/le O/8ect
The )ashtable ob3ect contains ite"s in #ey>value pairs.
)ashtable ;adiobutton*ist E
)ashtable ;adiobutton*ist 5
)ashtable 8rop8own*ist
Create a -ashta/le
The )ashtable ob3ect contains ite"s in #ey>value pairs. The #eys are used as indexes, and very 9uic#
searches can be "ade for values by searching through their #eys.
2te"s are added to the )ashtable with the Add./ "ethod.
The following code creates a )ashtable na"ed "ycountries and four ele"ents are added(Fscript
runatH7server7G
Sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycountriesHNew )ashtable
"ycountries.Add.7N7,7Norway7/
15
"ycountries.Add.7S7,7Sweden7/
"ycountries.Add.747,74rance7/
"ycountries.Add.727,72taly7/
end if
end sub
F>scriptG
*ata 1indin.
A )ashtable ob3ect "ay auto"atically generate the text and values to the following controls(
asp(;adio'utton*ist
asp(<hec#'ox*ist
asp(8rop8own*ist
asp(*istbox
To /ind data to a ,adio1utton5ist control7 !irst create a ,adio1utton5ist control <without an%
as(&5istIte# ele#ents= in an .as(4 (a.e&
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(;adio'utton*ist idH7rb7 runatH7server7 AutoPost'ac#H7True7 >G
F>for"G
F>bodyG
F>ht"lG
Then add the scri(t that /uilds the list&
Fscript runatH7server7G
sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycountriesHNew )ashtable
"ycountries.Add.7N7,7Norway7/
"ycountries.Add.7S7,7Sweden7/
"ycountries.Add.747,74rance7/
"ycountries.Add.727,72taly7/
rb.8ataSourceH"ycountries
rb.8ata-alue4ieldH7&ey7
rb.8ataText4ieldH7-alue7
rb.8ata'ind./
end if
end sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(;adio'utton*ist idH7rb7 runatH7server7 AutoPost'ac#H7True7 >G
F>for"G
F>bodyGF>ht"lG
Then we add a sub routine to be executed when the user clic#s on an ite" in the ;adio'utton*ist control.
$hen a radio button is clic#ed, a text will appear in a label(
E4a#(le
Fscript runatH7server7G
sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycountriesHNew )ashtable
"ycountries.Add.7N7,7Norway7/
"ycountries.Add.7S7,7Sweden7/
"ycountries.Add.747,74rance7/
"ycountries.Add.727,72taly7/
rb.8ataSourceH"ycountries
rb.8ata-alue4ieldH7&ey7
rb.8ataText4ieldH7-alue7
16
rb.8ata'ind./
end if
end sub
sub display!essage.s as Bb3ect,e As EventArgs/
lblE.textH7%our favorite country is( 7 K rb.Selected2te".Text
end sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(;adio'utton*ist idH7rb7 runatH7server7
AutoPost'ac#H7True7 onSelected2ndex<hangedH7display!essage7 >G
FpGFasp(label idH7lblE7 runatH7server7 >GF>pG
F>for"G
F>bodyGF>ht"lG
Note: .ou cannot choose the sort orer of the items ae to the Hashtable. To sort items al!habeticall# or
numericall#( use the )orteList ob&ect.
ASP.NET - The Sorted5ist O/8ect
The Sorted*ist ob3ect co"bines the features of both the Array*ist ob3ect and the )ashtable ob3ect.
Sorted*ist ;adiobutton*ist E
Sorted*ist ;adiobutton*ist 5
Sorted*ist 8rop8own*ist
The Sorted5ist O/8ect
The Sorted*ist ob3ect contains ite"s in #ey>value pairs. A Sorted*ist ob3ect auto"atically sort the ite"s in
alphabetic or nu"eric order.
2te"s are added to the Sorted*ist with the Add./ "ethod. A Sorted*ist can be siAed to its final siAe with the
Tri"ToSiAe./ "ethod.
The !ollowin. code creates a Sorted5ist na#ed #%countries and !our ele#ents are added&
Fscript runatH7server7G
sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycountriesHNew Sorted*ist
"ycountries.Add.7N7,7Norway7/
"ycountries.Add.7S7,7Sweden7/
"ycountries.Add.747,74rance7/
"ycountries.Add.727,72taly7/
end if
end sub
F>scriptG
*ata 1indin.
A Sorted*ist ob3ect "ay auto"atically generate the text and values to the following controls(
asp(;adio'utton*ist
asp(<hec#'ox*ist
asp(8rop8own*ist
asp(*istbox
To bind data to a ;adio'utton*ist control, first create a ;adio'utton*ist control .without any asp(*ist2te"
ele"ents/ in an .aspx page(
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(;adio'utton*ist idH7rb7 runatH7server7 AutoPost'ac#H7True7 >G
F>for"G
17
F>bodyG
F>ht"lG
Then add the scri(t that /uilds the list&
Fscript runatH7server7G
sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycountriesHNew Sorted*ist
"ycountries.Add.7N7,7Norway7/
"ycountries.Add.7S7,7Sweden7/
"ycountries.Add.747,74rance7/
"ycountries.Add.727,72taly7/
rb.8ataSourceH"ycountries
rb.8ata-alue4ieldH7&ey7
rb.8ataText4ieldH7-alue7
rb.8ata'ind./
end if
end sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(;adio'utton*ist idH7rb7 runatH7server7 AutoPost'ac#H7True7 >G
F>for"G
F>bodyG
F>ht"lG
Then we add a sub routine to be executed when the user clic#s on an ite" in the ;adio'utton*ist control.
$hen a radio button is clic#ed, a text will appear in a label(
E4a#(le
Fscript runatH7server7G
sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycountriesHNew Sorted*ist
"ycountries.Add.7N7,7Norway7/
"ycountries.Add.7S7,7Sweden7/
"ycountries.Add.747,74rance7/
"ycountries.Add.727,72taly7/
rb.8ataSourceH"ycountries
rb.8ata-alue4ieldH7&ey7
rb.8ataText4ieldH7-alue7
rb.8ata'ind./
end if
end sub
sub display!essage.s as Bb3ect,e As EventArgs/
lblE.textH7%our favorite country is( 7 K rb.Selected2te".Text
end sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(;adio'utton*ist idH7rb7 runatH7server7
AutoPost'ac#H7True7 onSelected2ndex<hangedH7display!essage7 >G
FpGFasp(label idH7lblE7 runatH7server7 >GF>pG
F>for"G
F>bodyG
F>ht"lG
18
ASP .NET - >5 "iles
$e can bind an +!* file to a list control.
An >5 "ile
-ere is an >5 !ile na#ed ?countries.4#l?&
F6x"l versionH7E.17 encodingH72SBSSLOE76G
FcountriesG
FcountryG
FtextGNorwayF>textG
FvalueGNF>valueG
F>countryG
FcountryG
FtextGSwedenF>textG
FvalueGSF>valueG
F>countryG
FcountryG
FtextG4ranceF>textG
FvalueG4F>valueG
F>countryG
FcountryG
FtextG2talyF>textG
FvalueG2F>valueG
F>countryG
F>countriesG
1ind a *ataSet to a 5ist Control
4irst, i"port the 7Syste".8ata7 na"espace. $e need this na"espace to wor# with 8ataSet ob3ects. 2nclude
the following directive at the top of an .aspx page(FIR 2"port Na"espaceH7Syste".8ata7 IG
Next, create a 8ataSet for the +!* file and load the +!* file into the 8ataSet when the page is first
loaded(Fscript runatH7server7G
sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycountriesHNew 8ataSet
"ycountries.;ead+"l.!apPath.7countries.x"l7//
end if
end sub
To /ind the *ataSet to a ,adio1utton5ist control7 !irst create a ,adio1utton5ist control <without an%
as(&5istIte# ele#ents= in an .as(4 (a.e&
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(;adio'utton*ist idH7rb7 runatH7server7 AutoPost'ac#H7True7 >G
F>for"G
F>bodyGF>ht"lG
Then add the scri(t that /uilds the >5 *ataSet&
FIR 2"port Na"espaceH7Syste".8ata7 IG
Fscript runatH7server7G
sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycountriesHNew 8ataSet
"ycountries.;ead+"l.!apPath.7countries.x"l7//
rb.8ataSourceH"ycountries
rb.8ata-alue4ieldH7value7
rb.8ataText4ieldH7text7
rb.8ata'ind./
end if
end sub
F>scriptG
Fht"lG
FbodyG
19
Ffor" runatH7server7G
Fasp(;adio'utton*ist idH7rb7 runatH7server7
AutoPost'ac#H7True7 onSelected2ndex<hangedH7display!essage7 >G
F>for"G
F>bodyG
F>ht"lG
Then we add a sub routine to be executed when the user clic#s on an ite" in the ;adio'utton*ist control.
$hen a radio button is clic#ed, a text will appear in a label(
E4a#(le
FIR 2"port Na"espaceH7Syste".8ata7 IG
Fscript runatH7server7G
sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycountriesHNew 8ataSet
"ycountries.;ead+"l.!apPath.7countries.x"l7//
rb.8ataSourceH"ycountries
rb.8ata-alue4ieldH7value7
rb.8ataText4ieldH7text7
rb.8ata'ind./
end if
end sub
sub display!essage.s as Bb3ect,e As EventArgs/
lblE.textH7%our favorite country is( 7 K rb.Selected2te".Text
end sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(;adio'utton*ist idH7rb7 runatH7server7
AutoPost'ac#H7True7 onSelected2ndex<hangedH7display!essage7 >G
FpGFasp(label idH7lblE7 runatH7server7 >GF>pG
F>for"G
F>bodyG
F>ht"lG
ASP.NET - The ,e(eater Control
The ;epeater control is used to display a repeated list of ite"s that are bound to the control.
1ind a *ataSet to a ,e(eater Control
The ;epeater control is used to display a repeated list of ite"s that are bound to the control. The ;epeater
control "ay be bound to a database table, an +!* file, or another list of ite"s. )ere we will show how to
bind an +!* file to a ;epeater control.
$e will use the following +!* file in our exa"ples .7cdcatalog.x"l7/(
F6x"l versionH7E.17 encodingH72SBSSLOE76G
FcatalogG
FcdG
FtitleGE"pire 'urles9ueF>titleG
FartistG'ob 8ylanF>artistG
FcountryG@SAF>countryG
Fco"panyG<olu"biaF>co"panyG
FpriceGE1.O1F>priceG
FyearGEOSLF>yearG
F>cdG
FcdG
FtitleG)ide your heartF>titleG
FartistG'onnie TylerF>artistG
FcountryG@&F>countryG
20
Fco"panyG<'S ;ecordsF>co"panyG
FpriceGO.O1F>priceG
FyearGEOSSF>yearG
F>cdG
FcdG
FtitleGTreatest )itsF>titleG
FartistG8olly PartonF>artistG
FcountryG@SAF>countryG
Fco"panyG;<AF>co"panyG
FpriceGO.O1F>priceG
FyearGEOS5F>yearG
F>cdG
FcdG
FtitleGStill got the bluesF>titleG
FartistGTary !ooreF>artistG
FcountryG@&F>countryG
Fco"panyG-irgin recordsF>co"panyG
FpriceGE1.51F>priceG
FyearGEOO1F>yearG
F>cdG
FcdG
FtitleGErosF>titleG
FartistGEros ;a"aAAottiF>artistG
FcountryGE@F>countryG
Fco"panyG'!TF>co"panyG
FpriceGO.O1F>priceG
FyearGEOONF>yearG
F>cdG
F>catalogG
4irst, i"port the 7Syste".8ata7 na"espace. $e need this na"espace to wor# with 8ataSet ob3ects. 2nclude
the following directive at the top of an .aspx page(FIR 2"port Na"espaceH7Syste".8ata7 IG
Next, create a 8ataSet for the +!* file and load the +!* file into the 8ataSet when the page is first loaded(
Fscript runatH7server7G
sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycdcatalogHNew 8ataSet
"ycdcatalog.;ead+"l.!apPath.7cdcatalog.x"l7//
end if
end sub
Then we create a ;epeater control in an .aspx page. The contents of the F)eaderTe"plateG ele"ent are
rendered first and only once within the output, then the contents of the F2te"Te"plateG ele"ent are
repeated for each 7record7 in the 8ataSet, and last, the contents of the F4ooterTe"plateG ele"ent are
rendered once within the output(Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(;epeater idH7cdcatalog7 runatH7server7G
F)eaderTe"plateG
...
F>)eaderTe"plateG
F2te"Te"plateG
...
F>2te"Te"plateG
F4ooterTe"plateG
...
F>4ooterTe"plateG
F>asp(;epeaterG
F>for"G
F>bodyG
F>ht"lG
21
Then we add the script that creates the 8ataSet and binds the "ycdcatalog 8ataSet to the ;epeater control.
$e also fill the ;epeater control with )T!* tags and bind the data ite"s to the cells in theF2te"Te"plateG
section with the FI=<ontainer.8ata2te".7fieldna"e7/IG "ethod(
E4a#(le
FIR 2"port Na"espaceH7Syste".8ata7 IG
Fscript runatH7server7G
sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycdcatalogHNew 8ataSet
"ycdcatalog.;ead+"l.!apPath.7cdcatalog.x"l7//
cdcatalog.8ataSourceH"ycdcatalog
cdcatalog.8ata'ind./
end if
end sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(;epeater idH7cdcatalog7 runatH7server7G
F)eaderTe"plateG
Ftable borderH7E7 widthH7E11I7G
FtrG
FthGTitleF>thG
FthGArtistF>thG
FthG<ountryF>thG
FthG<o"panyF>thG
FthGPriceF>thG
FthG%earF>thG
F>trG
F>)eaderTe"plateG
F2te"Te"plateG
FtrG
FtdGFI=<ontainer.8ata2te".7title7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7artist7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7country7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7co"pany7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7price7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7year7/IGF>tdG
F>trG
F>2te"Te"plateG
F4ooterTe"plateG
F>tableG
F>4ooterTe"plateG
F>asp(;epeaterG
F>for"G
F>bodyG
F>ht"lG

+sin. the @Alternatin.Ite#Te#(lateA
%ou can add an FAlternating2te"Te"plateG ele"ent after the F2te"Te"plateG ele"ent to describe the
appearance of alternating rows of output. 2n the following exa"ple each other row in the table will be
displayed in a light grey color(Exa"pleFIR 2"port Na"espaceH7Syste".8ata7 IG
Fscript runatH7server7G
22
sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycdcatalogHNew 8ataSet
"ycdcatalog.;ead+"l.!apPath.7cdcatalog.x"l7//
cdcatalog.8ataSourceH"ycdcatalog
cdcatalog.8ata'ind./
end if
end sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(;epeater idH7cdcatalog7 runatH7server7G
@-eaderTe#(lateA
Ftable borderH7E7 widthH7E11I7G
FtrG
FthGTitleF>thG
FthGArtistF>thG
FthG<ountryF>thG
FthG<o"panyF>thG
FthGPriceF>thG
FthG%earF>thG
F>trG
F>)eaderTe"plateG
@Ite#Te#(lateA
FtrG
FtdGFI=<ontainer.8ata2te".7title7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7artist7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7country7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7co"pany7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7price7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7year7/IGF>tdG
F>trG
F>2te"Te"plateG
@Alternatin.Ite#Te#(lateA
Ftr bgcolorH7=eSeSeS7G
FtdGFI=<ontainer.8ata2te".7title7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7artist7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7country7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7co"pany7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7price7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7year7/IGF>tdG
F>trG
F>Alternating2te"Te"plateG
F4ooterTe"plateG
F>tableG
F>4ooterTe"plateG
F>asp(;epeaterG
F>for"G
F>bodyGF>ht"lG
+sin. the @Se(aratorTe#(lateA
The FSeparatorTe"plateG ele"ent can be used to describe a separator between each record. The following
exa"ple inserts a horiAontal line between each table row(
E4a#(le
FIR 2"port Na"espaceH7Syste".8ata7 IG
Fscript runatH7server7G
23
sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycdcatalogHNew 8ataSet
"ycdcatalog.;ead+"l.!apPath.7cdcatalog.x"l7//
cdcatalog.8ataSourceH"ycdcatalog
cdcatalog.8ata'ind./
end if
end sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(;epeater idH7cdcatalog7 runatH7server7G
F)eaderTe"plateG
Ftable borderH717 widthH7E11I7G
FtrG
FthGTitleF>thG
FthGArtistF>thG
FthG<ountryF>thG
FthG<o"panyF>thG
FthGPriceF>thG
FthG%earF>thG
F>trG
F>)eaderTe"plateG
F2te"Te"plateG
FtrG
FtdGFI=<ontainer.8ata2te".7title7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7artist7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7country7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7co"pany7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7price7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7year7/IGF>tdG
F>trG
F>2te"Te"plateG
FSeparatorTe"plateG
FtrG
Ftd colspanH7C7GFhr >GF>tdG
F>trG
F>SeparatorTe"plateG
F4ooterTe"plateG
F>tableG
F>4ooterTe"plateG
F>asp(;epeaterG
F>for"G
F>bodyG
F>ht"lG
ASP.NET - The *ata5ist Control
The 8ata*ist control is, li#e the ;epeater control, used to display a repeated list of ite"s that are bound to
the control. )owever, the 8ata*ist control adds a table around the data ite"s by default.
1ind a *ataSet to a *ata5ist Control
The 8ata*ist control is, li#e the ;epeater control, used to display a repeated list of ite"s that are bound to
the control. )owever, the 8ata*ist control adds a table around the data ite"s by default. The 8ata*ist
control "ay be bound to a database table, an +!* file, or another list of ite"s. )ere we will show how to
bind an +!* file to a 8ata*ist control.
$e will use the following +!* file in our exa"ples .7cdcatalog.x"l7/(
F6x"l versionH7E.17 encodingH72SBSSLOE76G
FcatalogG
24
FcdG
FtitleGE"pire 'urles9ueF>titleG
FartistG'ob 8ylanF>artistG
FcountryG@SAF>countryG
Fco"panyG<olu"biaF>co"panyG
FpriceGE1.O1F>priceG
FyearGEOSLF>yearG
F>cdG
FcdG
FtitleG)ide your heartF>titleG
FartistG'onnie TylerF>artistG
FcountryG@&F>countryG
Fco"panyG<'S ;ecordsF>co"panyG
FpriceGO.O1F>priceG
FyearGEOSSF>yearG
F>cdG
FcdG
FtitleGTreatest )itsF>titleG
FartistG8olly PartonF>artistG
FcountryG@SAF>countryG
Fco"panyG;<AF>co"panyG
FpriceGO.O1F>priceG
FyearGEOS5F>yearG
F>cdG
FcdG
FtitleGStill got the bluesF>titleG
FartistGTary !ooreF>artistG
FcountryG@&F>countryG
Fco"panyG-irgin recordsF>co"panyG
FpriceGE1.51F>priceG
FyearGEOO1F>yearG
F>cdG
FcdG
FtitleGErosF>titleG
FartistGEros ;a"aAAottiF>artistG
FcountryGE@F>countryG
Fco"panyG'!TF>co"panyG
FpriceGO.O1F>priceG
FyearGEOONF>yearG
F>cdG
F>catalogG
"irst7 i#(ort the ?S%ste#.*ata? na#es(ace. We need this na#es(ace to wor$ with *ataSet o/8ects.
Include the !ollowin. directi)e at the to( o! an .as(4 (a.e&
FIR 2"port Na"espaceH7Syste".8ata7 IG
Next, create a 8ataSet for the +!* file and load the +!* file into the 8ataSet when the page is first
loaded(Fscript runatH7server7G
sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycdcatalogHNew 8ataSet
"ycdcatalog.;ead+"l.!apPath.7cdcatalog.x"l7//
end if
end sub
Then we create a 8ata*ist in an .aspx page. The contents of the F)eaderTe"plateG ele"ent are rendered
first and only once within the output, then the contents of the F2te"Te"plateG ele"ent are repeated for each
7record7 in the 8ataSet, and last, the contents of the F4ooterTe"plateG ele"ent are rendered once within
the output(
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(8ata*ist idH7cdcatalog7 runatH7server7G
F)eaderTe"plateG
25
...
F>)eaderTe"plateG
F2te"Te"plateG
...
F>2te"Te"plateG
F4ooterTe"plateG
...
F>4ooterTe"plateG
F>asp(8ata*istG
F>for"G
F>bodyG
F>ht"lG
Then we add the script that creates the 8ataSet and binds the "ycdcatalog 8ataSet to the 8ata*ist control.
$e also fill the 8ata*ist control with a F)eaderTe"plateG that contains the header of the table, an
F2te"Te"plateG that contains the data ite"s to display, and a F4ooterTe"plateG that contains a text. Note
that the gridlines attribute of the 8ata*ist is set to 7both7 to display table borders(
E4a#(le
FIR 2"port Na"espaceH7Syste".8ata7 IG
Fscript runatH7server7G
sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycdcatalogHNew 8ataSet
"ycdcatalog.;ead+"l.!apPath.7cdcatalog.x"l7//
cdcatalog.8ataSourceH"ycdcatalog
cdcatalog.8ata'ind./
end if
end sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(8ata*ist idH7cdcatalog7
gridlinesH7both7 runatH7server7G
F)eaderTe"plateG
!y <8 <atalog
F>)eaderTe"plateG
F2te"Te"plateG
7FI=<ontainer.8ata2te".7title7/IG7 of
FI=<ontainer.8ata2te".7artist7/IG
UFI=<ontainer.8ata2te".7price7/IG
F>2te"Te"plateG
F4ooterTe"plateG
<opyright )ege ;efsnes
F>4ooterTe"plateG
F>asp(8ata*istG
F>for"G
F>bodyG
F>ht"lG

+sin. St%les
%ou can also add styles to the 8ata*ist control to "a#e the output "ore fancy(
E4a#(le
FIR 2"port Na"espaceH7Syste".8ata7 IG
Fscript runatH7server7G
26
sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycdcatalogHNew 8ataSet
"ycdcatalog.;ead+"l.!apPath.7cdcatalog.x"l7//
cdcatalog.8ataSourceH"ycdcatalog
cdcatalog.8ata'ind./
end if
end sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(8ata*ist idH7cdcatalog7
runatH7server7
cellpaddingH757
cellspacingH757
borderstyleH7inset7
bac#colorH7=eSeSeS7
widthH7E11I7
headerstylefontna"eH7-erdana7
headerstylefontsiAeH7E5pt7
headerstylehoriAontalalignH7center7
headerstylefontboldH7true7
ite"stylebac#colorH7=NNSSOO7
ite"styleforecolorH7=ffffff7
footerstylefontsiAeH7Opt7
footerstylefontitalicH7true7G
F)eaderTe"plateG
!y <8 <atalog
F>)eaderTe"plateG
F2te"Te"plateG
7FI=<ontainer.8ata2te".7title7/IG7 of
FI=<ontainer.8ata2te".7artist7/IG
UFI=<ontainer.8ata2te".7price7/IG
F>2te"Te"plateG
F4ooterTe"plateG
<opyright )ege ;efsnes
F>4ooterTe"plateG
F>asp(8ata*istG
F>for"G
F>bodyG
F>ht"lG
+sin. the @Alternatin.Ite#Te#(lateA
%ou can add an FAlternating2te"Te"plateG ele"ent after the F2te"Te"plateG ele"ent to describe the
appearance of alternating rows of output. %ou "ay style the data in the FAlternating2te"Te"plateG section
within the 8ata*ist control(
E4a#(le
FIR 2"port Na"espaceH7Syste".8ata7 IG
Fscript runatH7server7G
sub PageJ*oad
if Not Page.2sPost'ac# then
di" "ycdcatalogHNew 8ataSet
"ycdcatalog.;ead+"l.!apPath.7cdcatalog.x"l7//
cdcatalog.8ataSourceH"ycdcatalog
cdcatalog.8ata'ind./
end if
end sub
F>scriptG
Fht"lG
27
FbodyG
Ffor" runatH7server7G
Fasp(8ata*ist idH7cdcatalog7
runatH7server7
cellpaddingH757
cellspacingH757
borderstyleH7inset7
bac#colorH7=eSeSeS7
widthH7E11I7
headerstylefontna"eH7-erdana7
headerstylefontsiAeH7E5pt7
headerstylehoriAontalalignH7center7
headerstylefontboldH7True7
ite"stylebac#colorH7=NNSSOO7
ite"styleforecolorH7=ffffff7
alternatingite"stylebac#colorH7=eSeSeS7
alternatingite"styleforecolorH7=1111117
footerstylefontsiAeH7Opt7
footerstylefontitalicH7True7G
F)eaderTe"plateG
!y <8 <atalog
F>)eaderTe"plateG
F2te"Te"plateG
7FI=<ontainer.8ata2te".7title7/IG7 of
FI=<ontainer.8ata2te".7artist7/IG
UFI=<ontainer.8ata2te".7price7/IG
F>2te"Te"plateG
FAlternating2te"Te"plateG
7FI=<ontainer.8ata2te".7title7/IG7 of
FI=<ontainer.8ata2te".7artist7/IG
UFI=<ontainer.8ata2te".7price7/IG
F>Alternating2te"Te"plateG
F4ooterTe"plateG
Kcopy? )ege ;efsnes
F>4ooterTe"plateG
F>asp(8ata*istG
F>for"G
F>bodyG
F>ht"lG
ASP.NET - *ata/ase Connection
A8B.NET is also a part of the .NET 4ra"ewor#. A8B.NET is used to handle data access. $ith A8B.NET
you can wor# with databases.
E4a#(les&
B *ata/ase connection - 1ind to a *ata5ist control
FIR 2"port Na"espaceH7Syste".8ata.Ble8b7 IG
Fscript runatH7server7G
sub PageJ*oad
di" dbconn,s9l,dbco"",dbread
dbconnHNew Ble8b<onnection.7ProviderH!icrosoft.,et.B*E8'.P.1?data sourceH7 K
server."appath.7>db>northwind."db7//
dbconn.Bpen./
s9lH7SE*E<T V 4;B! custo"ers7
dbco""HNew Ble8b<o""and.s9l,dbconn/
dbreadHdbco"".Execute;eader./
custo"ers.8ataSourceHdbread
28
custo"ers.8ata'ind./
dbread.<lose./
dbconn.<lose./
end sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(8ata*ist
idH7custo"ers7
runatH7server7
cellpaddingH757
cellspacingH757
borderstyleH7inset7
bac#colorH7=eSeSeS7
widthH7E11I7
headerstylefontna"eH7-erdana7
headerstylefontsiAeH7E5pt7
headerstylehoriAontalalignH7center7
headerstylefontboldH7True7
ite"stylebac#colorH7=NNSSOO7
ite"styleforecolorH7=ffffff7
footerstylefontsiAeH7Opt7
footerstylefontitalicH7True7G
F)eaderTe"plateG
<usto"ers Table
F>)eaderTe"plateG
F2te"Te"plateG
FI=<ontainer.8ata2te".7co"panyna"e7/IG in
FI=<ontainer.8ata2te".7address7/IG, FI=<ontainer.8ata2te".7city7/IG
F>2te"Te"plateG
F4ooterTe"plateG
Source( Northwind 8atabase
F>4ooterTe"plateG
F>asp(8ata*istG
F>for"G
F>bodyG
F>ht"lG
,esult&
Custo#ers Ta/le
Alfreds 4utter#iste in Bbere Str. LN, 'erlin
'erglunds snabb#Wp in 'erguvsvXgen S, *uleY
<entro co"ercial !octeAu"a in Sierras de Tranada OOO0, !Zxico 8.4.
Ernst )andel in &irchgasse C, TraA
42SSA 4abrica 2nter. Salchichas S.A. in <> !oralAarAal, SC, !adrid
Taler[a del gastr\no"o in ;a"bla de <atalu]a, 50, 'arcelona
2sland Trading in Tarden )ouse <rowther $ay, <owes
&Wniglich Essen in !aubelstr. O1, 'randenburg
*aughing 'acchus $ine <ellars in EO11 Ba# St., -ancouver
!agaAAini Ali"entari ;iuniti in -ia *udovico il !oro 55, 'erga"o
North>South in South )ouse 011 Queensbridge, *ondon
Paris spZcialitZs in 5CL, boulevard <haronne, Paris
;attlesna#e <anyon Trocery in 5SEN !ilton 8r., Albu9uer9ue
Si"ons bistro in -inb^ltet 0P, &_benhavn
29
The 'ig <heese in SO ,efferson $ay Suite 5, Portland
-affel3ernet in S"agsl_get PL, `rhus
$ols#i Ma3aAd in ul. 4iltrowa CS, $arsAawa
Source( Northwind 8atabase
B*ata/ase connection - 1ind to a ,e(eater control
FIR 2"port Na"espaceH7Syste".8ata.Ble8b7 IG
Fscript runatH7server7G
sub PageJ*oad
di" dbconn,s9l,dbco"",dbread
dbconnHNew Ble8b<onnection.7ProviderH!icrosoft.,et.B*E8'.P.1?data sourceH7 K
server."appath.7>db>northwind."db7//
dbconn.Bpen./
s9lH7SE*E<T V 4;B! custo"ers7
dbco""HNew Ble8b<o""and.s9l,dbconn/
dbreadHdbco"".Execute;eader./
custo"ers.8ataSourceHdbread
custo"ers.8ata'ind./
dbread.<lose./
dbconn.<lose./
end sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(;epeater idH7custo"ers7 runatH7server7G
F)eaderTe"plateG
Ftable borderH7E7 widthH7E11I7G
Ftr bgcolorH7=b1cPde7G
FthG<o"panyna"eF>thG
FthG<ontactna"eF>thG
FthGAddressF>thG
FthG<ityF>thG
F>trG
F>)eaderTe"plateG
F2te"Te"plateG
Ftr bgcolorH7=f1f1f17G
FtdGFI=<ontainer.8ata2te".7co"panyna"e7/IG F>tdG
FtdGFI=<ontainer.8ata2te".7contactna"e7/IG F>tdG
FtdGFI=<ontainer.8ata2te".7address7/IG F>tdG
FtdGFI=<ontainer.8ata2te".7city7/IG F>tdG
F>trG
F>2te"Te"plateG
F4ooterTe"plateG
F>tableG
F>4ooterTe"plateG
F>asp(;epeaterG
F>for"G
F>bodyG
F>ht"lG
,esult&
30
Co#(an%na#e Contactna#e Address Cit%
Alfreds 4utter#iste !aria Anders Bbere Str. LN 'erlin
'erglunds snabb#Wp <hristina 'erglund 'erguvsvXgen S *uleY
<entro co"ercial
!octeAu"a
4rancisco <hang Sierras de Tranada
OOO0
!Zxico 8.4.
Ernst )andel ;oland !endel &irchgasse C TraA
42SSA 4abrica 2nter.
Salchichas S.A.
8iego ;oel <> !oralAarAal, SC !adrid
Taler[a del gastr\no"o Eduardo Saavedra ;a"bla de <atalu]a, 50 'arcelona
2sland Trading )elen 'ennett Tarden )ouse <rowther
$ay
<owes
&Wniglich Essen Philip <ra"er !aubelstr. O1 'randenburg
*aughing 'acchus $ine
<ellars
%oshi Tanna"uri EO11 Ba# St. -ancouver
!agaAAini Ali"entari
;iuniti
Tiovanni ;ovelli -ia *udovico il !oro 55 'erga"o
North>South Si"on <rowther South )ouse 011
Queensbridge
*ondon
Paris spZcialitZs !arie 'ertrand 5CL, boulevard
<haronne
Paris
;attlesna#e <anyon
Trocery
Paula $ilson 5SEN !ilton 8r. Albu9uer9ue
Si"ons bistro ,ytte Petersen -inb^ltet 0P &_benhavn
The 'ig <heese *iA Nixon SO ,efferson $ay Suite
5
Portland
-affel3ernet Palle 2bsen S"agsl_get PL `rhus
$ols#i Ma3aAd MbysAe# PiestrAeniewicA ul. 4iltrowa CS $arsAawa
What is A*O.NET?
A8B.NET is a part of the .NET 4ra"ewor#
A8B.NET consists of a set of classes used to handle data access
A8B.NET is entirely based on +!*
A8B.NET has, unli#e A8B, no ;ecordset ob3ect
Create a *ata/ase Connection
$e are going to use the Northwind database in our exa"ples.
4irst, i"port the 7Syste".8ata.Ble8b7 na"espace. $e need this na"espace to wor# with !icrosoft Access
and other B*E 8' database providers. $e will create the connection to the database in the PageJ*oad
subroutine. $e create a dbconn variable as a new Ble8b<onnection class with a connection string which
identifies the B*E 8' provider and the location of the database. Then we open the database connection(
FIR 2"port Na"espaceH7Syste".8ata.Ble8b7 IG
Fscript runatH7server7G
sub PageJ*oad
di" dbconn
dbconnHNew Ble8b<onnection.7ProviderH!icrosoft.,et.B*E8'.P.1?
data sourceH7 K server."appath.7northwind."db7//
dbconn.Bpen./
end sub
F>scriptG
Note( The connection string "ust be a continuous string without a line brea#D
<reate a 8atabase <o""and
To specify the records to retrieve fro" the database, we will create a dbco"" variable as a new
Ble8b<o""and class. The Ble8b<o""and class is for issuing SQ* 9ueries against database tables(FIR
2"port Na"espaceH7Syste".8ata.Ble8b7 IG
31
Fscript runatH7server7G
sub PageJ*oad
di" dbconn,s9l,dbco""
dbconnHNew Ble8b<onnection.7ProviderH!icrosoft.,et.B*E8'.P.1?
data sourceH7 K server."appath.7northwind."db7//
dbconn.Bpen./
s9lH7SE*E<T V 4;B! custo"ers7
dbco""HNew Ble8b<o""and.s9l,dbconn/
end sub
F>scriptG
Create a *ata,eader
The Ble8b8ata;eader class is used to read a strea" of records fro" a data source. A 8ata;eader is
created by calling the Execute;eader "ethod of the Ble8b<o""and ob3ect(
FIR 2"port Na"espaceH7Syste".8ata.Ble8b7 IG
Fscript runatH7server7G
sub PageJ*oad
di" dbconn,s9l,dbco"",dbread
dbconnHNew Ble8b<onnection.7ProviderH!icrosoft.,et.B*E8'.P.1?
data sourceH7 K server."appath.7northwind."db7//
dbconn.Bpen./
s9lH7SE*E<T V 4;B! custo"ers7
dbco""HNew Ble8b<o""and.s9l,dbconn/
dbreadHdbco"".Execute;eader./
end sub
F>scriptG
1ind to a ,e(eater Control
Then we bind the 8ata;eader to a ;epeater control(
E4a#(le
FIR 2"port Na"espaceH7Syste".8ata.Ble8b7 IG
Fscript runatH7server7G
sub PageJ*oad
di" dbconn,s9l,dbco"",dbread
dbconnHNew Ble8b<onnection.7ProviderH!icrosoft.,et.B*E8'.P.1?
data sourceH7 K server."appath.7northwind."db7//
dbconn.Bpen./
s9lH7SE*E<T V 4;B! custo"ers7
dbco""HNew Ble8b<o""and.s9l,dbconn/
dbreadHdbco"".Execute;eader./
custo"ers.8ataSourceHdbread
custo"ers.8ata'ind./
dbread.<lose./
dbconn.<lose./
end sub
F>scriptG
Fht"lG
FbodyG
Ffor" runatH7server7G
Fasp(;epeater idH7custo"ers7 runatH7server7G
F)eaderTe"plateG
Ftable borderH7E7 widthH7E11I7G
FtrG
FthG<o"panyna"eF>thG
FthG<ontactna"eF>thG
FthGAddressF>thG
FthG<ityF>thG
F>trG
32
F>)eaderTe"plateG
F2te"Te"plateG
FtrG
FtdGFI=<ontainer.8ata2te".7co"panyna"e7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7contactna"e7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7address7/IGF>tdG
FtdGFI=<ontainer.8ata2te".7city7/IGF>tdG
F>trG
F>2te"Te"plateG
F4ooterTe"plateG
F>tableG
F>4ooterTe"plateG
F>asp(;epeaterG
F>for"G
F>bodyG
F>ht"lG
Close the *ata/ase Connection
Always close both the 8ata;eader and database connection after access to the database is no longer
re9uired(dbread.<lose./
dbconn.<lose./
ASP.NET 2.0 - New "eatures
ASP.NET 5.1 i"proves ASP.NET by adding several new features.
I#(ro)e#ents in ASP.NET 2.0
ASP.NET 5.1 was designed to "a#e web develop"ent easier and 9uic#er.
*esi.n .oals !or ASP.NET 2.0&
2ncrease productivity by re"oving N1I of the code
@se the sa"e controls for all types of devices
Provide a faster and better web server platfor"
Si"plify co"pilation and installation
Si"plify the ad"inistration of web applications
WhatCs New in ASP.NET 2.0?
So"e of the new features in ASP.NET 5.1 are(
!aster Pages, The"es, and $eb Parts
Standard controls for navigation
Standard controls for security
;oles, personaliAation, and internationaliAation services
2"proved and si"plified data access controls
4ull support for +!* standards li#e, +)T!*, +!*, and $S8*
2"proved co"pilation and deploy"ent .installation/
2"proved site "anage"ent
New and i"proved develop"ent tools
The new features are described below.
aster Pa.es
ASP.NET didn't have a "ethod for applying a consistent loo# and feel for a whole web site.
!aster pages in ASP.NET 5.1 solves this proble".
A "aster page is a te"plate for other pages, with shared layout and functionality. The "aster page defines
placeholders for content pages. The result page is a co"bination ."erge/ of the "aster page and the
content page.
;ead "ore about "aster pages.
The"es
33
The"es is another feature of ASP.NET 5.1. The"es, or s#ins, allow developers to create a custo"iAed loo#
for web applications.
8esign goals for ASP.NET 5.1 the"es(
!a#e it si"ple to custo"iAe the appearance of a site
Allow the"es to be applied to controls, pages, and entire sites
Allow all visual ele"ents to be custo"iAed
We/ Parts
ASP.NET 5.1 $eb Parts can provide a consistent loo# for a site, while still allowing user custo"iAation of
style and content.
New controls(
Mone controls areas on a page where the content is consistent
$eb part controls content areas for each Aone
Navigation
ASP.NET 5.1 has builtin navigation controls li#e
Site !aps
8yna"ic )T!* "enus
Tree -iews
Securit%
Security is very i"portant for protecting confidential and personal infor"ation.
In ASP.NET 2.0 the !ollowin. controls has /een added&
A *ogin control, which provides login functionality
A *oginStatus control, to control the login status
A *oginNa"e control to display the current user na"e
A *ogin-iew control, to provide different views depending on login status
A <reate@ser wiAard, to allow creation of user accounts
A Password;ecovery control, to provide the 72 forgot "y password7 functionality
,oles and Personali;ation
2nternet co""unities are growing very popular.
ASP.NET 5.1 has personaliAation features for storing user details. This provides an easy way to custo"iAe
user .and user group/ properties.
Internationali;ation
;eaching people with different languages is i"portant if you want to reach a larger audience.
ASP.NET 5.1 has i"proved support for "ultiple languages.
*ata Access
!any web sites are data driven, using databases or +!* files as data sources.
$ith ASP.NET this involved code, and often the sa"e code had to be used over and over in
different web pages.
A #ey goal of ASP.NET 5.1 was to ease the use of data sources.
ASP.NET 5.1 has new data controls, re"oving "uch of the need for progra""ing and indepth
#nowledge of data connections.
o/ilit% Su((ort
The proble" with !obile devices is screen siAe and display capabilities.
2n ASP.NET, the !icrosoft !obile 2nternet Tool#it .!!2T/ provided this support.
2n ASP.NET 5.1, !!2T is no longer needed because "obile support is built into all controls.
2"ages
ASP.NET 2.0 has new controls !or handlin. i#a.es&
The 2"age!ap control i"age "ap support
The 8yna"ic2"age control i"age support for different browsers
34
These controls are i"portant for better i"age display on "obile devices, li#e handheld co"puters and cell
phones.
Auto#atic Co#(ilation
ASP.NET 5.1 provides auto"atic co"pilation. All files within a directory will be co"piled on the first
run, including support for $S8*, and +S8 files.
<o"piled 8eploy"ent .2nstallation/ and Source Protection
ASP.NET 5.1 also provides preco"pilation. An entire web site can be preco"piled. This provides
an easy way to deploy .upload to a server/ co"piled applications, and because only co"piled files
are deployed, the source code is protected.
Site ana.e#ent
ASP.NET 5.1 has three new features for web site configuration and "anage"ent(
New local "anage"ent console
New progra""able "anage"ent functions .AP2/
New webbased "anage"ent tool
*e)elo(#ent Tools
$ith ASP.NET -isual Studio.NET was released with pro3ect and design features targeted at
corporate developers.
$ith ASP.NET 5.1, -isual Studio 511L was released.
&ey design features for -isual Studio 511L include(
Support for the features described above
@pload files fro" anywhere .4TP, 4ile Syste", 4ront Page..../
No pro3ect files, allowing code to be "anipulated outside -isual Studio
2ntegrated $eb Site Ad"inistration Tool
No 7build7 step ability to co"pile on first run
-isual $eb 8eveloper is a new free ASP.NET 5.1 tool for noncorporate developers who don't have access
to -isual Studio.NET.
ASP.NET 2.0 - aster Pa.es
!aster pages provide te"plates for other pages on your web site.
aster Pa.es
!aster pages allow you to create a consistent loo# and behavior for all the pages .or group of pages/ in your
web application.
A "aster page provides a te"plate for other pages, with shared layout and functionality. The "aster page
defines placeholders for the content, which can be overridden by content pages. The output result is a
co"bination of the "aster page and the content page.
The content pages contains the content you want to display.
$hen users re9uest the content page, ASP.NET "erges the pages to produce output that co"bines the
layout of the "aster page with the content of the content page.
aster Pa.e E4a#(le&
FIR !aster IG
Fht"lG
FbodyG
FhEGStandard )eader 4or All PagesF>hEG
Fasp(<ontentPlace)older idH7<P)E7 runatH7server7G
F>asp(<ontentPlace)olderG
F>bodyG
F>ht"lG
The "aster page above is a nor"al )T!* page designed as a te"plate for other pages.
The D aster directi)e de!ines it as a #aster (a.e.
The "aster page contains a placeholder tag Fasp(<ontentPlace)olderG for individual content.
35
The idH7<P)E7 attribute identifies the placeholder, allowing "any placeholders in the sa"e "aster page.
This "aster page was saved with the na"e 7"asterE."aster7.
Note: The master !age can also contain coe( allowing #namic content.
Content Pa.e
E4a#(le
FIR Page !asterPage4ileH7"asterE."aster7 IG
Fasp(<ontent <ontentPlace)older2dH7<P)E7 runatH7server7G
Fh5G2ndividual <ontentF>h5G
FpGParagraph EF>pG
FpGParagraph 5F>pG
F>asp(<ontentG
The content page above is one of the individual content pages of the web.
The R Page directive defines it as a standard content page.
The content page contains a content tag Fasp(<ontentG with a reference to the "aster page
.<ontentPlace)older2dH7<P)E7/.
This content page was saved with the na"e 7"ypageE.aspx7.
$hen the user re9uests this page, ASP.NET "erges the content page with the "aster page.
Note: The content te+t must be insie the <as!:/ontent> tag. No content is allowe outsie the tag.
/ontent $age 0ith /ontrols
FIR Page !asterPage4ileH7"asterE."aster7 IG
Fasp(<ontent <ontentPlace)older2dH7<P)E7 runatH7server7G
Fh5G$0SchoolsF>h5G
Ffor" runatH7server7G
Fasp(Text'ox idH7textboxE7 runatH7server7 >G
Fasp('utton idH7buttonE7 runatH7server7 textH7'utton7 >G
F>for"G
F>asp(<ontentG
The content page above de"onstrates how .NET controls can be inserted into the content page 3ust li#e an
into an ordinary page.
ASP.NET 2.0 - Na)i.ation
ASP.NET 5.1 has builtin navigation controls
We/ Site Na)i.ation
!aintaining the "enu of a large web site is difficult and ti"e consu"ing.
2n ASP.NET 5.1 the "enu can be stored in a file to "a#e it easier to "aintain. This file is nor"ally called
web.site"ap, and is stored in the root directory of the web.
In addition7 ASP.NET 2.0 has three new na)i.ation controls&
8yna"ic "enus
Tree-iews
Site !ap Path
The Site"ap 4ile
The !ollowin. site#a( !ile is used in this tutorial&
F6x"l versionH7E.17 encodingH72SBSSLOE7 6G
Fsite!apG
Fsite!apNode titleH7)o"e7 urlH7>aspnet>w0ho"e.aspx7G
Fsite!apNode titleH7Services7 urlH7>aspnet>w0services.aspx7G
Fsite!apNode titleH7Training7 urlH7>aspnet>w0training.aspx7>G
Fsite!apNode titleH7Support7 urlH7>aspnet>w0support.aspx7>G
F>site!apNodeG
F>site!apNodeG
36
F>site!apG
,ules !or creatin. a site#a( !ile&
The +!* file "ust contain a Fsite!apG tag surrounding the content
The Fsite!apG tag can only have one Fsite!apNodeG child node .the 7ho"e7 page/
Each Fsite!apNodeG can have several child nodes .web pages/
Each Fsite!apNodeG has attributes defining page title and @;*
Note( The site"ap file "ust be placed in the root directory of the web and the @;* attributes "ust be
relative to the root directory.
*%na#ic enu
The Fasp(!enuG control displays a standard site navigation "enu.
Code E4a#(le&
Fasp(Site!ap8ataSource idH7navE7 runatH7server7 >G
Ffor" runatH7server7G
Fasp(!enu runatH7server7 8ataSource2dH7navE7 >G
F>for"G
The Fasp(!enuG control in the exa"ple above is a placeholder for a server created navigation "enu.
The data source of the control is defined by the 8ataSource2d attribute. The idH7navE7 connects it to the
Fasp(Site!ap8ataSourceG control.
The Fasp(Site!ap8ataSourceG control auto"atically connects to the default site"ap file .web.site"ap/.
Tree2iew
The Fasp(Tree-iewG control displays a "ulti level navigation "enu.
The "enu loo#s li#e a tree with branches that can be opened or closed with : or sy"bol.
Code E4a#(le&
Fasp(Site!ap8ataSource idH7navE7 runatH7server7 >G
Ffor" runatH7server7G
Fasp(Tree-iew runatH7server7 8ataSource2dH7navE7 >G
F>for"G
The Fasp(Tree-iewG control in the exa"ple above is a placeholder for a server created navigation "enu.
The data source of the control is defined by the 8ataSource2d attribute. The idH7navE7 connects it to the
Fasp(Site!ap8ataSourceG control.
The Fasp(Site!ap8ataSourceG control auto"atically connects to the default site"ap file .web.site"ap/.
Sitea(Path
The Site!apPath control displays the trail .navigation path/ to the current page. The path acts as clic#able
lin#s to previous pages.
@nli#e the Tree-iew and !enu control the Site!apPath control does NBT use a Site!ap8ataSource. The
Site!apPath control uses the web.site"ap file by default.
Ti!s: "f the )iteMa!$ath is!la#s incorrectl#( most li1el# there is an 23L error 4t#!o5 in the web.sitema! file.
<ode Exa"ple(Ffor" runatH7server7G
Fasp(Site!apPath runatH7server7 >G
F>for"G
The Fasp(Site!apPathG control in the exa"ple above is a placeholder for a server created site path display.
-T5 Ser)er Controls
)T!* server controls are )T!* tags understood by the server.
-T5 Ser)er Controls
)T!* ele"ents in ASP.NET files are, by default, treated as text. To "a#e these ele"ents progra""able,
add a runatH7server7 attribute to the )T!* ele"ent. This attribute indicates that the ele"ent should be
treated as a server control.
Note: All HTML server controls must be within a <form> tag with the runat="server" attribute'
Note: A)$.N*T re6uires that all HTML elements must be !ro!erl# close an !ro!erl# neste.
-T5 Ser)er Control *escri(tion
37
)t"lAnchor <ontrols an FaG )T!* ele"ent
)t"l'utton <ontrols a FbuttonG )T!* ele"ent
)t"l4or" <ontrols a Ffor"G )T!* ele"ent
)t"lTeneric <ontrols other )T!* ele"ent not specified by a specific )T!* server
control, li#e FbodyG, FdivG, FspanG, etc.
)t"l2"age <ontrols an Fi"ageG )T!* ele"ent
)t"l2nput'utton <ontrols Finput typeH7button7G, Finput typeH7sub"it7G, and Finput
typeH7reset7G )T!* ele"ents
)t"l2nput<hec#'ox <ontrols an Finput typeH7chec#box7G )T!* ele"ent
)t"l2nput4ile <ontrols an Finput typeH7file7G )T!* ele"ent
)t"l2nput)idden <ontrols an Finput typeH7hidden7G )T!* ele"ent
)t"l2nput2"age <ontrols an Finput typeH7i"age7G )T!* ele"ent
)t"l2nput;adio'utton <ontrols an Finput typeH7radio7G )T!* ele"ent
)t"l2nputText <ontrols Finput typeH7text7G and Finput typeH7password7G )T!*
ele"ents
)t"lSelect <ontrols a FselectG )T!* ele"ent
)t"lTable <ontrols a FtableG )T!* ele"ent
)t"lTable<ell <ontrols FtdGand FthG )T!* ele"ents
)t"lTable;ow <ontrols a FtrG )T!* ele"ent
)t"lTextArea <ontrols a FtextareaG )T!* ele"ent
We/ Ser)er Controls
$eb server controls are special ASP.NET tags understood by the server.
We/ Ser)er Controls
*i#e )T!* server controls, $eb server controls are also created on the server and they re9uire a
runatH7server7 attribute to wor#. )owever, $eb server controls do not necessarily "ap to any existing )T!*
ele"ents and they "ay represent "ore co"plex ele"ents.
The s%nta4 !or creatin. a We/ ser)er control is&
Fasp(controlJna"e idH7so"eJid7 runatH7server7 >G
We/ Ser)er Control *escri(tion
Ad;otator 8isplays a se9uence of i"ages
'utton 8isplays a push button
<alendar 8isplays a calendar
<alendar8ay A day in a calendar control
<hec#'ox 8isplays a chec# box
<hec#'ox*ist <reates a "ultiselection chec# box group
8ataTrid 8isplays fields of a data source in a grid
8ata*ist 8isplays ite"s fro" a data source by using te"plates
8rop8own*ist <reates a dropdown list
)yper*in# <reates a hyperlin#
2"age 8isplays an i"age
2"age'utton 8isplays a clic#able i"age
*abel 8isplays static content which is progra""able .lets you apply styles to its
content/
*in#'utton <reates a hyperlin# button
*ist'ox <reates a single or "ultiselection dropdown list
*ist2te" <reates an ite" in a list
*iteral 8isplays static content which is progra""able.does not let you apply styles to
its content/
Panel Provides a container for other controls
Place)older ;eserves space for controls added by code
;adio'utton <reates a radio button
;adio'utton*ist <reates a group of radio buttons
'ulleted*ist <reates a list in bullet for"at
;epeater 8isplays a repeated list of ite"s bound to the control
Style Sets the style of controls
38
Table <reates a table
Table<ell <reates a table cell
Table;ow <reates a table row
Text'ox <reates a text box
+"l 8isplays an +!* file or the results of an +S* transfor"
2alidation Ser)er Controls
-alidation server controls are used to validate userinput.
2alidation Ser)er Controls
A -alidation server control is used to validate the data of an input control. 2f the data does not pass
validation, it will display an error "essage to the user.
The s%nta4 !or creatin. a 2alidation ser)er control is&
Fasp(controlJna"e idH7so"eJid7 runatH7server7 >G
2alidation Ser)er Control *escri(tion
<o"pare-alidator <o"pares the value of one input control to the value of another input
control or to a fixed value
<usto"-alidator Allows you to write a "ethod to handle the validation of the value
entered
;ange-alidator <hec#s that the user enters a value that falls between two values
;egularExpression-alidator Ensures that the value of an input control "atches a specified pattern
;e9uired4ield-alidator !a#es an input control a re9uired field
-alidationSu""ary 8isplays a report of all validation errors occurred in a $eb page
ASP.NET E4a#(les

ASP.NET -T5 Controls
)T!*Anchor
)T!*'utton
)T!*2"age
)T!*2"age 5
)T!*2nputbutton
)T!*2nput<hec#box
)T!*2nput)idden
)T!*2nput2"age
)T!*2nput;adiobutton
)T!*Table
)T!*Table 5
)T!*Textarea
ASP.NET We/ Controls
Ad;otator
'utton
'utton 5
<alendar
<alendar 5
<alendar 0
<hec#box
<hec#box*ist
8ata*ist
39
8ata*ist with styles
8ata*ist with FAlternating2te"Te"plateG
8ropdown*ist
)yperlin#
2"age
2"age'utton
*abel
*in#'utton
*istbox
*iteral
*iteral 5
Panel
;adiobutton
;adiobutton*ist
;epeater
;epeater with FAlternating2te"Te"plateG
;epeater with FSeparatorTe"plateG
Table
Table 5
Textbox
Textbox 5
Textbox 0
+!*
ASP.NET 2alidation Controls
<o"pare-alidator
<o"pare-alidator 5
<usto"-alidator
;ange-alidator
;ange-alidator 5
;egularExpression-alidator
;e9uired4ield-alidator
-alidationsu""ary
-alidationsu""ary 5
ASP.NET E)ents
PageJ*oad
Page.2sPost'ac#
ASP.NET *ata 1indin.
Array*ist ;adio'utton*ist
Array*ist 8rop8own*ist
)ashtable ;adio'utton*ist E
)ashtable ;adiobutton*ist 5
)ashtable 8rop8own*ist
Sorted*ist ;adio'utton*ist E
Sorted*ist ;adiobutton*ist 5
Sorted*ist 8rop8own*ist
+!* ;adiobutton*ist
ASP.NET *ata/ase
8atabase connection 'ind to a ;epeater control
8atabase connection 'ind to a 8ata*ist control
40
41

Potrebbero piacerti anche