| |||||||||||||
Web Forms Syntax OverviewASP.NET Web Forms Programming Web Forms ASP.NET Web pages are created in a manner similar to static HTML Web pages ( pages that do not include server-based processing ), but they include extra elements that ASP.NET recognizes and processes when the page runs. The characteristics that distinguish ASP.NET Web pages from static HTML ( or other ) pages are as follows:
The sections below provide more details on each of these elements. You can rename any HTML page with the .aspx file name extension and it will run as an ASP.NET Web page. However, if a page does not involve server processing, you do not need to add the .aspx file name extension to it because doing so adds overhead to page processing. Example ASP.NET Web PageThe following code example shows a page that includes the basic elements that constitute an ASP.NET Web page. The page contains static text as you might have in an HTML page, along with elements that are specific to ASP.NET. The elements that are specific to ASP.NET are highlighted.
@ DirectivesASP.NET pages usually contain directives that allow you to specify page properties and configuration information for the page. The directives are used by ASP.NET as instructions for how to process the page, but they are not rendered as part of the markup that is sent to the browser. The most commonly used directive is the @ Page directive, which allows you to specify many configuration options for the page, including the following:
If you do not include an @ Page directive in the page, or if the directive does not include a specific setting, settings are inherited the from the configuration file for the Web application ( the Web.config file ) or from the site configuration file ( the Machine.config file ) . In addition to including an @ Page directive, you can include other directives that support additional page-specific options. Other common directives include the following:
Certain types of ASP.NET files use a directive other than @ Page. For example, ASP.NET master pages use an @ Master directive, and ASP.NET user controls use an @ Control directive. Each directive allows you to specify different options that are appropriate for the file. For details, see Introduction to Master Pages and ASP.NET User Controls. Form ElementsIf your page includes controls that allow users to interact with the page and submit it, the page must include a form element. You use the standard HTML form element, but certain rules apply. The rules for using the form element are as follows:
Web Server ControlsIn most ASP.NET pages, you will add controls that allow the user to interact with the page, including buttons, text boxes, lists, and so on. These Web server controls are similar to HTML buttons and input elements. However, they are processed on the server, allowing you to use server code to set their properties. These controls also raise events that you can handle in server code. Server controls use a special syntax that ASP.NET recognizes when the page runs. The following code example shows some typical Web server controls. <asp:TextBox ID="TextBox1" runat="server" /> <asp:Button ID="Button1" runat="server" Text="Click" OnClick="Button1_Click" /> The tag name for ASP.NET server controls starts with a prefix — in this case, When the page runs, it identifies the server controls and runs the code that is associated with those controls. Many controls render some HTML or other markup into the page. For example, the asp:textbox control renders an input element with the type="text" attribute into a page. However, there is not necessarily a one-to-one mapping between a Web server control and an HTML element. For example, the asp:calendar control renders an HTML table. Some controls do not render anything to the browser; instead, they are processed on the server only, and they provide information to other controls. HTML Elements as Server ControlsInstead of, or in addition to, using ASP.NET server controls, you can use ordinary HTML elements as server controls. You can add the runat="server" attribute and an ID attribute to any HTML element in the page. When the page runs, ASP.NET identifies the element as a server control and makes it available to server code. For example, you can add the required elements to an HTML body element, as shown in the following code example. <body runat="server" id="body"> You can then reference the body element in server code — for example, to set the body background color at run time in response to user input or to information from a database. For more information, see ASP.NET Web Server Controls. Server CodeMost ASP.NET pages include code that runs on the server when the page is processed. ASP.NET supports many languages including C#, Visual Basic, J#, Jscript, and others. ASP.NET supports two models for writing server code for a Web page. In the single-file model, the code for the page is in a script element where the opening tag includes the runat="server" attribute. The example earlier in this topic shows the single-file model. Alternatively, you can create the code for the page in a separate class file, which is referred to as the code-behind model. In this case, the ASP.NET Web page generally contains no server code. Instead, the @ Page directive includes information that links the .aspx page with its associated code-behind file. The following code example shows a typical @ Page directive for a page with a code-behind file.
The CodeFile attribute specifies the name of the separate class file, and the Inherits attribute specifies the name of the class within the code-behind file that corresponds to the page. For more information, see ASP.NET Web Page Code Model.
See Also |
| ||||||||||||
Check out related books at Amazon
© 2000-2008 Rey Nuñez All rights reserved.
If you have any question, comment or suggestion
about this site, please send us a note
You can help support aspxtreme