1 data bound controls ii chapter 15 - 16. 2 objectives you will be able to use a data source control...

65
1 Data Bound Controls II Chapter 15 - 16

Upload: penelope-ray

Post on 12-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

1

Data Bound Controls II

Chapter 15 - 16

Page 2: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

2

Objectives

You will be able to Use a Data Source control to get

data from a SQL database and make it available to display components

Use a GridView control to display data from a Data Source control. All without writing any C# code.

Page 3: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

Data Binding

Permits us to bind a control to a data source. Example: SQL Server database table. Many others.

Control automatically shows data from the data source. No C# code required.

Can update the data source with new values input by the user.

Page 4: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

4

The Northwind Traders Database

We will use the Customers table from the Northwind Traders database for an example today. Need own copy so that we can modify it.

Download script from the class web site: http://www.cse.usf.edu/~turnerr/Web_Application_Design/Downloads

/ File create_customers.sql

Put the file into a convenient folder.

Page 5: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

5

Script create_customers.sql

Page 6: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

6

Executing the Script

Set up VPN connection.

Page 7: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

7

Check the Table in Visual Studio

Page 8: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

8

Show Table Data

Page 9: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

9

Table Customers

Page 10: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

10

Set Primary Key

In order to update the table, it must have a Primary Key. Set in the standard Northwinds

Database version of the table. Not in our copy.

View Server Explorer Expand Customers table

Open Table Definition

Page 11: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

11

Set Primary Key

Right-click hereSelect Set Primary Key

Page 12: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

12

Set Primary Key

Page 13: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

13

Data Source Controls

Family of ASP.NET controls with similar functionality and identical interfaces

Connection information Query information Parameters

Avoid need to write low level code to access the database.

Bind to display controls to show the data on a web page.

Page 14: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

14

The GridView Control

Displays data in a grid Think Excel spreadsheet http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.a

spx http://msdn.microsoft.com/en-us/magazine/cc163933.aspx

Binds to a Data Source control

Many properties! Control appearance and behavior

Page 15: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

15

Create a New Web Site

Page 16: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

16

Rename the .aspx file

Page 17: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

17

Drag a SqlDataSource to the Page

Page 18: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

18

Configure the DataSource

Click here

Click here

Page 19: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

19

Set Up a new Connection

Click here

Page 20: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

20

Add Connection

Click here

Page 21: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

21

Configure the DataSource

Click here

Page 22: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

22

Save the Connection String

Say “yes”

Page 23: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

23

Specify the Query

Page 24: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

24

Test the Query

Click here

Page 25: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

25

Query Result

Click here

Page 26: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

26

The SqlDataSource Ready for Action

The SqlDataSource control holds the connection and query information that will be used by a data bound control.

Page 27: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

What We Wrote

Page 28: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

$ Expressions

<%$ ConnectionStrings:wpusr40 %>

Will be evaluated by the server as the .aspx file is processed.

Invokes an expression builder ConnectionStrings in this case.

Does not emit HTML. Used only to set a property value of the

enclosing ASPX object SqlDataSource in this case.

Page 29: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

web.config

Page 30: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

$ Expressions

<%$ ConnectionStrings:wpusr40 %>

The expression builder ConnectionStrings will return the connection string with name wpusr40 from the <connectionStrings> section of web.config.

That string will be the value of the ConnectionString property of SqlDataSource1.

Equivalent to writing

SqlDataSource1.ConnectionString = WebConfigurationManager.ConnectionStrings["wpusr40"].ConnectionString;

in Page_Load.

Page 31: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

31

Drag a GridView onto the Page

Page 32: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

32

Bind to the SqlDataSource

Click here to select data source

Page 33: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

33

Bind to the SqlDataSource

Select SqlDataSource1

Page 34: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

34

The GridView

The GridView is now specific for the data source.

Page 35: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

Try it!

Page 36: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

Zoom Out and Resize the Window

36

Page 37: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

37

Enable Paging

Page 38: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

38

View the Source

Page 39: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

Paging Enabled

39

Page 40: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

40

Make it look nicer.

Click on the GridView’s smart tag and select Auto Format. Explore the choices

Page 41: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

41

Enable Sorting

Page 42: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

42

Run the App!

Note the page selections.

Click on the column headings to sort by that column.

Page 43: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

43

Compare Paging to Scrolling

Back in the design view Click on the GridView’s smart tag and

unselect Enable Paging.

Build and run the app again. Try the scroll bar.

Page 44: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

44

Paging Disabled

Page 45: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

45

Adding Insert, Update, and Delete

Currently we can only display the table contents. No capability to modify the database.

Look at the source. Find <asp:SqlDataSource ... >

Page 46: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

46

The SqlDataSource

Page 47: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

47

Add Update Capability

To add the capability of updating the database, go back to design view. Click the smart tag on SqlDataSource1. Click Configure Data Source

Page 48: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

48

Add Update Capability

Page 49: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

49

Add Update Capability

Click here.

Page 50: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

50

Add Update Capability

Click here.

Page 51: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

51

Add Update Capability

Click here.

Page 52: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

52

Add Update Capability

Click here.

Page 53: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

53

Add Update Capability

Click here.

Click here to test.

Page 54: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

54

Back in Design View

Click on the GridView and then click on the smart tag

which will appear at the upper right corner.

Page 55: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

55

Adding Update Capability

Select Enable Editing and Enable Deleting

Page 56: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

56

Source View

Page 57: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

57

Try it!

Click on “Edit” link to edit a row.

Page 58: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

58

Doing Edits

Edit textboxes then click “Update”.

Page 59: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

59

Updated Field

Page 60: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

60

Back in Visual Studio Server Explorer

Page 61: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

61

Buttons Instead of Links

Click the GridView smart tag and select Edit Columns

Page 62: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

62

Buttons Instead of Links

Click here.

Page 63: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

63

Buttons Instead of Links

1. Select “CommandField”

2. Select Button from dropdown list.

3. Click OK

Page 64: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

64

Visual Studio Design View

Page 65: 1 Data Bound Controls II Chapter 15 - 16. 2 Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available

65

The App in Action