Upload
peter-gfader
View
2.582
Download
0
Embed Size (px)
DESCRIPTION
Goal: Bringing data to the web and back :-) Old Style New Style DataBinding DataGrid Input Validation
Citation preview
Developing Windows and Web Applications using Visual Studio.NET
Eric Phan
Solution Architect
Homework?
Address Control
Credit Card Validation
http://sharepoint.ssw.com.au/Training/UTSNET/
Part 2: .NET Webforms
1. Overview of .NET Webforms2. Data in Webforms TODAY3. Usability
• Rich Web Forms and • Other ASP.NET Features
4. Web Security 5. Advanced Topics & Future Technology (Silverlight)
Webforms
Recap
ClientGet URL Request ServerResponse
1
2
Client
Post requestwith data
ServerResponse
Session 7: Data in the Web• Goal: Bringing data to the weband back :-)
•Old Style
•New Style
•DataBinding
•DataGrid
•Input Validation
Click icon to add picture
Data in our database
Data
Bring database to the web
Data
Show/view
Enter
OLD way
Set of classes that expose data access services to the .NET programmer
ADO.NET provides a rich set of components for creating distributed, data-sharing applications
It is an integral part of the .NET Framework, providing access to relational data, XML, and application data
What is ADO.NET?
WebForm Apps WinForm Apps
DataSets
SQL Data Providers
SQLConnection
SQLCommand
SQLDataAdapter
SQLDataReader
SQL Data XML DataOracle Data
…
ADO.NET
Objects
Instead of DataSets we use classes
Extensible Can embed logic and validation A more OO approach
WebForm Apps WinForm Apps
Objects/Entities
Data Sources
SQL Data XML DataOracle Data
…
Why use DataSets
A highly functional container for data that can be strongly typed
Automatic change tracking for contained data Seamless data-binding support
Why Use Objects
Business Objects (NHibernate, NTiers, LINQ to SQL, LINQ to Entities)
Light weight Good data-binding support Extensible and OO Very easy to use No SQL command Strings
DataSets or Business Objects
The new way!
LINQ 2 EntitiesLINQ 2 SQL
Brand new in the .NET 3.5 Framework
Easily access and query your database through classes
Logical access to class hierarchies Supports Inheritance Intuitive to use
LINQ to SQL/Entities
Database Mapping Language
Contains a mapping of classes to database tables
Has built in logic to handle selection, updates and deletes
Can be customized
www.hookedonlinq.com
DBML File
var customers = from c in db.Customers
where c.FirstName.StartsWith(“A”)select c
SQL Syntax
var customers = db.Customers.Where(c => c.FirstName.StartsWith(“A”);
Lambda Syntax
Challenge, use this instead of SQL Server Management Studio for a month
LINQPad
See our Rules
http://www.ssw.com.au/ssw/Standards/Rules/RulesToBetterLINQ.aspx
Databinding
Bind the data
DataData source
From MSDN: Data binding is the process that establishes connection between the application UI and business logic
Binding controls to data from database
Bind a grid on Webform to a database table
Simple, convenient, and powerful way to create a read/write link between the controls on a form and the data in their application
Data Binding
DataData source
C#NorthwindDataContext db = new NorthwindDataContext();
GridView1.DataSource = db.Customers;
GridView1.DataBind();
English Instantiate our data access class Make the datasource of GridView1 all the customers
from our database Bind the Grid
Databinding the GridView (code)
Databinding the GridView (designer)
Databinding the GridView (designer)
Databinding the GridView (designer)
Databinding the GridView (designer)
Databinding the GridView (designer)
Databinding the GridView (running)
GridView
• Select sort and edit
• Display a Table from a Database
• Easy to configure
• Without writing any code at all
Lots of built in features like:
SortingPagingInsertingUpdating DeletingSelecting
GridView
Enable Delete, Insert, Update on the DataSource
Enabling these features
Enabling these features
Enable them on the GridView
Easy!
Bindable to Lists DataList DetailsView FormView Repeater/ListView DropDownList ListBox CheckBoxList RadioButtonList
Bindable to Properties/Columns Most other controls
Other Data Bindable Web Controls
Most controls in .NET Framework support data binding
Controls inherit from the WebControl base classwhich implements IDataBindingsAccessor interface
Important Data-binding properties
DataSource DataMember DisplayMember ValueMember
Design your own controls or purchase 3rd party controls
Data-Bound Controls
Passing Parameters
Pass parameters to pages or controls
Pass parameters to our data sources
Parameters can be obtained from: Query String Session Variable Control Property Hidden Field Cookie
Passing Parameters between pages
Add a Details View and create a new Datasource
1. Create a CustomerDetails.aspx
2. Create a LINQ Data Source
3. Specify the Where clause
4. Make it read from the Query String
5. Change the Select on the GridView to Link to the CustomerDetails Page
6. Edit the templates
7. Delete Select and Add a Hyperlink
CustomerDetails.aspx?CustomerID={0}
8. Configure the Hyperlink
Run it
Validation – Don’t trust anyone
Validation - Bring database to the web
Data
Show/view
Enter
Why do we need it?
PEBCAC Bad Data
Validation
Input Validation
Seamless verification of data fields Check for mandatory fields Check for correct data formats Check for data ranges
Reduces incorrect data entered into your database
Prevent possible runtime exceptions
Why use Validations?
Validation Types
ClientData
Show/view
Enter
Client Server
Client-Side Validation
Validations are performed before the form is posted back Quick and responsive for the end user Insecure
Server-Side Validation
Validation checks are performed on the server Secure because these checks cannot be easily by passed Sometimes can be slow
Recommendation
Use both the types This approach provides the best of both worlds
Validation Types
ClientData
Show/view
Enter
Client Server
RequiredFieldValidator
CompareValidator
RangeValidator
RegularExpressionValidator
CustomValidator
ValidationSummary
ASP.NET Validation Server Controls
To put the Northwind database up on the web?
CRUD for all tables Pages for all tables Validation
So how long do you think it would take...
1 line of code
5 Minutes
Awesome starting point
Learn more from http://www.asp.net/dynamicdata/
ASP.NET Dynamic Data
Extend what’s given to you and you can make really great websites
You can use it as a framework
Any issues ... questions
Thank You!
Gateway Court Suite 10 81 - 91 Military Road Neutral Bay, Sydney NSW 2089 AUSTRALIA
ABN: 21 069 371 900
Phone: + 61 2 9953 3000 Fax: + 61 2 9953 3105
www.hookedonlinq.com
http://www.asp.net/get-started/
http://weblogs.asp.net/scottgu/
http://www.asp.net/dynamicdata/
If you have the next awesome web idea http://www.microsoft.com/BizSpark/
http://www.ssw.com.au/ssw/Standards/Rules/RulesToBetterLINQ.aspx
Resources