ASP.NET 2.0 Tips & Tricks

bubblemessengerSecurity

Nov 5, 2013 (3 years and 9 months ago)

97 views

ASP.NET 2.0 Tips & Tricks

Tips & Tricks for ASP.NET 2.0

Rob Howard

Telligent

Microsoft Regional Director

For more information…

Rob Howard

Email:

rhoward@telligent.com

Website:

telligent.com

Blog:

weblogs.asp.net/rhoward


Telligent

Community Server (communityserver.org)

Custom Application Development (ASP.NET)

Agenda

1.
Cross Page Posting

2.
Validation Groups

3.
Wizard Control and Profile Registration

4.
Secure Connection String Storage

5.
Config
-
based Control Registration

6.
Caching Features

7.
Client Script Goodies, Client Callbacks

8.
XML Databinding to build a Blog Reader

9.
URL Rewriting

10.

Site Map

1. Cross Page Posting

Problem

Postback model isn’t always desired

Scenario: Search or lookup button at top of page


New “PostBackUrl” property

<asp:button PostBackUrl=“b.aspx” runat=“server”/>

Declaratively or programmatically set


Page posted to has strongly typed access

Access controls via “Page.PreviousPage” property

<%@ PreviousPage VirtualPath=“a.aspx” %>

Cross Page Posting

2. Validation Groups

Problem

Today validation controls apply “all or nothing”


ASP.NET 2.0 “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

Validation Groups

3. Wizard Control

Problem

Difficult to build Wizard style UI today


Enables linear and non
-
linear navigation

Developer defines templated “steps” within control

Control state maintained throughout wizard steps


Flexible Wizard Control Navigation Model

MoveTo(wizardStep), ActiveStepIndex, etc

Events can fire on steps and completion

Wizard Control

4. Connection String Storage

Persistence and declarative referencing

Stored in *.config

Avoid hard
-
coding within pages/code

Can be optionally encrypted

Built
-
in design time support

Promote use for best practices

Enable optional encrypting of values in config

Admin Support

MMC Admin Tool Support

Configuration API Support

Config Connection Strings

<connectionStrings>


<add name="pubs“ ProviderName=“…”


connectionString=“…” />

</connectionStrings>

<asp:SqlDataSource Id=“MySource”


ConnectionString=“<%$ connectionStrings:pubs %>”


SelectCommand=“select au_id from authors”


runat=“server”/>

Web.config:

Page.aspx
:

Dim connstr As String =


ConfigurationSettings.ConnectionStrings(“pubs”)

Code.vb
:

Connection Strings

5. Web.Config Registration of Controls

Server controls can now be registered within
web.config files instead of on every page


Full Visual Studio support w/ intellisense

<!
--

Web.Config
--
>


<controls>


<add tagPrefix="acme" tagName="uc" src="controls/uc.ascx" />


<add tagPrefix="my" namespace="MyControls.Basic" assembly="MyControls" />

</controls>

Control Registration

6. Caching

SQL 7 & 2000 Support

Table change dependencies on SQL 7 & 2000

Requires
<caching>

configuration settings

One
-
time setup of SQL Server database

Polling model


SQL Server “Yukon”

Result Set dependencies for SQL Yukon

Supported through ADO.NET
SqlCommand

No setup required

Notification model

SQL Server 7 & 2000

Table level notifications only

Notification when data in table changes

Row
-
level notification
is not

supported


Requires one time setup of SQL 7 / 2000

Triggers on tables that participate

Stored procedures called to check


Of Note:

Entries in cache table < # of tables in DB

Entries in cache = # items in cache table

Data Caching

How it works: SQL 7 & 2000

N

n1

data

a

ASP.NET

SQL Server

Northwind Database

tableName

changeId

ChangeNotification table

Products table

trigger

products

1

SqlCacheDependency

3

z

aspnet_regsql.exe

Page

DataSet

Cache

7. Client Script Goodies

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”>

<asp:button Text=“Push Me!”


OnClick=“Button1_Click”


OnClientClick=“ClientButton1_Click”


runat=“server” />



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


Client Script Goodies

8. 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

RSS Reader

9. URL Rewriting

Enable vanity URLs

Eliminate ugly query string URLs

Simplifies customer navigation experience


Possible in ASP.NET 1.1

Required custom code

Ex., Community Server


ASP.NET 2.0

Simply edit configuration file

Add/Remove/Clear mappings

URL Rewriting

10. Site Map

How to manage all the site urls

Many navigation points

What is the relationship between these points


Possible in ASP.NET 1.1

Write a complete site map

Ex., Community Server


ASP.NET 2.0

Site Map is a built in feature (Provider based)

Makes adding URLs/Pages unbelievably easy

Site Map

Summary

Enormous number of new features in
ASP.NET 2.0

Tons of additional cool features out there…


Visit
http://beta.asp.net/quickstarts

to try
out hundreds of samples online


Download these slides+demos from:
http://www.rob
-
howard.net

10. Dynamic Master Pages

Master pages encapsulate layout

Enforce a consistent look and feel across pages


Master pages can be dynamically chosen

Scenario: Per browser type or per customer type


Two mechanisms to set dynamically change master:


1) Set Page.MasterPageFile property in Page_PreInit event


2) Device Filter: <%@ page ie:masterpagefile=“ie.master” %>

Dynamic MasterPages

10 Localization

Whidbey supports nice localization model

Store strings and values in a separate .resx file


Culture and language settings

programmatically and declaratively set

Override InitalizeCulture on the Page class

<%@ Page UIculture=“value” %>


Cool Tip: You can set value to “auto” to have it
automatically based on the client browser’s
user agent string

Localization