2010 - Basta: ASP.NET Controls für Web Forms und MVC

37
Controls im Web ASP.NET Controls für Web Forms und MVC Daniel Fisher (lennybacon) | devcoach.com

Transcript of 2010 - Basta: ASP.NET Controls für Web Forms und MVC

Controls im WebASP.NET Controls für Web Forms und MVC

Daniel Fisher (lennybacon) | devcoach.com

Lennybacon.com■ Daniel Fisher | CTO & Software Architect ■

MCP, MCTS, MCPD…[email protected]

■ Mit-Gründer und Geschäftsführer von devcoach.comwww.devcoach.com

■ Mit-Gründer und Vorstand dergemeinnützigen www.just community.de e.V.

■ Veranstalter des größten Entwickler & IT-Pro Community Events in Deutschland: www.nrwconf.de

■ Mit-Gründer und Leiter derINETA Usergroup Düsseldorf

www.NetUG-NiederRhein.de

■ Mitglied im Microsoft Community Leader & Insider Program (CLIP)

■ Connected Systems Advisory BoardExpertengruppe für WCF, WF & BizTalk

Efficient Communication…

devcoach.com■ Leistungen

■ Architektur-BeratungStrukturierter und effizienter zu einer wartbaren Anwendung.

■ Software-EntwicklungTeam-out-of-the-box (Near-shoring)Objektmodelle und DatenzugriffKommunikations-InfrastrukturenIdentitäts- und BerechtigungsmodelleWeb 2.0 und Rich Internet Applikation

■ Coaching & TrainingTechnologien schneller verstehen und richtig einsetzen.

■ Technologien

■ Microsoft Windows & .NET FrameworkASP.NET, WCF, WF, WPF, Silverlight & Geneva

■ Kunden

■ Versicherung, Finanzindustrie, Mittelstand, Handel, Kommunikation, Softwarehersteller u.v.a.Bundesamt für Sicherheit in der Informationstechnologie, Microsoft, Dresdner Bank…

Project Experience

Technology Know-how

devcoach®

Agenda

• Web Controls

• What‘s MVC anyway?

• Recap 2006

• Controls for the Microsoft Web Platform

• Summary

7

Is all about Maintainablility!

■ The controls concept achieves reusability for

■ Html mark-up

■ Server-side code

■ Client-side scripts

■ Compiled code that can be reused in any ASP.NET web application.

Controls in Code

public class MyControl : Control{ //…}

Controls in Code: Usage

<asp:Contol id="MyId" runat="server" />

Web Form Pros

Web Form Cons

■ No visual way of developing server controls, generated via code.

Controller

Logic

ViewModel

What‘s MVC anyway?

Concepts

• Nice URLs & Routing

• Process over „just add a Form“

• Loads of naming conventions

• Code, Code, Code

• As strongly typed as possible!

• Once build, forever used…

Concepts cont.

• No Code-Behind for ASPXs

• No Postbacks

• No Viewstate

• And last but not least „No controls out of Redmond“!

demo!

Web Forms or MVC?

Once apon a time…

CENSORE

D

18

Processing

■ Child control creation

■ CreateChildControls()■ All display logic

■ OnPreRender()■ Rendering

■ Custom: Render()

■ Composite: Nothing to do?

PROBLEM #1: POSTBACKS

First Aid

■ Response.Redirect(http://example.org);

■ Server.Transfer(http://example.org);

■ <a href=“…“>link</a>

■ Server side only events

■ AJAX

demo!

PROBLEM #2: VIEWSTATE

23

Postback - Lifecycle of a control

Instantiate : Constructor

Initialize : OnInit method and Init Event

Begin Tracking View State : TrackViewState

Load View State : LoadViewState method

Load Postback Data : IPostBackDataHandler.LoadPostdata method

Load: OnLoad method and Load event

Raise Changed Events : IPostBackDataHandler.RaisePostDataChangedEvent method

Raise Postback Event : IPostBackEventHandler.RaisePostBackEvent method

PreRender : OnPreRender method and PreRender event

Save View State : SaveViewState method

Render : Render method

Unload : OnUnload method and Unload event

Dispose : Dispose Method

24

ViewState

■ ViewState is used to track and restore the state values of controls that would otherwise be lost

■ Base64 encoded - not easily readable, but not encrypted!

■ What to store

■ Integers

■ Strings

■ Floats

■ Decimals

■ Arrays of the data types stated above

Viewstate in code

[Bindable(false)][Category("Appearance")][DefaultValue("This is the default text.")][Description("The text of the custom control.")]public virtual string Text{ get { var o = ViewState["Text"]; return o!=null ? (string) o : "This is the default text."; } set { ViewState["Text"] = value; }}

Viewstate in markup

First Aid

■ Query Request.Form and Request.QueryString

■ Use local fields■ most times they work fine!

demo!

PROBLEM #3: BINDING

Binding in code

public overrride OnInit( object sender, EventArgs e){ List<Person> persons = Repository<Person>.GetAll(); myControl.DataSource = persons; myControl.DataBind()}

First Aid

■ HttpContext.Current.Items

■ Cast to ViewPage■ Access Model

■ Cast to IEnumerable

demo!

PROBLEM #4: RESOURCES

Resources

■ Images

■ Java Script

■ Styles

Resources in code

[assembly: WebResource("devcoach.Web.UI.Controls.Busy.gif", "image/gif")]

imageUrl = Page.ClientScript.GetWebResourceUrl(

GetType(),

"devcoach.Web.UI.Controls.Busy.gif");

First Aid

■ Embedded

■ VirtualPathProvider

■ Routing