cool tips and tricks with asp.net 2.0 stefan schackow program manager asp.net team microsoft...

33
Cool Tips and Tricks with Cool Tips and Tricks with ASP.NET 2.0 ASP.NET 2.0 Stefan Schackow Program Manager ASP.NET Team Microsoft Corporation

Upload: junior-harris

Post on 28-Dec-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

Cool Tips and Tricks with Cool Tips and Tricks with ASP.NET 2.0ASP.NET 2.0

Stefan SchackowProgram Manager

ASP.NET TeamMicrosoft Corporation

AgendaAgenda

Cross Page Post-backsValidation GroupsWizard ControlURL Rewriting/MappingCompilation Build Providers and .WSDL Files“No-Compile” PagesBuilding CMS System w/ File System ProviderSecuring non-ASP.NET web content with Forms AuthenticationClient Script Goodies, Client CallbacksXML Databinding

Cross Page PostingCross Page Posting2.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” %>

Cross-Page Postback Example: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

Cross Page PostbackCross Page Postback

Validation GroupsValidation GroupsEnable validation controls to only apply in response to a specific button/action

Today validation controls apply “all or nothing”

Indicated via “ValidationGroup” propertySupported by all Validation and Postback controlsControls in ValidationGroup validate with postback

Programmatic Support for Validating GroupsIf (Page.Validate(“group_name”)) ThenPage.IsValid evaluates ValidationGroup Postback

ValidationGroup Example: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>

Validation GroupsValidation Groups

<ASP:Wizard> Control<ASP:Wizard> ControlEnables linear and non-linear navigation

Developer defines templated “steps” within controlControl state maintained throughout wizard steps

Ideal for multi-step gathering workflowExample Usage: Customer Registration <asp:CreateUserWizard> built w/ Wizard

Flexible Wizard Control Navigation ModelMoveTo(wizardStep), ActiveStepIndex, etcEvents can fire both on individual steps and completion

Controls in wizard templates flattened to pageEnables direct control access w/o template fishing

<ASP:Wizard> Example:<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

Wizard ControlWizard Control

URL Rewriting/MappingURL Rewriting/MappingBuilt-in UrlMapping Module for rewriting paths

Enables “vanity” URLs instead of querystringsEnables 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>

URL RewritingURL Rewriting

Compilation Build ProvidersCompilation 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

Compilation Build ProvidersCompilation Build ProvidersTo build your own custom build provider:

Subclass System.Web.Compilation.BuildProviderRegister 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>

Compilation Build Compilation Build ProvidersProviders

““No-Compile” PagesNo-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 siteEnable 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

““No-Compile” ConfigNo-Compile” Config

<system.web> <pages compilationMode=“[mode]”/></system.web>

[mode] = “Always”, “Never”, “Auto”// Default: Always

No-Compile PagesNo-Compile Pages

File System ProviderFile 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

Simple CMS Simple CMS DB-Driven File ProviderDB-Driven File Provider

Securing Non-ASP.NET ContentSecuring 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, etcDynamic Files: .ASP, .PHP, .JSP

Requires IIS 6.0 (new ExecuteUrl feature)ASP.NET executes resource first, then passes execution flow to originating ISAPI

Web.Config Security PermissionsWeb.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>

IIS 6.0 MMC ConfigurationIIS 6.0 MMC Configuration

Securing ASP Pages Securing ASP Pages with ASP.NETwith ASP.NET

Client Script GoodiesClient Script GoodiesClient-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”>

<asp:button Text=“Push Me!” OnClick=“Button1_Click” OnClientClick=“ClientButton1_Click” runat=“server” />

Client Script GoodiesClient Script GoodiesValidation Error Focus

“SetFocusOnError” property

Auto-scroll maintenance on postbackIdeal for large pages – no code required

Simplified Client Script RegistrationPage.ClientScript helper methods

Client-side Event CallbacksICallBackEventHandler interfaceUsed by TreeView, GridView controls

Client Script GoodiesClient Script Goodies

XML DatabindingXML 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

RSS Reader Using XML RSS Reader Using XML DatabindingDatabinding

SummarySummary

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!

ASP.NET CommunityASP.NET Communityhttp://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: [email protected]