ASP.NET
Rule One: Our client is always right Rule One: Our client is always right. Rule Two: If you think our client is wrong, see Rule One. Anonymous
25.1 Introduction ASP.NET 2.0 and Web Forms and Controls Web application development with Microsoft’s ASP.NET 2.0 technology Web Form files have the filename extension .aspx and contain the Web page’s GUI Every ASPX file created in Visual Studio has a corresponding class written in a .NET language, such as Visual Basic/ C# Contains event handlers, initialization code, utility methods and other supporting code Called the code-behind file Provides the ASPX file’s programmatic implementation
Creating and Running a Simple Web-Form Example Program consists of two related files ASPX file C# code-behind file Example Show the output Step-by-step process to create the program Present the code (much of which is generated by Visual Studio)
WebTime.cs Program Output WebTime ouput WebTime.cs Program Output
Creating and Running a Simple Web Form Example Adding Web Form for project WebTime (Right click on project in Solution Explorer)
Creating and Running a Simple Web Form Example Click on Add New Item and Add a Web Form for project WebTime .
Creating and Running a Simple Web Form Example ASPX file code-behind file
Creating and Running a Simple Web Form Example code-behind file ASPX file Solution Explorer window for project WebTime .
Creating and Running a Simple Web Form Example Toolbox in Visual Web Developer.
Creating and Running a Simple Web Form Example Design mode button Source mode of Web Forms designer.
Creating and Running a Simple Web Form Example Split mode of Web Forms designer.
Creating and Running a Simple Web Form Example Code-behind file for WebTime.aspx.cs generated by Visual Web Developer.
Designing the Page Designing a Web Form as simple as a Windows Form Use Toolbox to add controls to page in Design mode Control and other elements are placed sequentially on a Web Form position is relative to Web Forms upper left corner Alternate type layout (absolute positioning) is discouraged
Designing the Page label Web Form WebForm.aspx after adding Label and setting its properties.
Adding Page Logic Open WebTime.aspx.cs Add to Page_Load event handler //display the server's current time in timeLabel timeLabel.Text=DateTime.Now.ToString("hh:mm:ss");
Running the Program Can view the Web Form several ways Select Debug > Start Without Debugging runs the app by opening it in a browser window If created on a local file system URL http://localhost:PortNumber/WebTime/WebTime.aspx Debug>Start Debugging view web app in a web browser with debugging enabled Do you want IDE to modify the web.config file to enable debugging? Click OK Finally, can open web browser and type the web page’s URL in the Address field
WebTime.aspx Directive to specify information needed to process file This attribute determines how event handlers are linked to a control’s events <%-- Fig. 22.4: WebTime.aspx --%> <%-- A page that displays the current time in a Label. --%> <%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebTime.aspx.cs" Inherits="WebTime" EnableSessionState="False" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Simple Web Form Example</title> <style type="text/css"> #form1 { height: 255px; width: 655px; } .style1 font-size: large; </style> </head> AutoEventWireUp set to true so ASP.NET treats method of name Page_eventName as an event handler for a specified event Specify class in the code-behind file from which this ASP .NET document Document type declaration, specifies document element name and URI Title for web page
<form id="form1" runat="server"> <div> Body tag, beginning of Web page’s viewable content <body> <form id="form1" runat="server"> <div> <h2> Current time on the Web Server:</h2> <p> </p> </div> <asp:Label ID="timeLabel" runat="server" BackColor="Black" Font-Size="XX-Large" ForeColor="Lime"></asp:Label> </form> </body> </html> Attribute indicates the server processes the form and generates HTML for client The asp:Label control maps to HTML span element – markup for the Label Web control
// definitions for graphical controls used in Web Forms // WebTime.aspx.cs // The code-behind file for a page that displays the Web server's time. using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; // definitions for graphical controls used in Web Forms using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; public partial class WebTime : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) //display the server's current time in timeLabel timeLabel.Text = DateTime.Now.ToString("hh:mm:ss"); } Contains classes that manage client requests and server responses Contain classes for creation of Web-based applications and controls Event raised when Web page loads Set timeLabel’s Text property to Web server’s time
25.2.1 Examining an ASPX File Examining an ASPX File ASP.NET comments begin with <%-- and terminate with --%> Page directive Specifies the language of the code-behind file ASP.NET markup is not case-sensitive, so using a different case is not problematic
25.2.1 Examining an ASPX File asp: tag prefix in a control declaration runat attribute Indicates that when a client requests this ASPX file: Process the head element and its nested elements on the server Generate the corresponding XHTML Sent to the client asp: tag prefix in a control declaration Indicates that it is an ASP.NET Web control Each Web control maps to a corresponding XHTML element When processing a Web control, ASP.NET generates XHTML markup that will be sent to the client to represent that control in a Web browser span element Contains text that is displayed in a Web page
Outline ASP.NET comments WebTime.aspx Page directive to specify information needed by ASP.NET to process this file Document type declaration Mark up of a label web control
Retrieves the current time and formats it as hh:mm:ss Outline WebTime.aspx.vb Retrieves the current time and formats it as hh:mm:ss
25.2.5 Examining the XHTML Generated by an ASP.NET Application XHTML forms can contain visual and nonvisual components Attribute method Specifies the method by which the Web browser submits the form to the server Attribute action Identifies the name and location of the resource that will be requested when this form is submitted The runat attribute is removed when the form is processed on the server The method and action attributes are added The resulting XHTML form is sent to the client browser
Outline Declaration for a non-visual component: hidden input WebTime.html Declaration for a non-visual component: hidden input Represents the text in the label
Web Controls
25.3.1 Text and Graphics Controls
Set the color of a specific piece of text Outline WebControls.aspx (1 of 5) Set the color of a specific piece of text
Define a TextBox control used to collect the user’s first name Outline WebControls.aspx (2 of 5) Define a TextBox control used to collect the user’s first name
Outline Defines a DropDownList (3 of 5) WebControls.aspx (3 of 5) Each item in the drop-down list is defined by a ListItem element Adds a hyperlink to the web page
Outline (4 of 5) Defines a RadioButtonList control WebControls.aspx (4 of 5) Defines a RadioButtonList control Each item in the drop-down list is defined by a ListItem element Defines a Button web control
Outline WebControls.aspx (5 of 5)
Fig. 25.14 | DropDownList Tasks smart tag menu.