1 / 33

Cool Tips and Tricks with ASP.NET 2.0

Cool Tips and Tricks with ASP.NET 2.0. Stefan Schackow Program Manager ASP.NET Team Microsoft Corporation. Agenda. Cross Page Post-backs Validation Groups Wizard Control URL Rewriting/Mapping Compilation Build Providers and .WSDL Files “No-Compile” Pages

malinda
Download Presentation

Cool Tips and Tricks with ASP.NET 2.0

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. Cool Tips and Tricks with ASP.NET 2.0 Stefan Schackow Program Manager ASP.NET Team Microsoft Corporation

  2. Agenda • Cross Page Post-backs • Validation Groups • Wizard Control • URL Rewriting/Mapping • Compilation Build Providers and .WSDL Files • “No-Compile” Pages • Building CMS System w/ File System Provider • Securing non-ASP.NET web content with Forms Authentication • Client Script Goodies, Client Callbacks • XML Databinding

  3. Cross Page Posting • 2.0 Supports Cross Page Postbacks • Scenario: Search or lookup button at top of page • Postback target via “PostBackUrl” property • <asp:button PostBackUrl=“b.aspx” runat=“server”/> • Can be declaratively or programmatically set • Postback “target page” has full access to server controls for “originating page” • Access controls via “Page.PreviousPage” property • <%@ PreviousPage VirtualPath=“a.aspx” %>

  4. Cross-Page Postback Example: <!-- Page1.aspx --> <html> <body> <form runat=“server”> <asp:button text=“Same Page” OnClick=“Btn1_Click” runat=“server”/> <asp:calendar id=“MyCalendar” runat=“server”/> <asp:button text=“Page 2” PostBackUrl=“page2.aspx” runat=“server”/> </form> </body> </html> ‘ Page2.aspx Dim MyCalendar as Calendar = PreviousPage.FindControl(“MyCalendar”) Label1.Text = “You selected: “ & MyCalendar.SelectedDate <%@ PreviousPage VirtualPath=“page1.aspx” %> Label1.Text = “You selected: “ & PreviousPage.Exposed Property

  5. Cross Page Postback

  6. Validation Groups • Enable validation controls to only apply in response to a specific button/action • Today validation controls apply “all or nothing” • Indicated via “ValidationGroup” property • Supported by all Validation and Postback controls • Controls in ValidationGroup validate with postback • Programmatic Support for Validating Groups • If (Page.Validate(“group_name”)) Then • Page.IsValid evaluates ValidationGroup Postback

  7. ValidationGroup Example: <html> <body> <form runat=“server”> <asp:textbox id=“TextBox1” runat=“server”/> <asp:requiredfieldvalidator ValidationGroup=“Group1” ErrorText=“Need to Fill in Value!” ControlToValidate=“TextBox1” runat=“server”/> <asp:textbox id=“TextBox2” runat=“server”/> <asp:requiredfieldvalidator ValidationGroup=“Group2” ErrorText=“Need to Fill in Value!” ControlToValidate=“TextBox2” runat=“server”/> <asp:button text=“Group1” ValidationGroup=“Group1” runat=“server”/> <asp:button text=“Group2” ValidationGroup=“Group2” runat=“server”/> </form> </body> </html>

  8. Validation Groups

  9. <ASP:Wizard> Control • Enables linear and non-linear navigation • Developer defines templated “steps” within control • Control state maintained throughout wizard steps • Ideal for multi-step gathering workflow • Example Usage: Customer Registration • <asp:CreateUserWizard> built w/ Wizard • Flexible Wizard Control Navigation Model • MoveTo(wizardStep), ActiveStepIndex, etc • Events can fire both on individual steps and completion • Controls in wizard templates flattened to page • Enables direct control access w/o template fishing

  10. <ASP:Wizard> Example: <asp:Wizard id=“Wizard1” runat=“server” OnFinishButtonClick=“FinishBtn_Click”> <WizardSteps> <asp:WizardStep id=“WizardStep1” title=“Step 1”> Enter Name: <asp:textbox id=“Name” runat=“server”/> </asp:WizardStep> <asp:WizardStep id=“WizardStep1” title=“Step 2”> Enter State: <asp:textbox id=“State” runat=“server”/> </asp:WizardStep> <asp:WizardStep id=“Done” title=“Complete”> Congrats! The registration is complete! </asp:WizardStep> </WizardSteps> </asp:Wizard> Sub FinishBtn_Click(Sender as Object, E as WizardNavigationEventArgs) Label1.Text = “Hi “ & Name.Text & “ you are from: “ & State.Text End Sub

  11. Wizard Control

  12. URL Rewriting/Mapping • Built-in UrlMapping Module for rewriting paths • Enables “vanity” URLs instead of querystrings • Enables easy moving of pages without 404s • Useful Performance Tip: • Enables kernel level http caching on IIS6 for dynamic pages that would need querystrings <!-- Web.Config --> <urlMappings enabled=“true”> <add url=“~/Home.aspx” mappedUrl=“~/Default.aspx?tab=0” /> <add url=“~/Products/Books.aspx” mappedUrl=“~/Catalog.aspx?catid=1” /> </urlMappings>

  13. URL Rewriting

  14. Compilation Build Providers • 2.0 introduces concept of “Build Providers” that participate in compile process • Enable declarative file formats as code resources • Example Providers Built-in Whidbey: • .WSDL (web service proxies) • .XSD (data components) • .RESX (resource files) • Model of declarative files + partial types enables rich functionality w/ rich extensibility

  15. Compilation Build Providers • To build your own custom build provider: • Subclass System.Web.Compilation.BuildProvider • Register file extension in web/machine.config <compilation debug="false" explicit="true" defaultLanguage="vb"> <buildProviders> <add extension=".aspx" appliesTo="Web" type="System.Web.Compilation.PageBuildProvider" /> <add extension=".ascx" appliesTo="Web" type="System.Web.Compilation.UserControlBuildProvider" /> <add extension=".master" appliesTo="Web" type="System.Web.Compilation.MasterPageBuildProvider" /> <add extension=".asmx" appliesTo="Web" type="System.Web.Compilation.WebServiceBuildProvider" /> <add extension=".ashx" appliesTo="Web" type="System.Web.Compilation.WebHandlerBuildProvider" /> <add extension=".soap" appliesTo="Web" type="System.Web.Compilation.WebServiceBuildProvider" /> <add extension=".resx" appliesTo="Code,Resources" type="System.Web.Compilation.ResXBuildProvider" /> <add extension=".wsdl" appliesTo="Code" type="System.Web.Compilation.WsdlBuildProvider" /> <add extension=".xsd" appliesTo="Code" type="System.Web.Compilation.XsdBuildProvider" /> </buildProviders> </compilation>

  16. Compilation Build Providers

  17. “No-Compile” Pages • New feature in 2.0 to enable .ASPX pages to be executed without compilation • V1 .aspx pages always dynamically compiled • Two primary scenarios: • Enable site administrators to lockdown ability of content owners to write code on portions of site • Enable better scaling for sites with thousands of pages – don’t require separate type for each • Note: This is independent of the new pre-compilation tool – which compiles the pages prior to deployment

  18. “No-Compile” Config <system.web> <pages compilationMode=“[mode]”/> </system.web> [mode] = “Always”, “Never”, “Auto” // Default: Always

  19. No-Compile Pages

  20. File System Provider • 2.0 enables web content to be served from non-file system locations • Example: Database or CMS system • Developers sub-class “VirtualPathProvider”: • GetCacheDependency() • GetFileHash() • GetFile() • GetDirectory() • Tip: Can leverage new SqlCacheDependency object to enable SQL Cache Invalidation

  21. Simple CMS DB-Driven File Provider

  22. Securing Non-ASP.NET Content • 2.0 enables devs to use ASP.NET forms authentication for non-ASP.NET files • Static Files: .html, .jgp, .gif, .xml, etc • Dynamic Files: .ASP, .PHP, .JSP • Requires IIS 6.0 (new ExecuteUrl feature) • ASP.NET executes resource first, then passes execution flow to originating ISAPI

  23. Web.Config Security Permissions <authentication mode=“Forms” loginUrl=“login.aspx”/> <location path=“MyClassicASP.asp”> <system.web> <authorization> <allow roles=“PremiumUsers”/> <allow roles=“Admin”/> <deny users=“*”/> </authorization> </system.web> </location>

  24. IIS 6.0 MMC Configuration

  25. Securing ASP Pages with ASP.NET

  26. Client Script Goodies <asp:button Text=“Push Me!” OnClick=“Button1_Click” OnClientClick=“ClientButton1_Click” runat=“server” /> • Client-side click event handlers on controls: • Focus mechanisms: • Page.SetFocus(control) • TextBox.Focus() • Default button and focus • <form DefaultFocus=“control1” runat=“server”> • <form DefaultButton=“button1” runat=“server”>

  27. Client Script Goodies • Validation Error Focus • “SetFocusOnError” property • Auto-scroll maintenance on postback • Ideal for large pages – no code required • Simplified Client Script Registration • Page.ClientScript helper methods • Client-side Event Callbacks • ICallBackEventHandler interface • Used by TreeView, GridView controls

  28. Client Script Goodies

  29. XML Databinding • <asp:XmlDataSource> control enables data source binding against XML files • Optional XPath expression to scope results • <asp:DataList> supports binding against <asp:XMLDataSource> • Use “XPath(expression)” statement in templates • “XPathSelect(expression)” selects a node list • Combine two to build simple RSS Reader

  30. RSS Reader Using XML Databinding

  31. Summary • Enormous number of new features in ASP.NET 2.0 • Tons of additional cool features out there… • Visit http://www.asp.net/whidbey to download slides and samples • Start exploring ASP.NET 2.0 today!

  32. ASP.NET Community • http://www.asp.net/whidbey has whitepapers, samples, slides and forums dedicated to ASP.NET 2.0 • http://beta.asp.net/quickstart has tons of samples and source code • Email: • bradmi@microsoft.com

More Related