discovering asp.net 4.5 web forms
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.TRANSCRIPT
![Page 1: Discovering ASP.NET 4.5 Web Forms](https://reader033.vdocuments.us/reader033/viewer/2022061214/5499a814b47959a66e8b4667/html5/thumbnails/1.jpg)
Discovering ASP.NET 4.5 Web Forms Jurgen
PostelmansU2U
![Page 2: Discovering ASP.NET 4.5 Web Forms](https://reader033.vdocuments.us/reader033/viewer/2022061214/5499a814b47959a66e8b4667/html5/thumbnails/2.jpg)
Session Contents• Strongly Typed Data Data Controls• Model Binding – Retrieving Data• Model Binding – Value Providers• Model Binding – Updating Data• Model Binding - Validation
![Page 3: Discovering ASP.NET 4.5 Web Forms](https://reader033.vdocuments.us/reader033/viewer/2022061214/5499a814b47959a66e8b4667/html5/thumbnails/3.jpg)
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)
![Page 4: Discovering ASP.NET 4.5 Web Forms](https://reader033.vdocuments.us/reader033/viewer/2022061214/5499a814b47959a66e8b4667/html5/thumbnails/4.jpg)
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
![Page 5: Discovering ASP.NET 4.5 Web Forms](https://reader033.vdocuments.us/reader033/viewer/2022061214/5499a814b47959a66e8b4667/html5/thumbnails/5.jpg)
Model Binding – Overview• Extends data binding to work with code-focused
data access• Uses concepts from ObjectDataSource and model
binding in ASP.NET MVC
![Page 6: Discovering ASP.NET 4.5 Web Forms](https://reader033.vdocuments.us/reader033/viewer/2022061214/5499a814b47959a66e8b4667/html5/thumbnails/6.jpg)
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
![Page 7: Discovering ASP.NET 4.5 Web Forms](https://reader033.vdocuments.us/reader033/viewer/2022061214/5499a814b47959a66e8b4667/html5/thumbnails/7.jpg)
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
![Page 8: Discovering ASP.NET 4.5 Web Forms](https://reader033.vdocuments.us/reader033/viewer/2022061214/5499a814b47959a66e8b4667/html5/thumbnails/8.jpg)
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
![Page 9: Discovering ASP.NET 4.5 Web Forms](https://reader033.vdocuments.us/reader033/viewer/2022061214/5499a814b47959a66e8b4667/html5/thumbnails/9.jpg)
Custom Value Providers• Implement IValueProvider interface in class• GetValue method should return the value to be assigned
to the parameter
![Page 10: Discovering ASP.NET 4.5 Web Forms](https://reader033.vdocuments.us/reader033/viewer/2022061214/5499a814b47959a66e8b4667/html5/thumbnails/10.jpg)
Hooking up Custom Value Provider• Implement custom attribute inheriting from
ValueProviderSourceAttribute• Override GetValueProvider method and return an instance
of your custom Value Provider
![Page 11: Discovering ASP.NET 4.5 Web Forms](https://reader033.vdocuments.us/reader033/viewer/2022061214/5499a814b47959a66e8b4667/html5/thumbnails/11.jpg)
Model Binding – Updating Data• Data Controls provide the following properties• UpdateMethod• InsertMethod• DeleteMethod
![Page 12: Discovering ASP.NET 4.5 Web Forms](https://reader033.vdocuments.us/reader033/viewer/2022061214/5499a814b47959a66e8b4667/html5/thumbnails/12.jpg)
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 ?
![Page 13: Discovering ASP.NET 4.5 Web Forms](https://reader033.vdocuments.us/reader033/viewer/2022061214/5499a814b47959a66e8b4667/html5/thumbnails/13.jpg)
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
![Page 14: Discovering ASP.NET 4.5 Web Forms](https://reader033.vdocuments.us/reader033/viewer/2022061214/5499a814b47959a66e8b4667/html5/thumbnails/14.jpg)
Model Binding - Validation• Model Binding supports Data Annotations from
System.ComponentModel.DataAnnotations namespace
![Page 15: Discovering ASP.NET 4.5 Web Forms](https://reader033.vdocuments.us/reader033/viewer/2022061214/5499a814b47959a66e8b4667/html5/thumbnails/15.jpg)
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
![Page 16: Discovering ASP.NET 4.5 Web Forms](https://reader033.vdocuments.us/reader033/viewer/2022061214/5499a814b47959a66e8b4667/html5/thumbnails/16.jpg)
Displaying Model Validation Errors• ValidationSummary control has new
ShowModelStateErrors property• ValidationSummuray will access the ModelState and
display any validation errors in contains
![Page 17: Discovering ASP.NET 4.5 Web Forms](https://reader033.vdocuments.us/reader033/viewer/2022061214/5499a814b47959a66e8b4667/html5/thumbnails/17.jpg)
Extending the Validation• Implement a custom attribute inheriting from
ValidationAttribute base class• Override IsValid method and implement you custom
validation logic.
![Page 18: Discovering ASP.NET 4.5 Web Forms](https://reader033.vdocuments.us/reader033/viewer/2022061214/5499a814b47959a66e8b4667/html5/thumbnails/18.jpg)
Q&A
![Page 19: Discovering ASP.NET 4.5 Web Forms](https://reader033.vdocuments.us/reader033/viewer/2022061214/5499a814b47959a66e8b4667/html5/thumbnails/19.jpg)
© 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.