Upload
microsoft-developer-network-msdn-belgium-and-luxembourg
View
1.481
Download
4
Tags:
Embed Size (px)
DESCRIPTION
In this session you’ll learn about the new features coming to ASP.NET 4.5 Web Forms. We’ll dive into the powers of new data-related improvements in ASP.NET 4.5 Web Forms like model binders, strongly typed data controls and unobtrusive JavaScript validation. We will look at the new support for HTML5 in ASP.NET Web Forms and learn how to read and write HTTP requests and responses.
Citation preview
Discovering ASP.NET 4.5 Web Forms Jurgen
PostelmansU2U
Session Contents• Strongly Typed Data Data Controls• Model Binding – Retrieving Data• Model Binding – Value Providers• Model Binding – Updating Data• Model Binding - Validation
Strongly Typed Data Controls• Pre ASP.NET 4.5 Data Controls• Late-bound Eval/Bind functions • no compile time checking • Missing IntelliSense• No Navigation support (like Go To Definition)
Strongly Typed Data Controls• ASP.NET 4.5 introduces ModelType property on Data
Controls• Declare what type of data a control is bound to.• Strongly-typed Item and BoundItem property• Adds IntelliSense, compile-time checking and Navigation
support
Model Binding – Overview• Extends data binding to work with code-focused
data access• Uses concepts from ObjectDataSource and model
binding in ASP.NET MVC
Model Binding – Selecting Data• Set the control’s SelectMethod property to the name
of a method in your code• Should return a IEnumerable or IQueryable object
Model Binding – Selecting Data• Control calls the method at the appropriate time in
the page life cycle • Automatically binds the returned data. • No need to explicitly call the DataBind method
• Automatically supports paging and sorting• Handled by SQL Server
Model Binding – Filtering Data• Define the filtering parameters on the Select Method
in your code• Parameters get their value injected from Value
Providers• QueryString, Cookies, Form Values, Session ...• Custom Value Providers
Custom Value Providers• Implement IValueProvider interface in class• GetValue method should return the value to be assigned
to the parameter
Hooking up Custom Value Provider• Implement custom attribute inheriting from
ValueProviderSourceAttribute• Override GetValueProvider method and return an instance
of your custom Value Provider
Model Binding – Updating Data• Data Controls provide the following properties• UpdateMethod• InsertMethod• DeleteMethod
Model Binding – Updating Data• Approach 1• Define a first parameter of the type the control is bound to. • Model binding implicitly creates an instance of this type • Model Binding attempts to bind the values from the data control based on
expressions you write in the template• Only works for simple cases • What if you only display a subset of the fields ? Relationships ?
Model Binding – Updating Data
• Approach 2• First load the load the object to be updated from database• Define the update method with a first parameter representing the Primary Key
field • Ask Model Binding system to bind data-control values onto the
members • TryUpdateModel method
Model Binding - Validation• Model Binding supports Data Annotations from
System.ComponentModel.DataAnnotations namespace
Model Binding - Validation• When control values are model bound on the Type
these validation rules are checked• Page object now has a ModelState property• Can be used to iterate over validation errors• ModelState.IsValid returns true if there where any Model Binding
errors
Displaying Model Validation Errors• ValidationSummary control has new
ShowModelStateErrors property• ValidationSummuray will access the ModelState and
display any validation errors in contains
Extending the Validation• Implement a custom attribute inheriting from
ValidationAttribute base class• Override IsValid method and implement you custom
validation logic.
Q&A
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.