microsoft visual c# 2005 (part 3 of 4): asp.net 2.0 data binding
TRANSCRIPT
![Page 1: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/1.jpg)
Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding
Fritz OnionCofounderPluralsight LLChttp://pluralsight.com/fritz/
![Page 2: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/2.jpg)
Introduction to ASP.NET 2.0
A four-part webcast series for developers new to Microsoft® ASP.NET (two deliveries: Microsoft® Visual C# and Microsoft® Visual Basic .NET)
Lectures with accompanying labs
Date Topic
Mon, Feb. 19, 10 A.M. ASP.NET 2.0 Fundamentals (C#)
Tue, Feb. 20, 10 A.M. User Interface Elements (C#)
Wed, Feb. 21, 10 A.M. DataBinding and AJAX (C#)
Thu, Feb. 22, 10 A.M. Configuration and Deployment (C#)
Mon, Feb. 19, 1 P.M. ASP.NET 2.0 Fundamentals (VB)
Tue, Feb. 20, 1 P.M. User Interface Elements (VB)
Wed, Feb. 21, 1 P.M. DataBinding and AJAX (VB)
Thu, Feb. 22, 1 P.M. Configuration and Deployment (VB)
![Page 3: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/3.jpg)
Agenda
Data Binding
Declarative Binding and SqlDataSource
GridView
Parameters
Updating, Inserting, and Deleting
DetailsView
Object Data Source
Microsoft® ASP.NET AJAX: Using the UpdatePanel
![Page 4: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/4.jpg)
Data Binding
Data binding
A mechanism for displaying and modifying dynamic data from a Web page
ASP.NET supports data binding
Can point controls to database table as source for data
Displayed content drawn from table
Interface for modifying data built into some controls (update, delete, insert)
![Page 5: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/5.jpg)
Data Binding
Client Browser Web Server
Default.aspx
Quotes
Database
Data-
binding
![Page 6: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/6.jpg)
Binding Controls to Data
Several ASP.NET controls designed to bind to data
GridView
Display/edit a database table as a grid
DetailsView
Display one table row at a time, insert new items
BulletedList
Display a list of items from a table
Many more …
![Page 7: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/7.jpg)
SqlDataSource Control
SqlDataSource control handles data retrieval, inserts, updates, and deletes
Acts as bridge between database and data-bound control
Contains SQL statements to perform database calls
<asp:SqlDataSource ID="QuotesDataSource" runat="server"SelectCommand="SELECT ID, Author, Quote FROM Quotes"ConnectionString="…"/>
![Page 8: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/8.jpg)
Adding a GridView
1. Set page to Design mode
2. Open Database Explorer
3. Drag table onto page
4. Select desired options
5. Optionally Auto Format…
6. Edit Columns and set
ID column Visible=false
![Page 9: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/9.jpg)
Generated Source<asp:GridView ID="GridView1" runat="server"
AllowSorting="True"AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EmptyDataText=“No records.">
<Columns><asp:BoundField DataField="ID" HeaderText="ID"
ReadOnly="True" SortExpression="ID"Visible="False" />
<asp:BoundField DataField="Author" HeaderText="Author"SortExpression="Author" />
<asp:BoundField DataField="Quote" HeaderText="Quote"SortExpression="Quote" />
</Columns></asp:GridView><asp:SqlDataSource ID="SqlDataSource1" runat="server"
SelectCommand="SELECT ID, Author, Quote FROM Quotes"ConnectionString="<%$ ConnectionStrings:QuotesConnectionString1 %>"/>
<configuration><connectionStrings>
<add name="QuotesConnectionString1" connectionString="Data Source=… "providerName="System.Data.SqlClient" />
</connectionStrings></configuration>
web.config
Default.aspx
GridView declaration
BoundFields for columns
SqlDataSource declaration
SQL query to retrieve data
Connection string pointing
to local Quotes.mdf file
![Page 10: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/10.jpg)
SQL Parameters
SQL supports parameters to fill values in dynamically
Microsoft® SQL Server™ syntax is @varname
Before executing statement, parameters must be associated with values
UPDATE Quotes SET Author=@Author WHERE ID=@ID
parameters
![Page 11: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/11.jpg)
SqlDataSource Parameters
How to associate parameters with SqlDataSource
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:QuotesConnectionString1 %>"SelectCommand="SELECT [ID], [Author], [Quote] FROM [Quotes]"UpdateCommand="UPDATE Quotes SET Author=@Author, Quote=@Quote WHERE ID=@ID"DeleteCommand="DELETE FROM Quotes WHERE ID=@ID"><UpdateParameters>
<asp:Parameter Name="Author" Type="String" /><asp:Parameter Name="Quote" Type="String" /><asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters><DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" /></DeleteParameters>
</asp:SqlDataSource>
Add Update
and Delete
commands with
parameters
List parameter
names and types
Note: Parameters should always be named the same as the corresponding
column names to work properly with the GridView and DetailsView controls
![Page 12: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/12.jpg)
GridView Updates and Deletes
Once your SqlDataSource has Update and Delete commands, the GridView can be enabled with Update and Delete features
Must set DataKeyNames
to identity column name
to support Update/Delete
![Page 13: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/13.jpg)
GridView Updating
![Page 14: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/14.jpg)
Inserting with a DetailsView
DetailsView provides insert feature
Can also be used to display/update/delete one row at a time
Configure new data source
![Page 15: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/15.jpg)
Inserting with a DetailsView
Add InsertCommand to data source
Enable Inserting and set DefaultMode=Insert
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ConnectionStrings:QuotesConnectionString1 %>"SelectCommand="SELECT [ID], [Author], [Quote] FROM [Quotes]"InsertCommand="INSERT INTO Quotes (Author, Quote) VALUES (@Author, @Quote)"><InsertParameters>
<asp:Parameter Name="Author" Type="string" /><asp:Parameter Name="Quote" Type="string" />
</InsertParameters></asp:SqlDataSource>
![Page 16: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/16.jpg)
Inserting with a DetailsView
![Page 17: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/17.jpg)
IDataSource
Binding to Objects
ObjectDataSource supports binding to middle-tier objects
Data-bound control
Repository
ObjectDataSource Middle-tier
data objects
<asp:ObjectDataSource ID="peopleDataSource"
runat="server"
SelectMethod="GetPeople"
TypeName="PeopleDS" />
public static class PeopleDS {static public List<Person> GetPeople() {List<Person> ret = new List<Person>();// Add Person instances from some source herereturn ret;
}}
![Page 18: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/18.jpg)
Wiring Up a Data Access Layer
<asp:ObjectDataSource ID="ds1" runat="server"DataObjectTypeName="PS.MyObject"DeleteMethod="DeleteMyObject"InsertMethod="InsertMyObject"MaximumRowsParameterName="maxRows"SelectMethod="GetMyObjects"SortParameterName="sortExpression"TypeName="PS.DataAccessLayer"UpdateMethod="UpdateMyObject"StartRowIndexParameterName="startRowIndex"><DeleteParameters><asp:Parameter Name="objectId" Type="Int32" />
</DeleteParameters></asp:ObjectDataSource>
![Page 19: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/19.jpg)
ASP.NET AJAX
ASP.NET AJAX features added after release
Official release in February 2007
Three core components to release
ASP.NET AJAX Extensions (current supported features)
ASP.NET AJAX Community Technology Preview (CTP) (preview future features)
ASP.NET AJAX Control Toolkit (community-driven "collaborative source" controls)
![Page 20: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/20.jpg)
UpdatePanel
Enables partial page rendering
Asynchronous JavaScript And XML (AJAX)-style pages without custom client script
All postback requests within a panel altered to be AJAX calls
Fallback to normal postback if client doesn’t support it
<asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate>
--controls go here--</ContentTemplate>
</asp:UpdatePanel>
![Page 21: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/21.jpg)
UpdateProgress Control
UpdateProgress control displays content during an UpdatePanel’s asynchronous postback
Common to embed animated GIF, or simple message
By default, shows up for all update panels
Can set AssociatedUpdatePanelID to associate with just one panel
<asp:UpdateProgress runat="server" ID="UpdateProgress1“AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>Processing...
</ProgressTemplate></asp:UpdateProgress>
![Page 22: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/22.jpg)
Summary
Declarative data sources
Reduce code, move data access code into framework
Data binding
Implicit with declarative data sources
Parameters
Each data source is completely customizable with parameters
Connection strings
New storage location in web.config
ASP.NET AJAX
Easily incorporate asynchronous JavaScript calls
![Page 23: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/23.jpg)
Lab Work
Lab 03 - Data Binding
Use declarative data sources
Use the GridView and DetailsView
Use parameters in a declarative data source
Hierarchical data binding
![Page 24: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/24.jpg)
Questions and Answers
Submit text questions using the “Ask” button.
Don’t forget to fill out the survey.
For upcoming and previously live webcasts: www.microsoft.com/webcasts
Got webcast content ideas? Contact us at: http://go.microsoft.com/fwlink/?LinkId=41781
Today's webcast was presented using Microsoft®
Office Live Meeting. Get a free 14-day trial by
visiting: www.microsoft.com/presentlive
![Page 25: Microsoft Visual C# 2005 (Part 3 of 4): ASP.NET 2.0 Data Binding](https://reader036.vdocuments.mx/reader036/viewer/2022062418/556310c2d8b42a81528b4913/html5/thumbnails/25.jpg)