Académique Documents
Professionnel Documents
Culture Documents
NET
Martin Kropp
University of Applied Sciences Northwestern Switzerland
Adopted from the presentations Dynamic Web Pages with ASP.NET , Prof. Dr. Hanspeter Mssenbck,University of Linz
Learning Targets
You
know and can explain the concepts und functionality of the ASP.NET class library and their most important components
client-side and server-side web development code-behind concept event handling the state management concepts validation user controls vs. custom controls
know the basic ASP.NET web controls and can apply them in concrete projects can develop web applications using ASP.NET
ASP.Net
ASP.Net
ASP.NET
Simple Dynamic Web Pages Web Forms Event Handling Web Controls Validators User Controls Custom Controls State Management Masterpages
Browser
Response(My.html)
Server (IIS)
My.html
ASP.Net
request ("Counter.aspx")
ASP.NET
server (IIS)
*.html
page object
.NET framework
ASP.Net Institute for Mobile and Distributed Systems, M. Kropp 7
does not contain any script code any browser can display this HTML
ASP.Net
Response.Write(CounterValue());
ASP.Net Institute for Mobile and Distributed Systems, M. Kropp 9
Code Behind
Counter.aspx
<%@ Page Language="C#" Inherits="CounterPage" CodeFile="CounterPage.aspx.cs" %> <html> <head> <title>Page counter</title> </head> <body> <h1>Welcome</h1> You are visitor number <%=CounterValue()%> ! </body> </html>
CounterPage.aspx.cs
using System.IO; public class CounterPage : System.Web.UI.Page { public int CounterValue() { FileStream s = new FileStream("c:\\Data\\Counter.dat", FileMode.OpenOrCreate); ... n = r.ReadInt32(); n++; ... return n; } }
ASP.Net Institute for Mobile and Distributed Systems, M. Kropp 10
aspx page
<%@ Page Language="C#"%> <html> <body> ... <%= ... %>... </body> </html>
Counter.aspx
Counter_aspx ...
ASP.Net, v1.1
11
CounterPage CounterValue()
aspx page
Counter.aspx
<%@ Page ... Inherits="CounterPage"%> <html> <body> ... <%=CounterValue()%>... </body> </html>
Counter_aspx ...
ASP.Net
12
13
Adder.aspx
ASP.Net
14
Adder.aspx.cs
15
ASP.Net
Example
<asp:Label ID="total" Text="Hello" ForeColor="Red" Runat="server" /> public class Label: WebControl { public virtual string ID { get {...} set {...} } public virtual string Text { get {...} set {...} } public virtual Color ForeColor { get {...} set {...} } ... } All web control classes are in the namespace System.Web.UI
Alternative Notation
<asp:Label ID="total" ForeColor="Red" Runat="server" > Hello </asp:Label>
ASP.Net Institute for Mobile and Distributed Systems, M. Kropp 17
One can implement custom GUI elements Web pages can access the whole .NET library
databases, XML, RMI, ...
ASP.Net
18
Calendar
ASP.Net
19
WebControl
Font Width Height
TemplateControl
...
Button
Text
TextBox
Text Rows Columns
Label
Text
...
Page
Request Response IsPostBack
UserControl
...
ASP.Net
20
Event-based Processing
mouse click click event
event handler
void DoClick (object sender, EventArgs e) { ... }
Client
Server
ASP.Net
21
Kinds of Events
Control all Event Init Load When does the event occur? when the control is created after the data that were sent by the browser have been loaded into the control before HTML code for this control is generated before the control is removed from memory when the button was clicked when the contents of the TextBox changed when the state of the CheckBox changed
SelectedIndexChanged when a new item from the list has been selected
Institute for Mobile and Distributed Systems, M. Kropp 22
Click
Client
ASP.Net
Server
Institute for Mobile and Distributed Systems, M. Kropp 23
Click
Client
ASP.Net
Server
Institute for Mobile and Distributed Systems, M. Kropp 24
Click
3. Loading - load controls with the values that the user has entered (page state) - raise Load events
Client
ASP.Net
Server
Institute for Mobile and Distributed Systems, M. Kropp 25
Client
ASP.Net
Server
Institute for Mobile and Distributed Systems, M. Kropp 26
TextBox Button
+ page state 5. Rendering - raise PreRender events - call Render methods of all controls, which render the controls to HTML
Client
ASP.Net
Server
Institute for Mobile and Distributed Systems, M. Kropp 27
Button
Client
ASP.Net
Server
Institute for Mobile and Distributed Systems, M. Kropp 28
ASP.Net
Validators
Objects for plausibility checks
Label
BaseValidator
BaseCompareValidator
ASP.Net
30
Validators (Example)
Name: <asp:TextBox ID="name" Runat="server" /> <asp:RequiredFieldValidator ControlToValidate="name" Text="*" ErrorMessage="You must enter a name" Runat="server" /> <br> Age: <asp:TextBox ID="age" Runat="server" /> <asp:RangeValidator ControlToValidate="age" Text="*" MinimumValue="0" MaximumValue="100" Type="Integer" ErrorMessage="The age must be between 0 and 100" Runat="server" /> <asp:Button Text="Submit" OnClick="DoClick" Runat="server" /> <asp:ValidationSummary Runat="server" />
ASP.Net
31
ASP.Net
32
ASP.Net
34
must override the Render method, which translates this control to HTML
Institute for Mobile and Distributed Systems, M. Kropp 35
Used as follows
<%@ Page Language="C#" %> <%@ Register TagPrefix="my" Namespace="Folds" Assembly="Fold" %> <html> <body> <form Runat="server"> <my:Fold Text="..." AlternateText="..." Runat="server" /> </form> </body> </html>
ASP.Net
36
Session state
(session = all requests from the same client within a certain time) e.g. shopping cart, email address of a client, ...
Application state (Application = all aspx files in the same virtual directory)
e.g. configuration data, number of sessions, ... application C l i e n t C l i e n t
ASP.Net
session /Samples x.aspx session state session S e r v e r y.aspx z.aspx application state
session state
Institute for Mobile and Distributed Systems, M. Kropp 37
Session state
writing: reading: Session["cart"] = shoppingCart; DataTable shoppingCart = (DataTable) Session["cart"];
Application state
writing: reading: Application["database"] = databaseName; string databaseName = (string) Application["database"];
ASP.Net
38
app directories
machine.config web.config
global configuration file stored in the .NET Framework directory specific configuration file stored in a web app directories or in its subdirectories overwrites configurations from machine.config or from other configuration files further up the hierarchy
ASP.Net
39
ASP.Net
40
Example: Tracing
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.web> <trace enabled="true" pageOutput="true" /> ... </system.web> ... </configuration>
ASP.Net
41
Authorization
Who may visit the pages of a specific directory?
The directory must have a web.config file with the following contents
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.web> <authorization> <allow users="admin" /> <deny users="?" /> </authorization> ... </system.web> ... </configuration>
users="user1, user2, ..." * ? name all users anonymous users users who have authenticated themselves with this name
machine.config contains
<allow users="*" />
ASP.Net
42
Authentication
4 kinds
None No authentication. All users are anonymous. Uses the login name and the password of the Windows login. Makes only sense for local servers. Users are redirected to the login page of the Passport server where they can authenticate themselves (with their user name and password). Users are authenticated by a custom login page.
Windows
Passport
Forms
ASP.Net
43
The users "peter" and "wolfgang" as well as their passwords are stored on the server
ASP.Net Institute for Mobile and Distributed Systems, M. Kropp 44
web.config
<deny users="?" /> .. <forms loginUrl="Login.aspx" ...>
no
5. User is redirected to A.aspx and may also access all other pages of this directory now (because she is authenticated)
ASP.Net
45
Masterpages
Idea: Uniform layout for all pages
My.master <%@ Master %> ... title area ... ... links ... <asp:ContentPlaceHolder id="Stuff" runat="server"/> Page1.aspx <%@ Page master="My.master" %> <asp:Content ContentPlaceHolderId="Stuff" runat="server"> http://domain/virtualdir/Page1.aspx
</asp:Content>
http://domain/virtualdir/Page2.aspx
</asp:Content>
ASP.Net
47
Main.master
Sub.master
MyPage.aspx
Master can be assigned to all pages of a site by specifying it in web.config Users can define different master pages for different browser types
ASP.Net Institute for Mobile and Distributed Systems, M. Kropp 48
Each of these pages uses the same Master. This Master contains a TreeView control. Thus the TreeView is re-displayed on every page.
Shows the path to the current page if this page belongs to a Sitemap
ASP.Net
50