Anatomy of an ASP.NET Pages - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Anatomy of an ASP.NET Pages

Description:

Protect your code. Promote code reuse. Code-Behind. Steps to use code-behind approach ... Not used in your program, it is used by the VS to insert auto generated code ... – PowerPoint PPT presentation

Number of Views:1287
Avg rating:3.0/5.0
Slides: 40
Provided by: natheerk
Category:

less

Transcript and Presenter's Notes

Title: Anatomy of an ASP.NET Pages


1
Anatomy of an ASP.NET Pages
OutlineASP.NET Life CycleInternet Information
Services (IIS 5.0)A Simple PageIntroducing
In-Line ScriptCode-Behind
2
(No Transcript)
3
(No Transcript)
4
A Simple Page
  • ASP.NET can be developed with a simple text
    editor or by using Visual Studio .NET
  • A Simple Page
  • .aspx pages are parsed by aspnet_wp.exe into HTML
    pages

lthtmlgtltbodygtHello Worldlt/bodygtlt/htmlgt
lthtmlgtltbodygtHello Worldlt/bodygtlt/htmlgt
Internet
aspnet_wp.exe
HelloWord.aspx
Result on the client side
5
A Simple Page
  • Adding a Web Control

Internet
HelloWorld.aspx
aspnet_wp.exe
Html page on the client side
6
Microsoft Internet Information Services (IIS) 5.0
  • Can be started by double clicking on
  • Control Panel -gt Performance and maintenance -gt
    Administrative Tools -gt Internet Information
    Services
  • Or type inetmgr on the Start menus Run command
    prompt.

7
Microsoft Internet Information Services (IIS) 5.0
8
Virtual Directory
C\Inetpub\wwwroot\index.aspx
http//localhost/index.aspx
C\Inetpub\wwwroot\myweb/index.aspx
http//localhost/myweb/index.aspx
9
Virtual Directory
Alias
Directory
New virtual directory
CIS340Web
D\WebFiles\
D\WebFiles\home.aspx
http//localhost/CIS340Web/home.aspx
D\WebFiles\root\home.aspx
http//localhost/CIS340Web/root/home.aspx
10
Microsoft Internet Information Services (IIS) 5.0
11
Microsoft Internet Information Services (IIS) 5.0
12
Microsoft Internet Information Services (IIS) 5.0
13
Microsoft Internet Information Services (IIS) 5.0
14
Introducing In-Line Script
  • lthtmlgt
  • ltheadgt
  • lt/headgt
  • ltbodygt
  • ltform runatservergt
  • ltaspLabel idlblHelloWorld text"Hello World"
    runatserver /gt
  • ltbrgt
  • ltaspButton onclickClickedIt text"Submit"
    runatServer /gt
  • lt/formgt
  • lt/bodygt
  • ltscript LanguageC runatservergt
  • void ClickedIt(object sender, System.EventArgs
    e)
  • lblHelloWorld.Text ".NET Rules!"
  • lt/scriptgt
  • lt/htmlgt

15
Introducing In-Line Script
HelloWorld31.txt
HelloWorld32.txt
16
(No Transcript)
17
Advantages of Code-Behind
  • Clean separation of HTML and code
  • Code in any Visual Studio language (C, VB, C,
    J)
  • Easier reuse
  • Simpler maintenance
  • Deployment without source code
  • Separate the script from the interface
  • Interface and Logic can be done in parallel
  • Compile errors are found without the need to
    request the page
  • Protect your code.
  • Promote code reuse

18
Code-Behind
  • Steps to use code-behind approach
  • Include header in the aspx file that refers to
    the code-behind file
  • Code-behind file shall be compiled into class
    library (.dll file) and inserted in the bin
    directory

19
Code-Behind HelloWorld.aspx
Page directive
  • lt_at_ Page language"c" Codebehind"HelloWorld.aspx
    .cs" Inherits"MyProject.HelloWorld" gt
  • ltHTMLgt
  • ltHEADgt
  • lttitlegtCode Behindlt/titlegt
  • lt/HEADgt
  • ltbodygt
  • ltform id"Form1" method"post" runat"server"gt
  • ltaspLabel id"lblHelloWorld" runat"server"gtHell
    o Worldlt/aspLabelgtltbrgt
  • ltaspButton id"Button1" runat"server"
    Text"Submit"gtlt/aspButtongt
  • lt/formgt
  • lt/bodygt
  • lt/HTMLgt

20
Code BehindHelloWorld.aspx.cs
  • namespace MyProject
  • public class HelloWorld System.Web.UI.Page
  • protected System.Web.UI.WebControls.Label
    lblHelloWorld
  • protected System.Web.UI.WebControls.Button
    Button1
  • private void Page_Load(object sender,
    System.EventArgs e)
  • // Put user code to initialize the page here
  • private void ClickedIt(object sender,
    System.EventArgs e)
  • lblHelloWorld.Text ".NET Rules!"

21
Code-Behind
  • Page Class
  • Inherited from System.UI.Page
  • Provide the base functionality that requires the
    ASP.NET to run

22
Code-BehindHelloWorld.aspx
  • lt_at_ Page language"c" Codebehind"HelloWorld.aspx
    .cs" Inherits"MyProject.HelloWorld" gt
  • Language Defines the programming language
  • Codebehind Set the filename of the code-behind
    file
  • Inherits Must be inherited from the Page class

23
Special Page Class Events
  • private void Page_Initfired when the page is
    first instantiated
  • First event fired in the page life cycle
  • All the controls for the page are created
  • Not used in your program, it is used by the VS to
    insert auto generated code
  • private void Page_Load fired after the page's
    controls are initialized, but before the page is
    rendered.
  • Fired after Page_Init
  • Page view state is available
  • Perfect place for run-time initialization
  • Visual Studio generates a stub for this event.
  • private void Page_Unload
  • Fired when the page is unloaded from memory
  • Good place to take care of any clean-up for the
    page

24
ASP.NET File Types
  • .aspx Web forms
  • .ascx Web forms user controls
  • .asmx Web services
  • .vb Visual Basic
  • .cs C
  • Global.asax Defines application- and
    session-variables
  • Web.config Configuration settings

25
Visual Studio Files
  • Global.asax
  • Used to handle special ASP.NET application such
    as Application_Start and Session_Start
  • Web.Config
  • XML file used for making overall application
    settings such as security and session state
  • .dosco
  • Used to advertise any web services that our
    website offer.
  • .aspx
  • ASP.NET file that defines the web page interface
  • .aspx.cs
  • C file that defines the business logic in the
    web page

26
Maintaining State
  • Input Controls
  • TextBox, ListBox, CheckBox, etc.
  • Automatically remember their state via
    "postback data sent in the HTTP header
  • Display Controls
  • Label, DataGrid, etc. (i.e. controls not set by
    user)
  • Automatically remember their state via
    "viewstate", a hidden control. State is
    automatically serialized into and out of the
    viewstate. This behavior can be turned off if
    desired.

27
Maintaining State
  • Cookies
  • Small chunks of text that are sent back and
    forth between the browser and the server.
  • Cookies have names and values.
  • Session cookies are valid only until the
    browser closes.
  • Persistent cookies have a specified lifetime
    independent of the browser's lifetime.
  • The page's Request and Response objects have
    cookie collections.

28
Saving State on the Server
  • Application
  • The same dataset is used by all sessions
  • Session
  • Users need to preserve their random number
    generators and how their last stories started

29
Application State
  • Values are stored in a hashtable available to all
    code in the application
  • A synchronization mechanism enables developers to
    coordinate concurrent access to state information
  • Other applications cannot access or modify values

30
Application State
  • There is a memory impact of storing application
    state.
  • Synchronization is important if simultaneous
    access is a possibility.
  • Locking resources can force other processes to
    operate in a serial manner.
  • Application state does not survive the tear-down
    of the hosting process.
  • Application state is not shared across a web farm
    (multiple servers) or web garden (multiple
    processors on a single server).

31
Session State
  • Very importantshopping carts, data scrolling,
    etc.
  • Can be released after a timeout period
  • Can survive IIS or worker process restarts. State
    does not have to be not stored in the worker
    process.
  • Scalableworks with web farms and web gardens
  • Must work with browsers that reject cookies
  • Good throughput required

32
Session State
  • In-process mode
  • State is stored in the worker process and lost if
    the process is recycled
  • The advantage is speed
  • Out-of-process mode
  • State server mode uses a Windows Service
    Aspnet_state.exe
  • State is memory-based.

33
(No Transcript)
34
Page-Level Events
  • In-process mode
  • State is stored in the worker process and lost if
    the process is recycled
  • The advantage is speed
  • Out-of-process mode
  • State server mode uses a Windows Service
    Aspnet_state.exe State is memory-based.
  • SQL Server mode persists the state information to
    a SQL server. Servers could be clustered to
    replicate data.
  • The choice is made in configuration files

35
Using the Global.asax file
  • Contains event handlers for responding to
    application- and session-level events
  • Resides in the root directory of the application
  • Optionalno handlers need be defined
  • Generated automatically by Visual Studio
  • Can use code-behind in Global.asax.cs

36
Using the Global.asax file
  • Contains event handlers for responding to
    application- and session-level events
  • Resides in the root directory of the application
  • Optionalno handlers need be defined
  • Generated automatically by Visual Studio
  • Can use code-behind in Global.asax.cs
  • protected void Application_Start(Object sender,
    EventArgs e)

37
Using the Global.asax file
  • Contains code for responding to application- and
    session-level events
  • Resides in the root directory of the application
  • Optionalno handlers need be defined
  • Generated automatically by Visual Studio
  • Can use code-behind in Global.asax.cs
  • protected void Application_Start(Object sender,
    EventArgs e)
  • Session_Start
  • Application_End
  • Session_End
  • Application_Error

38
Postback
  • private void Page_Load(object sender,
    System.EventArgs e)
  • //Place any code that needs to be executed on
    every request here. This
  • //could be things such as setting up a database
    connection that will
  • //be used throughout the processing of the page.
  • if(!IsPostBack)
  • //Place any code that needs to be executed only
    on first request
  • //here. This would include things such as
    retrieving static data
  • //from the database and populating drop-down
    lists or tables with
  • //this data.
  • else
  • //Place any code that needs to execute only on a
    postback here.
  • //This might include code that checks for some
    kind of user input
  • //that wouldnt be available when the page Is
    first requested.

39
Performance considerations
  • Disable session state when you are not using it
  • Choose the session state provider carefully
  • Avoid unnecessary round trips to the server
    (e.g., could validate input on the client)
  • Use IsPostBack to avoid unnecessary processing on
    a round trip
  • Save server control view state only when
    necessary
  • Don't rely on exceptions as a way to control
    program flow.
  • Use SQL Server DataReader for fast forward-only
    data access
  • See ASP.NET Caching for ways to cache page output
    or data
Write a Comment
User Comments (0)
About PowerShow.com