ASP.NET 2.0 Overview Created By: Ajay Bahoriya
Agenda Web development using Visual Studio Page Inheritance model Deployment model Master Pages Themes Miscellaneous Summary Questions
Web development using Visual Studio Directory Based Project System Model Edit any web anywhere (no project file required) No longer required to build entire site into a single DLL Can now re-build just individual pages to test and run HTML Source Preservation Tool will never re-format or mangle your code Intellisense Everywhere Code Behind Inline Code Better code separation using folder hierarchies App_Code, App_LocalResources, etc.
Page Inheritance model How does ASP.NET 1.1 model works? You have Code Behind file that contains all the control declarations along with code. HTML Markup is contained in the ASPX page. Compilation – ASPX page s compiled into a class that inherits from code behind class. Problems with the above model? Every time a change is made in ASPX page, InitializeComponent section had to be regenerated. Try removing a control from ASPX page, most of the time VS2003 does not cleans up the code.
Page Inheritance model contd … ASP.NET 2.0: New code-behind model Works using Partial Classes Uses CodeFile attribute instead of CodeBehind No need to explicit declaration of controls in code behind class. So how does this works? Class for ASPX file generated by ASP.NET namespace ASP { public class default_aspx : MsdnMag.Default ... }
Page Inheritance model contd … Sibling partial class generated by ASP.NET namespace MsdnMag { public partial class Default : IRequiresSessionState protected TextBox _nameTextBox; protected Button _enterButton; protected Label _messageLabel; private HtmlForm form1; ... }
Page Inheritance model contd … Codebehind partial class that you write namespace MsdnMag { public partial class Default : Page void _enterButton_Click(object sender, EventArgs e) _messageLabel.Text = "Hello there " + _nameTextBox.Text + "!"; }
Deployment Model ASP.NET 2.0 comes up with new deployment modes All Binary All Source Updateable (Source Code in binary and ASPX files in source) Both Source Code and ASPX are compiled into binary. Each folder is compiled into separate Assembly. The ASPX pages present are just marker files and does NOT contain any code. PrecompiledApp.config – contains version and update mode (as False)
Deployment Model Contd… Updateable Source Code is compiled into binary ASPX files remain “as is”. Each folder is compiled into separate Assembly The ASPX pages present contain actual code. PrecompiledApp.config – contains version and update mode (as True) Source Code Source Code deployed “as is” Compiled during first hit and then cached Advantage – any code changes take effect immediately
Deployment Model Contd… Command Line: Aspnet_compiler Path: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 Usage: aspnet_compiler.exe -v /web -p "D: \Apoorv\Tutorial\ASP.NET 2.0\Master Pages and Themes\web" c:\deploy UI (VS2005): Build -> Publish Web Site Strong Naming possible during publishing.
Deployment Model Contd… Assembly Generation and pitfalls Each folder is compiled into separate assembly. E.g. Folder Hierarchy Admin – Admin1.aspx, Admin1.aspx.cs Assembly1 - Admin2.aspx, Admin2.aspx.cs Mobile – Mobile1.aspx, Mobile1.aspx.cs Assembly2 - Mobile2.aspx, Mobile2.aspx.cs This may cause your site to fail with change in deployment mode. Need to realize that classes are generated at runtime If the Page and the dynamically referenced UserControl (e.g. using LoadControl) are in different page, by default you cannot access the UserControl programmatically. Need to use <%@ Register %> tag on the page to refer to the userControl.
Master Pages Visual Inheritance for Web Pages .master file contains template used by all pages in the application. Other pages are contained as controls in the Master page. Content Place Holders Define where custom content will be placed. Can define default content. Can have multiple place holders per page. Design support in Visual Studio .NET 2005.
Master Pages contd… <%@ Master %> tag denotes Master page. <asp:contentplaceholder id="MainContentPlaceHolder" runat="server"> </asp:contentplaceholder> tag denotes the place holder for the contents. Content Page Specify the Master page using the “MasterPageFile” attribute in the <%@ Page %> tag. <asp:Content ContentPlaceHolderID=MainContentPlaceHolder ID=mainContent runat=server>CONTENTS </asp:Content> - contents are placed within above tag. Nesting of master pages is also possible.
Themes Personalization of Web Pages Works using .skin files Place .skin file in App_Themes folder Set the Theme property on the page. Can be set using “Theme” attribute of <%@ Page %> tag Can set Theme property programmatically as well. Has to be done on or before the Page_PreInit Sample .skin file <asp:Label Runat="server" ForeColor="Blue" Font--Names="Verdana" Font-Size="Small"/>
Miscellaneous Lets look at some more features provided by ASP.NET 2.0 Setting default focus Page.SetFocus() Java Script rendered on browser is: <script type="text/JavaScript"> <!-- WebForm_AutoFocus('ctl00_MainContentPlaceHolder_ddlName');// --> </script> Setting default button (for Enter Key) Panel.DefaultButton Designates button to be "clicked" when Enter is pressed with focus in panel <asp:Panel DefaultButton=“Button ID”….
Miscellaneous contd… Cross Page Posting Pages can now post back to other pages Relevant properties: control.PostBackUrl - Identifies postback target Page.PreviousPage - Returns reference to page that originated a cross-page postback PreviousPage.IsCrossPagePostBack - Reveals whether a cross-page postback occurred @ PreviousPageType directive provides strongly typed access to previous page Reference previous page control in the new page
Miscellaneous contd… E.g. Referencing Previous Page’s Controls <asp:Button ID="btnThemes" runat=server Text="Themes and Skins" PostBackUrl="~/Personal.aspx"/> Referencing Previous Page’s Controls Using PreviousPage object. PreviousPage.IsCrossPagePostBack Weak Typing PreviousPage.FindControl() Strong Typing <%@ PreviousPageType VirtualPath="~/Default.aspx" %> PreviousPage.<<Property>>
Summary New Page Inheritance Model in ASP.NET 2.0 Various Deployment Modes available with .NET 2.0 Master Page for consistent UI Web page / site personalization using Themes. Various controls / features available to reduce coding efforts
References For Page Inheritance and Deployment Model http://west-wind.com/weblog/posts/3016.aspx http://msdn.microsoft.com/msdnmag/issues/06/01/ExtremeASPNET/
Questions