1 / 112

ASP.NET coding models

ASP.NET coding models. An ASP.NET Web page consists of two parts: Visual elements, which include markup, server controls, and static text. 2. Programming logic for the page, which includes event handlers and other code. ASP.NET coding models. ASP.NET supports two methods to author pages:

elvina
Download Presentation

ASP.NET coding models

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. ASP.NET coding models An ASP.NET Web page consists of two parts: Visual elements, which include markup, server controls, and static text. 2. Programming logic for the page, which includes event handlers and other code.

  2. ASP.NET coding models • ASP.NET supports two methods to author pages: • In-line code – (Single File) • Code-behind

  3. In-line code – (Single File) try.aspx <%@ Language=C# %> <HTML> <script runat="server" language="C#"> void MyButton_OnClick(Object sender, EventArgs e) { MyLabel.Text= MyTextbox.Text.ToString(); } </script> <body> <form id="MyForm" runat="server"> <asp:textbox id="MyTextbox" text="Hello World" runat="server"></asp:textbox> <asp:button id="MyButton" text="Echo Input" OnClick="MyButton_OnClick" runat="server"></asp:button> <asp:label id="MyLabel" runat="server"></asp:label> </form> </body> </HTML>

  4. Code-behind mycodebehind.aspx <%@ Page Language="C#" AutoEventWireup="false" CodeFile="Class1.cs" Inherits="MyStuff.MyClass" %> <HTML> <body> <form id="MyForm" runat="server"> <asp:textbox id="MyTextBox" text="Hello World" runat="server"></asp:textbox> <asp:button id="MyButton" text="Echo Input" Onclick="MyButton_Click" runat="server"> </asp:button> <asp:label id="MyLabel" runat="server" /> </form> </body> </HTML>

  5. mycodebehind.cs using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace MyStuff { public partial class MyClass : Page { public void MyButton_Click(Object sender, EventArgs e) { MyLabel.Text= MyTextBox.Text.ToString(); } } }

  6. In the preceding sample, the code-behind page is compiled before ASP.NET runs. Alternatively, you can reference the code-behind class by using an SRC tag as follows: • <%@ Language="C#" Inherits="MyStuff.MyClass" src="MyCodebehind.cs" %> • In this case, ASP.NET compiles the code-behind page on the fly.

  7. Advantages of Single-File Pages As a rule, the single-file model is suitable for pages in which the code consists primarily of event handlers for the controls on the page. • Advantages of the single-file page model include the following: • it can be easier to study a single-file page because you can see the code and the markup in one place. • Pages written using the single-file model are slightly easier to deploy or to send to another programmer because there is only one file. • Because there is no dependency between files, a single-file page is easier to rename. • Managing files in a source code control system is slightly easier, because the page is self-contained in a single file.

  8. Advantages of Code-Behind Pages Code-behind pages offer advantages that make them suitable for Web applications with significant code or in which multiple developers are creating a Web site. • Advantages of the code-behind model include the following: • Code-behind pages offer a clean separation of the markup (user interface) and code. It is practical to have a designer working on the markup while a programmer writes code. • Code is not exposed to page designers or others who are working only with the page markup. • Code can be reused for multiple pages.

  9. ASP.NET Page Directives • Directives specify settings that are used by the page and user-control compilers when the compilers process ASP.NET Web Forms pages (.aspx files) and user control (.ascx) files. • ASP.NET treats any directive block (<%@ %>) that does not contain an explicit directive name as an @ Page directive (for a page) or as an @ Control directive (for a user control).

  10. At run time, master pages are handled in the following sequence: • Users request a page by typing the URL of the content page. • When the page is fetched, the @ Page directive is read. If the directive references a master page, the master page is read as well. If this is the first time the pages have been requested, both pages are compiled. • The master page with the updated content is merged into the control tree of the content page. • The content of individual Content controls is merged into the corresponding ContentPlaceHolder control in the master page. • The resulting merged page is rendered to the browser.

  11. <%@ Import namespace="value" %> • The @ Import directive cannot have more than one namespace attribute. To import multiple namespaces, use multiple @ Import directives. • A set of namespaces can be automatically imported into .aspx pages. The imported namespaces are defined in the machine-level Web.config file, within the <namespaces> section of the <pages> element. The following namespaces are automatically imported into all pages: • System • System.Collections • System.Collections.Specialized • System.Configuration • System.Text • System.Text.RegularExpressions • System.Web • System.Web.Caching • System.Web.Profile • System.Web.Security • System.Web.SessionState • System.Web.UI • System.Web.UI.HtmlControls • System.Web.UI.WebControls • System.Web.UI.WebControls.WebParts

  12. <%@ Implements interface="ValidInterfaceName" %>

  13. Page directive • <%@ Page attribute="value" [attribute="value"...] %> • Some of the attributes of page directive are given below

  14. Async Makes the page an asynchronous handler (that is, it causes the page to use an implementation of IHttpAsyncHandler to process requests). The default is false. AsyncTimeOutDefines the time-out interval (in seconds) used when processing asynchronous tasks. The default is 45 seconds.The value must be an integer.

  15. Language Specifies the language used when compiling all inline rendering (<% %> and <%= %>) and code declaration blocks within the page. • Values can represent any .NET Framework-supported language, including Visual Basic, C#, or JScript. • Only one language can be used and specified per page

  16. Src Specifies a path to a source file containing code that is linked to the page. In the linked source file, you can choose to include programming logic for your page either in a class or in code declaration blocks.

  17. ClassName Specifies the class name for the class that is automatically generated from the markup and compiled when the master page is processed. This value can be any valid class name and can include a namespace. • CodeFile Specifies the name of a separate file that contains a partial class with the event handlers and other master page–specific code. For more information, see ASP.NET Web Page Code Model

  18. <%@ Page Language="C#" AutoEventWireup="false" CodeFile="Class1.cs" Inherits="MyStuff.MyClass" %>

  19. Postback Posting • Every time page is submitted back to itself so that it can run its server code again to produce the new version of itself for the user. • Login form • Combo box - ‘Auto PostBack’ property for that control set = true

  20. script language="VB" runat="server"> Sub Test_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  'enter your code to perform End Sub </script> <html> <body> <form runat="server" id="myForm">      <asp:linkbutton id="Test" runat="server" text="Create Text file" onclick="Test_Click" />     </form> </body> </html>

  21. <html> <body>     <form name="myForm" method="post" action="test.aspx" id="myForm">      <input type="hidden" name="__EVENTTARGET" value="" /> <input type="hidden" name="__EVENTARGUMENT" value="" /> <input type="hidden" name="__VIEWSTATE“  value="dDwtMTAwOTA0ODU1NTs7PsllCK1DzvZsK0J6OQ2dxKqmEwVs" /> <script language="javascript"> <!– function __doPostBack(eventTarget, eventArgument) { vartheform = document.myForm; theform.__EVENTTARGET.value = eventTarget; theform.__EVENTARGUMENT.value = eventArgument; theform.submit(); } --> </script> <a id="Test" href="javascript:__doPostBack('Test','')">Create Text file</a> </form> </body> </html>

  22. Cross Page Posting • When you build dynamic web site you may post one page to another with some information. • E.g baking takes palce with buttons control

  23. Client side Response.BufferOutput = true; if (UserLanguage == "English") { Response.Redirect("http://www.microsoft.com/gohere/look.htm"); } else if (UserLanguage == "Deutsch") { Response.Redirect("http://www.microsoft.com/gohere/look_deu.htm"); } else if (UserLanguage == "Español") { Response.Redirect("http://www.microsoft.com/gohere/look_esp.htm"); }

  24. Server side protected void Button1_Click(object sender, System.EventArgs e) { Server.Transfer("Page2.aspx", true); }

  25. Page Events and page life cycle • When you send a request to the web server for a page , the page runs through series of events during its creation and disposal. • Collection of events as a whole is known as page’s life cycle.

  26. Events

  27. Example of page_load using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; namespace WebApplication3 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } } }

  28. Application Compilation Models • Normal compilation Model – Code behind file – compiled - /bin , web page compilation on demand first request slow ASP .NE 2.0 still support • Deployment Pre-Compilation Full compilation prior to deployment Assemblies contain compiled code and resource files Configuration files are copied without making modification • Full Run Time Compilation compiles entire application at run time uncomplied files (code behind and code files) in \App_Code assemblies get created and maintained at run time – flexibility

  29. ASP.Net Controls • Custom Control - Programmers can also build custom controls for ASP.NET applications. Unlike user controls, these controls do not have an ASCX markup file, having all their code compiled into a dynamic link library (DLL) file. • User Control - User controls have their own events which are handled during the life of ASP.NET requests • HTML Controls • ASP.NET Server Controls • Validation Controls

  30. ASP.NET Controls Features • Automatic state management. • Simple access to object values without having to use the Request object. • Ability to react to events in server-side code to create applications that are better structured. • Common approach to building user interfaces for Web pages. • Output is automatically customized based on the capabilities of the browser.

  31. HTML Controls

  32. ASP.NET HTML Controls • By default, HTML elements on an ASP.NET Web page are not available to the server; they are treated as opaque text that is passed through to the browser. However, by converting HTML elements to HTML server controls, you expose them as elements you can program in server-based code.

  33. ASP.NET HTML Controls • By default, HTML elements on a Web Forms page are not available to the server; they are treated as markup that is passed through to the browser. However, if you add an id attribute and the attribute runat="server” • ASP.NET recognizes the element as a control on the page and you can program it with server-based code. If you convert an HTML DIV element to an ASP.NET server control, it is created as an instance of the HtmlGenericControl class.

  34. Anchor • <html><body><form runat="server"><a id="link1" runat="server">Visit W3Schools!</a><br /><a id="link2" runat="server">Visit Microsoft!</a></form></body></html>

  35. <script  runat="server">Sub Page_Load   link1.HRef="http://www.w3schools.com"   link1.Target="_blank"   link1.Title="W3Schools"   link2.HRef="http://www.microsoft.com"   link2.Target="_blank"   link2.Title="Microsoft"End Sub </script>

  36. ASP.Net HTML controls - button <script  runat="server">Sub button1(Source As Object, e As EventArgs)   p1.InnerHtml="You clicked the blue button!"End SubSub button2(Source As Object, e As EventArgs)   p1.InnerHtml="You clicked the pink button!"End Sub</script> <html><body><form runat="server“> <button id="b1" OnServerClick="button1”style="background-color:#e6e6fa;height:25;width:100" runat="server“>Blue button!</button> <button id="b2“ OnServerClick="button2“ style="background-color:#fff0f5;height:25;width:100" runat="server“>Pink button!</button> <p id="p1" runat="server" /></form> </body></html>

  37. Image HTML control <script  runat="server">Sub Page_Load(Sender As Object,E As EventArgs)   image1.Src="smiley.gif"   image1.Alt="Smiley"   image1.Border="3"End Sub</script><html><body><form runat="server"><img id="image1" runat="server" /></form></body></html>

More Related