1 / 49

Chapter 1 Introducing ASP.NET 2.0

Chapter 1 Introducing ASP.NET 2.0. What is ASP.NET?. ASP.NET 2.0 is the current version of ASP.NET, Microsoft’s powerful technology for creating dynamic Web content.

vlora
Download Presentation

Chapter 1 Introducing ASP.NET 2.0

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 1Introducing ASP.NET 2.0

  2. What is ASP.NET? • ASP.NET 2.0 • is the current version of ASP.NET, Microsoft’s powerful technology for creating dynamic Web content. • is one of the key technologies of Microsoft's .NET Framework(which is both a development framework and software platform).

  3. Static vs Dynamic Web Pages • Static content: means that Web applications will deliver the same content of information to all users • Dynamic content: means that Web applications are personalized to the user and is responsive to the individual choices

  4. Static vs Dynamic Web Pages • Most Web pages that you view are not static HTML pages. • Instead they are dynamic generated content output from programs that run on servers. • These programs can interact with server resources like databases and XML Web services.

  5. Static Web Content

  6. Dynamic Web Content

  7. Dynamic Web Technologies • There are quite a number of different technologies for dynamically generating Web content. • ASP.NET • ASP • CGI • ColdFusion • JSP • PHP • Ruby on Rails • All of these technologies share one thing in common: • Using programming logic, they generate HTML on the server and send it back to the requesting browser.

  8. ASP.NET Advantages • ASP.NET provides a number of advantages compared to Microsoft’s earlier, "classic" ASP technology. • Better performance over ASP • because ASP.NET pages are compiled. • More powerful development environment. • It uses fully object oriented languages that work with a rich class library along with a very complete set of server-based controls that encapsulate common Web functionality that significantly reduces the amount of coding for Web developers. • Easier maintenance • Smoother deployment and configuration. • Due to the architecture of the .NET Framework.

  9. The .NET Framework • Many of the advantages that ASP.NET provides in comparison to other dynamic Web technologies are a result of its integration into Microsoft’s .NET Framework. • The .NET Framework is a development framework that provides a new programming interface to Windows services and APIs, and integrates a number of technologies that emerged from Microsoft during the late 1990s. • The .NET Framework is also a software platform for the running and deployment of Windows-based software systems

  10. Core Features of .NET • Language interoperability • A software system can be created using any combination of the available .NET languages. • Fully object-oriented languages • Common runtime engine shared by all languages • Base class library usable by all languages • Better security • Better performance

  11. .NET Architecture • The .NET Framework "sits" on top of the Windows operating system. • Consists of the following components: • Language compilers • Common Language Runtime • .NET Framework Base Class Library Source: Lam and Tai, .NET Framework Essentials, 3rd Edition (O'Reilly, 2003).

  12. ASP.NET Web Forms • An ASP.NET web application : • Consists of any number of web pages, controls, programming classes, web services, and other files • Residing within a single web server application directory • The principal component of an ASP.NET web application are its web pages. • These are text files with an .aspx extension and are called web forms. • Consist of two parts: • The declaratively-defined (i.e., by markup/tags) visual elements. • The programming logic.

  13. Web Form Programming Logic • A web form's programming logic can exist in either: • The same file as the visual elements • i.e., the .aspx file. • This code is contained within a code-declaration block. • In a separate class file. • The file is usually called a code-behind file. • By convention, its filename is same as .aspx file but with a language extension. • HelloWorld.aspx <- web form • HelloWorld.aspx.cs <- code-behind file

  14. HelloWorld.aspx Example <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC … > <script runat="server"> protected void Page_Load(object sender, EventArgs e) { myDate.Text = DateTime.Now.ToShortDateString(); } </script> <html> <head><title>Hello World Embedded</title></head> <body> <form id="form1" runat="server" > <h1>Hello World</h1> The date is <em> <asp:Label ID="myDate" runat="server"></asp:Label> </em> </form> </body> </html> Page directive Code declaration block Necessary to make this a web form Web server control

  15. Page Directive • The Page directive is used to define page-specific instructions used by the ASP.NET environment. • This directive indicates that the programming code in any script blocks will be C#. • Directives are processing instructions for the parser and compiler when they process an ASP.NET page. • Although directives can be located anywhere in an .aspx file, the standard practice is to place them at the beginning of the file. • Directive statements are not case sensitive and quotation marks are not required around the attribute values. <%@ Page Language="C#" %>

  16. <script> Element <script runat="server"> protected void Page_Load(object sender, EventArgs e) { myDate.Text = DateTime.Now.ToShortDateString(); } </script> • Notice that the <script> element contains a runat="server" attribute. • This tells the ASP.NET environment that the code within the block is to be executed on the server (i.e., it is not client-side Javascript or VBScript). • The code itself declares a page-level event handler method that sets the Text property of a control named myDate to a short version of the current date.

  17. <asp:Label> Web Control <asp:Label ID="myDate" runat="server"></asp:Label> • In the example’s markup, there is an element named <asp:Label>. • This is a predefined ASP.NET Web server control (covered in more detail later). • The markup for this Label control sets its ID property to the value myDate. • The Label control must contain the runat="server" attribute. • If it does not have it, the ASP.NET environment simply passes on the markup to the browser, where it will be ignored.

  18. <form> element <form id="form1" runat="server" > … </form> • All ASP.NET pages must contain a <form> element that contains this runat attribute. • All body content in a Web form should appear within this special <form> element.

  19. Result in the browser <html> <head><title>Hello World Embedded</title></head> <body> <form name="form1" method="post" action="HelloWorld.aspx" id="form1"> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJODExMDE5NzY5D2QWAgIDD2QWAgIBDw8WAh4EVGV4dAUKMDgvMDEvMjAwNmRkZDZPhFHJER4chf3nmlgfL+uq4W58" /> <h1>Hello World</h1> The date is <em> <span id="myDate">23/06/2006</span> </em> </form> </body> </html> Notice no <asp:Label> control. Notice also the hidden input tag with the name of __VIEWSTATE.

  20. Example using Code-Behind • Remember that a web form can alternately have its programming code contained in a separate class file called a code-behind file.

  21. Code Behind Version <%@ Page Language="C#" AutoEventWireup="true" CodeFile="HelloWorldCodeBehind.aspx.cs" Inherits="HelloWorldCodeBehind" %> <!DOCTYPE … > <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Hello World Code-</title> </head> <body> <form id="form1" runat="server" > <h1>Hello World</h1> The date is <em> <asp:Label ID="myDate" runat="server"></asp:Label> </em> </form> </body> </html> Page directive HelloWorldCodeBehind.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class HelloWorldCodeBehind : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { myDate.Text = DateTime.Now.Date.ToString(); } } HelloWorldCodeBehind.aspx

  22. Why use code-behind? • The real advantage of separating the code into its own file is that it may lead to more maintainable web forms. • One of the main benefits of ASP.NET is that a page’s programming logic can be conceptually separated from the presentation • by using a code-behind file a page’s programming logic can also be physically separated from the presentation/markup. • By placing the programming code into its own file, it is also potentially easier to make use of a division of labor in the creation of the site. • Use whichever model you want • However, all the examples in text use code-behind.

  23. Web Application Structure • An ASP.NET web application can simply consist of a folder containing web forms and other files. • You can, however, add any number of additional nested subfolders within this root folder. • ASP.NET in fact has a number of reserved application folder names, e.g. • App_Code • App_Data • App_Theme

  24. Configuration File • Every folder in a web application can contain an XML-format configuration file named web.config. • This configuration file is used to define security, connection strings, and other configuration information.

  25. Visual Studio • While you can create an ASP.NET application in any text editor, using Visual Studio will make developing ASP.NET applications easier. • Can also use the free Visual Web Developer 2005 Express Edition. • All files, folders, and settings in a Web application created with Visual Studio are contained within conceptual containers called solutions and projects. • Depending upon which way you use Visual Studio to construct your Web application, a solution may contain one or more additional projects as well as additional files and metadata about the project.

  26. Web Server Options • To test or run ASP.NET Web applications, web server software is necessary. • One of the advantages of using Visual Studio 2005 for ASP.NET development is that your site can be run and tested with or without using IIS. Visual Studio supports a variety of configurations: • local IIS, file system, FTP, remote IIS sites • Microsoft’s production web server software is Internet Information Services (IIS). • In order to run IIS, your computer’s operating system must be one of the following: • Windows 2000, • Windows XP Professional (not XP Home), • Windows Vista Business or Ultimate (not Vista Home Basic), • Windows Server 2003.

  27. Web Server Options • With Visual Studio 2005, we could say that your computer is a development server (available only to yourself). • Alternately, you might upload your work to a staging server running IIS for team testing. • Of course, if you want others to see this Web application, it must eventually be uploaded to a production server (available to your Web application’s audience).

  28. Web Server Options

  29. Introducing Server Controls • Normal HTML tags such as <input>, <H1> and <select> are not processed by the server but are sent to and displayed by the browser. • Server controls, in contrast, are tags that can be understood by the server. • Each ASP.NET server control has an object model containing properties, methods, and events.

  30. Introducing Server Controls • There are five kinds of server controls: • HTML server controls • Web server controls • Validation server controls • User controls • Custom server controls • All five of these different types of controls can be used within a single given Web form.

  31. 1.HTML Server Controls • Most standard HTML tags can be turned into HTML server controls simply by adding the runat="server" attribute. • This runat attribute indicates that the element is to be processed on the server. • You can this programmatically respond to events or bind data. • HTML server controls can be useful for: • situations in which you need complete control over how the HTML element will be rendered in the browser, or • when migrating an existing ASP page to ASP.NET. • Web server controls are almost always preferable to HTML server controls due to their richly typed object model.

  32. 2.Web Server Controls • Web server controls are also created on the server and they require a runat="server" attribute to work. • Some Web server controls represent traditional HTML form elements such as buttons and drop-down lists. • Other Web server controls represent more complex or abstract elements such as calendars, data lists, data sources, and tree views. <asp:Button ID="myButton" runat="server" />

  33. 3.Validation Controls • These controls allow you to test a user’s input for validity. • They are actually a special type of Web server control. • Validation controls encapsulate common user input validation checks required by most Web applications: • ensuring that a required field is not empty, • comparing an input value against another value, • checking if a value falls within a range, • verifying that an input value matches a given pattern.

  34. 4.User Controls • These are developer-created controls that use the same programming techniques used to write Web forms pages. • They typically allow the encapsulation of the functionality of multiple server controls along with other ASP.NET or HTML content in a single unit.

  35. 5.Custom Server Controls • A custom server control is a Web server control that you can create. • A custom control is a compiled class and may combine multiple existing server controls. • A custom control, unlike a user control, contains no declarative elements, and can be extended, use templates, support data binding, and be redistributed in a precompiled assembly.

  36. Web Server Control Overview • Web server controls are added to a Web form in the same way as any HTML element: • You can type the markup code in Source view in VS. • You can use drag-and-drop from Source or Design view. • As well, you can programmatically add controls at runtime. • Web Server Control Syntax: <asp:controlNameid="some_id" runat="server" «other attributes» > </asp:controlName>

  37. List of the Common Web Server Controls

  38. List of the Common Web Server Controls

  39. List of the Common Web Server Controls

  40. Common Members • Learning how to work with all of these controls might seem a daunting prospect. • Thankfully, Web server controls share a common object model which make the process of learning how to use Web server controls easier. Object Model

  41. Common Members • Most Web server controls inherit from the WebControl class. • This WebControl class in turn inherits from the Control class. • Both of these base classes define a variety of properties, methods, and events that are available to all controls derived from them. • Most of these modify the formatting and display of the controls.

  42. Some Properties of the WebControl Class

  43. Some Properties of the Control Class

  44. Subproperties • Properties can also have properties; • these are called subproperties. • For instance, the Font property is a complex object with properties of its own, such as Name and Size. • When working with subproperties programmatically, you use dot notation. • e.g., somecontrol.Font.Size=10; • When working with subproperties declaratively, you use hyphen (-) notation. • e.g., <asp:Label id="acontrol" Font-Size="10" runat="server" />

  45. Event Properties • All controls support events. • You can specify the event handler method for a given event declaratively by affixing the Onprefix to the event property name. • E.g., if you have an event handling method named btnOne_Clickthat you want to run when the user clicks a button, you would use: <asp:button id="btnOne" runat="server" OnClick="btnOne_Click" />

  46. Event Methods • All event methods in the .NET Framework are void methods with two parameters: • the source object that raised the event • the data for the event, usually contained within an EventArgs object (or an object derived from it). • Thus, the method signature for the event handler would be: public void btnOne_Click(object source, EventArgs e) { // code goes here }

  47. Color-Based Properties • All color-based properties use the Color structure. • The Color structure defines: • all the HTML 4.0 system-defined colors • the color names supported by most browsers • methods for retrieving and specifying color using different color models (such as HSB and RGB).

  48. Color-Based Properties • Acolor can be specified in at least four different ways, as shown here: // Set the color using predefined value labMsg.ForeColor = Color.Gold; // Set the color using a predefined name labMsg.ForeColor = Color.FromName("Gold"); // Set the color using RGB labMsg.ForeColor = Color.FromArgb(204, 153, 0); // Set the color using hexadecimal HTML color labMsg.ForeColor = Color.FromName("#CC9900");

  49. Collection Properties • Some properties are not a single structure, primitive, or enumerated type, but a collection of other objects. • An example of such a property is the Items collection of the DropDownList control. • This collection contains zero or more ListItem objects. • These collection properties have their own methods and properties for determining the size of the collection, as well for adding, retrieving, and removing items from the collection. DropDownListdrpSample = new DropDownList(); // Create the item, then add to collection ListItem li = new ListItem("Item 2"); drpSample.Items.Add(li); // Combine the creation and addition to collection steps drpSample.Items.Add(new ListItem("Item 1"));

More Related