ASP.NET 2.0 ASP.NET 2.0 OverviewOverview
AgendaAgenda
Web development using Visual StudioWeb development using Visual StudioPage Inheritance modelPage Inheritance modelDeployment modelDeployment modelMaster PagesMaster PagesThemesThemesMiscellaneousMiscellaneousSummarySummaryQuestionsQuestions
Web development using Web development using Visual StudioVisual Studio
Directory Based Project System ModelDirectory Based Project System ModelEdit any web anywhere (no project file required)Edit any web anywhere (no project file required)No longer required to build entire site into a No longer required to build entire site into a single DLLsingle DLLCan now re-build just individual pages to test Can now re-build just individual pages to test and runand run
HTML Source PreservationHTML Source PreservationTool will never re-format or mangle your codeTool will never re-format or mangle your code
Intellisense EverywhereIntellisense EverywhereCode BehindCode BehindInline CodeInline Code
Better code separation using folder Better code separation using folder hierarchieshierarchies
App_Code, App_LocalResources, etc.App_Code, App_LocalResources, etc.
Page Inheritance modelPage Inheritance model
How does ASP.NET 1.1 model works?How does ASP.NET 1.1 model works?You have Code Behind file that contains all the You have Code Behind file that contains all the control declarations along with code.control declarations along with code.HTML Markup is contained in the ASPX page.HTML Markup is contained in the ASPX page.Compilation – ASPX page s compiled into a class Compilation – ASPX page s compiled into a class that inherits from code behind class.that inherits from code behind class.
Problems with the above model?Problems with the above model?Every time a change is made in ASPX page, Every time a change is made in ASPX page, InitializeComponent section had to be InitializeComponent section had to be regenerated. regenerated. Try removing a control from ASPX page, most of Try removing a control from ASPX page, most of the time VS2003 does not cleans up the code. the time VS2003 does not cleans up the code.
Page Inheritance model Page Inheritance model contd …contd …
ASP.NET 2.0: New code-behind modelASP.NET 2.0: New code-behind modelWorks using Partial ClassesWorks using Partial ClassesUses CodeFile attribute instead of CodeBehindUses CodeFile attribute instead of CodeBehindNo need to explicit declaration of controls in No need to explicit declaration of controls in code behind class.code behind class.So how does this works?So how does this works?
Class for ASPX file generated by ASP.NETClass for ASPX file generated by ASP.NETnamespace ASP namespace ASP {{ public class default_aspx : MsdnMag.Defaultpublic class default_aspx : MsdnMag.Default {{ ...... }}}}
Page Inheritance model Page Inheritance model contd …contd …
Sibling partial class generated by Sibling partial class generated by ASP.NETASP.NETnamespace MsdnMag namespace MsdnMag {{ public partial class Default : IRequiresSessionStatepublic partial class Default : IRequiresSessionState {{ protected TextBox _nameTextBox;protected TextBox _nameTextBox; protected Button _enterButton;protected Button _enterButton; protected Label _messageLabel;protected Label _messageLabel; private HtmlForm form1;private HtmlForm form1; ...... }}}}
Page Inheritance model Page Inheritance model contd …contd …
Codebehind partial class that you Codebehind partial class that you writewrite
namespace MsdnMag namespace MsdnMag {{ public partial class Default : Pagepublic partial class Default : Page {{ void _enterButton_Click(object sender, EventArgs e)void _enterButton_Click(object sender, EventArgs e) {{ _messageLabel.Text = "Hello there " + _nameTextBox.Text _messageLabel.Text = "Hello there " + _nameTextBox.Text
+ "!";+ "!"; }} }}}}
Deployment ModelDeployment ModelASP.NET 2.0 comes up with new ASP.NET 2.0 comes up with new deployment modesdeployment modes
All BinaryAll BinaryAll Source All Source Updateable (Source Code in binary and ASPX Updateable (Source Code in binary and ASPX files in source)files in source)
All BinaryAll BinaryBoth Source Code and ASPX are compiled into Both Source Code and ASPX are compiled into binary.binary.Each folder is compiled into separate Assembly.Each folder is compiled into separate Assembly.The ASPX pages present are just marker files The ASPX pages present are just marker files and does NOT contain any code.and does NOT contain any code.PrecompiledApp.config – contains version and PrecompiledApp.config – contains version and update mode (as False)update mode (as False)
Deployment ModelDeployment ModelContd…Contd…
Updateable Updateable Source Code is compiled into binarySource Code is compiled into binaryASPX files remain “as is”.ASPX files remain “as is”.Each folder is compiled into separate AssemblyEach folder is compiled into separate AssemblyThe ASPX pages present contain actual code.The ASPX pages present contain actual code.PrecompiledApp.config – contains version and PrecompiledApp.config – contains version and update mode (as True)update mode (as True)
Source Code Source Code Source Code deployed “as is”Source Code deployed “as is”Compiled during first hit and then cachedCompiled during first hit and then cachedAdvantage – any code changes take effect Advantage – any code changes take effect immediately immediately
Deployment ModelDeployment ModelContd…Contd…
Command Line: Aspnet_compilerCommand Line: Aspnet_compilerPath: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727Path: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727Usage: Usage: aspnet_compiler.exeaspnet_compiler.exe -v-v /web /web -p-p "D: "D:\Apoorv\Tutorial\ASP.NET 2.0\Master Pages and Themes\web" \Apoorv\Tutorial\ASP.NET 2.0\Master Pages and Themes\web"
c:\deployc:\deploy
UI (VS2005): Build -> Publish Web Site UI (VS2005): Build -> Publish Web Site Strong Naming possible during publishing. Strong Naming possible during publishing.
Deployment ModelDeployment ModelContd…Contd…
Assembly Generation and pitfallsAssembly Generation and pitfallsEach folder is compiled into separate assembly.Each folder is compiled into separate assembly.E.g. Folder HierarchyE.g. Folder Hierarchy
Admin – Admin1.aspx, Admin1.aspx.cs Assembly1Admin – Admin1.aspx, Admin1.aspx.cs Assembly1- Admin2.aspx, Admin2.aspx.cs- Admin2.aspx, Admin2.aspx.cs
Mobile – Mobile1.aspx, Mobile1.aspx.cs Assembly2Mobile – Mobile1.aspx, Mobile1.aspx.cs Assembly2- Mobile2.aspx, Mobile2.aspx.cs- Mobile2.aspx, Mobile2.aspx.cs
This may cause your site to fail with change in This may cause your site to fail with change in deployment mode.deployment mode.Need to realize that classes are generated at Need to realize that classes are generated at runtimeruntime
If the Page and the dynamically referenced UserControl If the Page and the dynamically referenced UserControl (e.g. using LoadControl) are in different page, by (e.g. using LoadControl) are in different page, by default you cannot access the UserControl default you cannot access the UserControl programmatically.programmatically.Need to use <%@ Register %> tag on the page to refer Need to use <%@ Register %> tag on the page to refer to the userControl.to the userControl.
Master PagesMaster PagesVisual Inheritance for Web PagesVisual Inheritance for Web Pages.master file contains template used by all pages in .master file contains template used by all pages in the application.the application.Other pages are contained as controls in the Other pages are contained as controls in the Master page.Master page.Content Place HoldersContent Place Holders
Define where custom content will be placed.Define where custom content will be placed.Can define default content.Can define default content.Can have multiple place holders per page.Can have multiple place holders per page.
Design support in Visual Studio .NET 2005.Design support in Visual Studio .NET 2005.
Master Pages Master Pages contd…contd…
<%@ Master<%@ Master %> tag denotes Master page. %> tag denotes Master page.<asp:contentplaceholder <asp:contentplaceholder id="MainContentPlaceHolder" runat="server"> id="MainContentPlaceHolder" runat="server"> <<//asp:contentplaceholderasp:contentplaceholder> tag denotes the place > tag denotes the place holder for the contents.holder for the contents.Content PageContent Page
Specify the Master page using the Specify the Master page using the “MasterPageFile” attribute in the <%@ Page %> “MasterPageFile” attribute in the <%@ Page %> tag.tag.<asp:Content <asp:Content ContentPlaceHolderID=MainContentPlaceHolder ContentPlaceHolderID=MainContentPlaceHolder ID=mainContent runat=server>ID=mainContent runat=server>CONTENTS CONTENTS <<//asp:Contentasp:Content> - contents are placed within > - contents are placed within above tag.above tag.Nesting of master pages is also possible.Nesting of master pages is also possible.
ThemesThemesPersonalization of Web PagesPersonalization of Web PagesWorks using .skin filesWorks using .skin filesPlace .skin file in App_Themes folderPlace .skin file in App_Themes folderSet the Theme property on the page.Set the Theme property on the page.
Can be set using “Can be set using “ThemeTheme” attribute of <%@ Page %> tag” attribute of <%@ Page %> tagCan set Theme property programmatically as well.Can set Theme property programmatically as well.
Has to be done on or before the Page_PreInit Has to be done on or before the Page_PreInit Sample .skin fileSample .skin file<asp:Label Runat="server" ForeColor="Blue" Font--<asp:Label Runat="server" ForeColor="Blue" Font--
Names="Verdana"Names="Verdana" Font-Size="Small"/>Font-Size="Small"/>
MiscellaneousMiscellaneousLets look at some more features provided by ASP.NET Lets look at some more features provided by ASP.NET 2.02.0
Setting default focusSetting default focusPage.SetFocus()Page.SetFocus()Java Script rendered on browser is:Java Script rendered on browser is:
<script type="text/JavaScript"><script type="text/JavaScript"><!--<!--WebForm_AutoFocus('ctl00_MainContentPlaceHolder_ddlNameWebForm_AutoFocus('ctl00_MainContentPlaceHolder_ddlName
');// -->');// --></script></script>
Setting default button (for Enter Key)Setting default button (for Enter Key)Panel.DefaultButtonPanel.DefaultButtonDesignates button to be "clicked" when Enter is Designates button to be "clicked" when Enter is pressed with focus in panelpressed with focus in panel<asp:Panel DefaultButton=“Button ID”….<asp:Panel DefaultButton=“Button ID”….
MiscellaneousMiscellaneouscontd…contd…
Cross Page PostingCross Page PostingPages can now post back to other pagesPages can now post back to other pagesRelevant properties:Relevant properties:
controlcontrol.PostBackUrl.PostBackUrl - Identifies postback target - Identifies postback targetPage.PreviousPagePage.PreviousPage - Returns reference to page that - Returns reference to page that originated a cross-page postbackoriginated a cross-page postbackPreviousPage.IsCrossPagePostBackPreviousPage.IsCrossPagePostBack - Reveals whether a - Reveals whether a cross-page postback occurredcross-page postback occurred
@ PreviousPageType@ PreviousPageType directive provides strongly directive provides strongly typed access to previous pagetyped access to previous pageReference previous page control in the new pageReference previous page control in the new page
MiscellaneousMiscellaneouscontd…contd…
E.g.E.g.<asp:Button ID="btnThemes" runat=server <asp:Button ID="btnThemes" runat=server Text="Themes and Skins" Text="Themes and Skins" PostBackUrl="~/Personal.aspx"/>PostBackUrl="~/Personal.aspx"/>Referencing Previous Page’s ControlsReferencing Previous Page’s Controls
Using Using PreviousPagePreviousPage object. object.PreviousPage.IsCrossPagePostBackPreviousPage.IsCrossPagePostBackWeak TypingWeak Typing
PreviousPagePreviousPage.FindControl().FindControl()Strong TypingStrong Typing
<%@ PreviousPageType VirtualPath="~/Default.aspx" <%@ PreviousPageType VirtualPath="~/Default.aspx" %>%>PreviousPagePreviousPage.<<Property>>.<<Property>>
SummarySummaryNew Page Inheritance Model in New Page Inheritance Model in ASP.NET 2.0ASP.NET 2.0Various Deployment Modes available Various Deployment Modes available with .NET 2.0with .NET 2.0Master Page for consistent UIMaster Page for consistent UIWeb page / site personalization using Web page / site personalization using Themes.Themes.Various controls / features available Various controls / features available to reduce coding effortsto reduce coding efforts
ReferencesReferencesFor Page Inheritance and For Page Inheritance and Deployment ModelDeployment Model
http://west-wind.com/weblog/posts/http://west-wind.com/weblog/posts/3016.aspx3016.aspxhttp://msdn.microsoft.com/msdnmag/http://msdn.microsoft.com/msdnmag/issues/06/01/ExtremeASPNET/issues/06/01/ExtremeASPNET/
QuestionsQuestions