ASP.NET Lecture 1


Published on

Lecture 1 contains
Introduction to the Web
ASP.NET overview
ASP.NET Controls
Page Class

Published in: Technology
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Difference between dynamic and static sites
  • Web Pages ASP.NET Web Pages and the new Razor syntax provide a fast, approachable, and lightweight way to combine server code with HTML to create dynamic web content. Connect to databases, add video, link to social networking sites, and include many more features that let you create beautiful sites using the latest web standards.MVCASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that enables a clean separation of concerns and that gives you full control over markup for enjoyable, agile development. ASP.NET MVC includes many features that enable fast, TDD-friendly development for creating sophisticated applications that use the latest web standards.Web FormsASP.NET Web Forms lets you build dynamic websites using a familiar drag-and-drop, event-driven model. A design surface and hundreds of controls and components let you rapidly build sophisticated, powerful UI-driven sites with data access.
  • First Hello World Then say HI
  • Default form tag html rendering <form method=“post” action=“samepage.aspx” id=“form”>
  • Default form tag html rendering <form method=“post” action=“samepage.aspx” id=“form”>
  • Separation of Concerns
  • Directives  are commands to the compiler when compiling a page or control<%@ [Directive] [Attribute=Value] %>Page Directive controlbehaviour of pages
  • ASP.NET is OO so everything is represented with classes and objectsThe application is starts once the first request to the application and must not be restarted often, except for maintenance.The page is compiled and cached on first request.Each Application has a virtual directory on the web server.ASP.NET is an event-driven way of making web applications. There are:1- global application events.2- page related events3- control related eventsNote : Eventually, the page is rendered into HTML.
  • AllControls page
  • Viewstate can be encrypted
  • Page InitializationFire Page.Init (controls maynot be created and viewstate information isn’t loaded yet) generates all controls defines with tags in .aspx If postbacked deserialize the viewstate information and apply it to the controls.User Code Initialization  Page.Load firedValidation After Page.Load and before events handlingEvent Handling  event model is just an emulationAutomatic Data Binding  automatically performs updates and queries against data source controls 1- changes submited 2- Page.PreRender 3- Queries and controls bound Event handlers wont have access to the most recent data, not yet retrievedCleanUp Page.Unload , no change to controls since HTML already rendered
  • PageEvents page
  • To access all HTTP context information from a non-page class, use System.Web.HttpContext class, HttpContext.Current static property, returns instance of HTTPContext representing all info on current request and response
  • PageControls pageDynamicControl pageRemember on postback dynamically created controls won’t be recreated automaticallyUse unique id to reach it or using recursive search
  • Windows developers are accustomed to a rich event model that lets reacts to mouse movements, key presses and control interaction. In ASP.NET, responding to events are handled by the server. It adds an amount of overhead to respond. Rapid events are impractical.For UI effects, use client-side javascript or AjaxIn HTML, there is only one way to submit a form  a submit buttonAutoPostBack available with web controls only
  • Classical ASP /PHP developer do it manually and write the code themselves
  • PostBackPage pageAutopostback page
  • ASP.NET Lecture 1

    1. 1. ASP.NET 4.0Julie IskanderMSC. Communication and Electronics
    2. 2. Lab Setup Windows 7 (not limited edition) IIS 7.x MS SQL Server 2008 + MS Visual Studio 2010 +
    3. 3. Lecture Outlines Introduction to the Web ASP.NET overview ASP.NET Controls Page Class
    4. 4. Introduction to theWeb
    5. 5. Introduction to the Web Client/Server Architecture Web Technologies (Client Side Versus Server Side)
    6. 6. Client/Server Architecture What is a web client? What is a web server? Web server examples (IIS, Apache, nginx,GWS, lighttpd…………etc.)
    7. 7. Client/Server Architecture
    8. 8. Web TechnologiesClient Side and Server Side
    9. 9. ASP.NET Overview
    10. 10. ASP.NET Overview What is ASP.NET? ASP.NET and MS Visual Studio Example How do ASP.NET works? PostBack Example
    11. 11. What is ASP.NET? ASP.NET is a free web framework for building WebSites and WebApplications using HTML, CSS and JavaScript and .Net Framework.  Microsoft definition from ASP.NET is a WebApplication framework developed by Microsoft to allow programmers to build dynamic WebSites, WebApplications and WebServices. First released, January 2002 with .NET Framework 1.0, and is the successor to Microsoft’s ASP technology.  From
    12. 12. ASP.NET and MS. Visual Studio Unified web development model for building web applications. A new programming model and infrastructure. Separates code from HTML Provides a GUI designer and a fully integrated debugging support. Is a compiled, .NET based environment. Event-driven model Applications are written in any .NET compatible language (VB.NET, C#,…., etc.)
    13. 13. ASP.NET and MS. VisualStudio ASP.NET supports three approaches to build web sites: ◦ Web Pages using WebMatrix ◦ MVC ◦ Web Forms
    14. 14. ASP.NET and MS. VisualStudio
    15. 15. Convert from an HTML to an aspx page
    16. 16. Define: RoundTrip, PostBack
    17. 17. PostBack An HTTP POST to the same page that the form is on. The contents of the form are POSTed back to the same URL as the form. Allows a page to perform validation and processing on its own form data.
    18. 18. PostBack
    19. 19. ASP.NET Coding Model Inline Code Model Code Behind Model ◦ We work with this model for better organization of code and separation of concerns
    20. 20. ASP.NET ASP.NET is OO so everything is represented with classes and objects The application starts once the first request to the application and must not be restarted often, except for maintenance. The page is compiled and cached on first request. Each Application has a virtual directory on the web server. ASP.NET is event-driven. There are: 1. global application events. 2. page related events 3. control related events Note : Eventually, the page is rendered into HTML.
    21. 21. How do ASP.NET works? Runs on the web server. Renders appropriate markup (HTML) to the requesting browser. Completely object-oriented. Works with HTML elements using properties, methods, and events. Controls look and feel through skins, themes and master-pages
    22. 22. How do ASP.NET works? A simple request flow: 1. A page is requested by a client 2. Web server sends it to the worker process (w3wp.exe) 3. Worker process processes the request and returns response a. Instantiates a page object b. Run page events c. Instantiates control objects d. Run control events e. Render page to html f. Destroys page object 4. Web server sends response to the client
    23. 23. How do ASP.NET works? HTTP is a stateless protocol. In ASP.NET, after page rendering to HTML, the page objects is destroyed. However, ASP.NET has state management techniques using session state, view state, cookies, query string,…… to save user information and controls state.
    24. 24. ASP.NET Page ObjectGeneration A merge between the generated code, and the partial class you write in the *.aspx.cs file
    25. 25. ASP.NET Controls
    26. 26. ASP.NET Controls HTML Controls HTML Server Controls Web Controls
    27. 27. HTML Controls Ordinary XHTML tags Stateless Aren’t accessed in server code except if runat=“server” attribute is added  HTML Server Control
    28. 28. HTML Server Controls Equivalents for standard HTML elements. Provide an object interface for HTML elements. Retain their state between postbacks The HTML Control Classes defined in the System.Web.UI.HtmlControls namespace. Fire server-side events ◦ ServerClick  actually post back the page, ◦ ServerChange  it doesn’t occur until the page is posted back.
    29. 29. HTML Server Controls
    30. 30. Web Controls Provide Windows closely-resembled controls. Feature user interface elements that may have/haven’t a direct HTML equivalent, such as the Label, TextBox, GridView, Calendar, and validation controls. Adaptive rendering
    31. 31. Web Controls
    32. 32. Web Controls
    33. 33. Convert from an HTML to an aspx page
    34. 34. RememberThe web form isrecreated with everyround-trip. It doesn’tpersist or remain inmemory longer than ittakes to render a singlerequest.
    35. 35. RememberOnly one form canhas a runat=serverattribute in eachpage
    36. 36. View State ASP.NET has an integrated state serialization mechanism. Hidden field to store information, in a compressed format, about the state of every control in the page. Advantage free server resources Disadvantage  bigger page size, longer receive and post time
    37. 37. View State Serialization is the process of converting an object into a stream of bytes in order to persist it to memory, a database, or a file. Its main purpose is to save the state of an objet in order to be able to recreate it when needed. The reverse process is called deserialization. ViewState is serialized as a Base64 string
    38. 38. Viewstate Enable ViewState property, to enable the storage of controls values. ◦ EnableViewState=false, a small amount of viewstate info is still stored (controlstate), it can never be disabled. ◦ EnableViewstate=false, has effect on datagrids
    39. 39. Page Class
    40. 40. Page Class Page Lifecycle Page Class properties Page as a Container Event Model How PostBack Events Work?
    41. 41. Page Life Cycle
    42. 42. Convert from an HTML to an aspx page
    43. 43. Page Class Inherits System.Web.UI.Page Basic Property ◦ IsPostBack :  boolean ◦ Request:  HttpRequest object info about the user’s browser, to transmit information from one page to another using query string, to RETRIEVE cookies, ◦ Response:  HttpResponse object to redirect to a different web page, to CREATE cookies. ◦ Server:  HttpServerUtility object tasks as to encode text
    44. 44. Response.Redirect() Server.Transfer() Sends a redirect msg to  Starts processing the client, which then sends new page and sends the a request for the new rendered HTML. page (round trip)  Can’t access pages Can redirect to any page outside web in the website or any application, or non- absolute URL. ASP.NET page. Client Browser reflects  Client Browser URL isn’t the URL Change changed  no bookmark support Sending user to another page
    45. 45. Page As A Control Container After creating page object Foreach element with runat=server attr. ◦ Create control object ◦ Configure properties ◦ Add to controls collection of the page The same happened recursively for nested controls
    46. 46. Page As A Control Container Page.Controls Page.FindControl(“ControlID”);
    47. 47. Convert from an HTML to an aspx page
    48. 48. AVOID INITIAIZING INRemember: CODE, USE CONTROL TAG PROPERTIES.Initializing a control inPage.Load counts as achange and is stored inthe viewstate.
    49. 49. Events Model AutoPostBack Events, Event Handling happens in two ways:Wait for next postback Automatic postbackA single postback results Force a control toin several change postback, which fire oneafter the other, in anundetermined order.
    50. 50. How PostBack Events Work?
    51. 51. How PostBack Events Work? On Server Side: ◦ ASP.NET re-creates the Page object. ◦ ASP.NET retrieves state info from the hidden viewstate field to update the controls. ◦ The Page.Load event is fired. ◦ The appropriate change events are fired for the controls. ◦ The Page.PreRender event fires, and the page is rendered. ◦ The Page.Unload event is fired. ◦ The new page is sent to the client.
    52. 52. Reading Assignment 1 ASP.NET Page Life Cycle Overview , us/library/ms178472.aspx View State Chunking
    53. 53. Report #1 What is the difference between a web page and a web site and a web application? What’s the difference between Website project and web application project in visual studio?
    54. 54. Lab #1The aim of the labs is to create awebsite to sell books, the site will becalled First I need to register my data (name, gender, age, preferences, country, city) (register.aspx) Then I will browser through books images and titles that are put in a table structure by default 2 rows and 2 columns with a more link for more data.
    55. 55. Lab hints Lab steps: ◦ Put your controls ◦ Add styles necessary ◦ Start adding functionality Use html controls whenever possible Html hints ◦ Search for fieldset and legend tags Use ◦ Request.QueryString ◦ HyperLink.NavigationUrl ◦ HyperLink.Enabled Enjoy ASP.NET!!!
    56. 56. Report #1 What is the difference between a web page and a web site and a web application? What’s the difference between Website project and web application project in visual studio?
    57. 57. backREFERENCES [1] Beginning ASP.NET 4 In C# 2010, Matthew Macdonald, Apress [2] Web Application Architecture Principles, Protocols And Practices, Leon Shklar And Richard Rosen, Wiley [3] Professional AS P.NE T 4 In C# And VB, Bill Evjen, Scott Hanselman And Devin Rader, Wiley [4] Pro ASP.NET In C# 2010, Fourth Edition,matthew Macdonald, Adam Freeman, And Mario Szpuszta, Apress