ASP.NET Part 4 Instructor: Charles Moen CSCI/CINF 4230.

Slides:



Advertisements
Similar presentations
User Controls, Master Pages, GridView. Content User Controls Styles, Themes, Master Pages Working with Data GridView Muzaffer DOĞAN - Anadolu University2.
Advertisements

11 User Controls II Chapter Objectives You will be able to Create a realistic reusable user control. Use data binding in a user control. Change.
Unit 5: Building Presentation Layer Applications with ASP.NET 2.0.
ASP.NET Data Binding. Slide 2 Lecture Overview Understanding the ASP.NET data binding model.
Web Development in Microsoft Visual Studio Slide 2 Lecture Overview Introduce Visual Studio 2013 Create a first ASP.NET application.
Introduction to Database Processing with ADO.NET.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
ASP.NET and ADO.NET. ASP.NET Server Controls Intrinsic Controls: These controls correspond to their HTML counterparts. –Ex. Textbox, listbox, button,
Chapter 14: Advanced Topics: DBMS, SQL, and ASP.NET
ASP.Net, Web Forms and Web Controls 1 Outline Introduction Simple HTTP Transaction System Architecture Creating and Running a Simple Web Form Example Web.
Philip Wolfe Senior Consultant Sogeti Session 3 - Leveraging Data in ASP.NET 2.0 (Level 200)
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.
Chapter 9 Using the SqlDataSource Control. References aspx.
ASP.NET Part 3 Instructor: Charles Moen CSCI/CINF 4230.
Ch6:creating consistent looking web sites. Master pages Master page defines a combination of fixed content and content place holder to hold the web page(.aspx)
Chapter 11 Introduction to Database Processing. Class 11: Database Processing Use a Visual Studio Wizard to establish a database connection used to load.
Programming with Visual Basic.NET An Object-Oriented Approach  Chapter 8 Introduction to Database Processing.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
ASP.Net Web Applications. Characteristics of a typical data driven web application Web Server HTML Graphics Active-X Java Applets HTTP Request ADO / JDBC.
MSIS 5133 Advanced MIS - E-Commerce Spring 2003 Lecture 4: DotNet Technologies - Part 4 Using Controls with Databases Dr. Rathindra Sarathy.
Chapter 71 Building Data-Driven ASP.NET Applications Introduction to ASP.NET By Kathleen Kalata.
1 ASP.NET ASP.NET Rina Zviel-Girshin Lecture 4. 2 Overview Data Binding Data Providers Data Connection Data Manipulations.
Developing Web Applications Using Microsoft ® Visual Studio ® 2008.
IT533 Lectures DataBinding Controls. Installations Microsoft® SQL Server® 2012 Express Download the sample sql script files from the course’s web site.
IT533 Lectures DataBinding Controls. Installations Microsoft® SQL Server® 2008 Express Download the sample MDF files from the course’s web site CodeWeek3.zip.
Needs for Accessing Database To make your web site more dynamic and maintainable, you can display information on your web pages that are retrieved from.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
Malek Kemmou Technology Architect, Application Platform Microsoft Middle East and Africa Using Visual Studio 2005 to Build Data- Driven.
Ventsislav Popov Crossroad Ltd.. 1. ASP.NET Data Source Controls  SqlDataSource  EntityDataSource  ObjectDataSource 2. Entity Data Model and ADO.NET.
Delivering Excellence in Software Engineering ® EPAM Systems. All rights reserved. Data Binding in ASP.NET.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
1 Chapter 12 – Web Applications 12.1 Programming for the Web, Part I 12.2 Programming for the Web, Part II 12.3 Using Databases in Web Programs.
ASP.NET Rina Zviel-Girshin Lecture 5
Session 8: ADO.NET. Overview Overview of ADO.NET What is ADO.NET? Using Namespaces The ADO.NET Object Model What is a DataSet? Accessing Data with ADO.NET.
Dynamic Dropdown Lists 1. Objectives You will be able to Use Dropdown Lists to solicit multiple choice user input in an ASPX web page. Populate a Dropdown.
Chapter 8 Working With Databases in ASP.NET. Listing 8.1 – ShowListControls Uses The SqlDataSource control for estabishing database connectivity and.
Database Handling, Sessions, and AJAX. Post Back ASP.NET Functionality The IsPostBack method in ASP.NET is similar to the BlackBerry.refresh method –IsPostBack.
1 CS 3870/CS 5870: Note05 Prog3 Web Application with Database.
TRAINING SESSIONS.NET Controls.  Standard Controls  Label  Textbox  Checkbox  Button, Image Button, Image control  Radio Button  Literal  Hyperlink.
11 The Repeater Control. 22 Objectives You will be able to use a Repeater Control to display data obtained with a SQL query with custom formatting.
Ventsislav Popov Crossroad Ltd.. 1. ASP.NET Data Source Controls  SqlDataSource  EntityDataSource  ObjectDataSource 2. Entity Data Model and ADO.NET.
CSE 409 – Advanced Internet Technology Today you will learn  Styles  Themes  Master Pages Themes and Master Pages.
BlackBerry Applications using Microsoft Visual Studio and Database Handling.
HNDIT Rapid Application Development
ADO.NET FUNDAMENTALS BEGINNING ASP.NET 3.5 IN C#.
1 CS387/CS587: Note04 Lab 3. 2 Master Page All Web pages will be similar Should be created before other web pages Add New Items Controls on the Master.
ASP.NET Data Binding. Slide 2 Lecture Overview Understanding the ASP.NET data binding model.
1 CS 3870/CS 5870: Note07 Prog 4. Master Pages Creating a master page based on another master page MainMasterPage –For all Progs and Tests Prog4MasterPage.
1 CS 3870/CS 5870: Note07 Prog 4. Master Pages Creating a master page based on another master page MainMasterPage –For all Progs and Tests Prog4MasterPage.
1111 Creating ASPX Controls Programatically Objectives You will be able to Dynamically add controls to a page. Dynamically alter properties of controls.
Module 10: Data Access in ASP.NET. Overview Overview of the ASP.NET Data Access What is Data Access List of Data Source Control What is Data Bound How.
11 User Controls Beginning ASP.NET in C# and VB Chapter 8.
ASP.NET Part 2 Instructor: Charles Moen CSCI/CINF 4230.
Working with Data Lesson 4. Objectives DataSource Controls A requirement for most modern Web sites is that they be dynamic. –Static pages are not often.
Introduction to Database Processing with ADO.NET
Displaying and Updating Data
Current Popular IT I Pertemuan 5
Introduction to Database Processing with ADO.NET
ADO.NET Framework.
Active Data Objects Binding ASP.NET Controls to Data
Listing 9.1 ShowLocalConnection.aspx
ASP.NET and ADO.NET.
VB.NET Using Database.
Using List Controls with SQL Server
ASP.NET.
E-commerce Applications Development
Chapter 10 Accessing Database Files
ASP.NET and ADO.NET.
Active Data Objects Binding ASP.NET Controls to Data
Presentation transcript:

ASP.NET Part 4 Instructor: Charles Moen CSCI/CINF 4230

ADO.NET (Continued from last week)

3 Disconnected Data Access Fetch the data and store it in memory Steps to query the database with direct data access 1. Create Connection and Command objects 2. Create DataSet and DataAdapter objects 3. Retrieve information from the database with the DataAdapter object and add it to the DataSet object 4. Close the connection 5. Interact with the data in your code ASP.NET (MacDonald)‏ using System.Data; using System.Data.OleDb; using System.Web.Configuration; In your C# code, import the correct ADO.NET namespaces

4 Deli Menu Demo ASP.NET (MacDonald)‏  Use ASP.NET and Visual Studio 2008 to build a web application to display the results of a menu search by category using a DropDownList

5 Demo web.config ASP.NET (MacDonald)‏  Define the connection string in the web.config file so that it is available throughout your application NOTE: This is a partial file. Points to the App_Data folder inside your web application directory The alias that you can use in your code

6 Demo Default.aspx.cs ASP.NET (MacDonald, Walther)‏ using System; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.OleDb; using System.Web.Configuration; public partial class _Default : System.Web.UI.Page { private string connectionString = WebConfigurationManager.ConnectionStrings["Deli"].ConnectionString; private DataSet ds; Add a DataSet object The DataSet stores the data that was retrieved from the database, so it can be considered to be an in-memory database

7 Demo Page_Load ASP.NET (MacDonald)‏ protected void Page_Load(object sender, EventArgs e) { string selectSql = "SELECT id, category FROM Categories "; OleDbConnection conn = new OleDbConnection(connectionString); OleDbCommand cmd = new OleDbCommand(selectSql, conn); OleDbDataAdapter adapter = new OleDbDataAdapter(cmd); ds = new DataSet(); try { conn.Open(); adapter.Fill(ds, "Categories"); } catch (Exception error) { resultsLabel.Text = "ERROR: " + error.Message; } finally { conn.Close(); } if (!IsPostBack) { foreach (DataRow row in ds.Tables["Categories"].Rows) { ListItem newItem = new ListItem(); newItem.Text = row["category"].ToString(); newItem.Value = row["id"].ToString(); categoriesDropDownList.Items.Add(newItem); } Create the DataSet and DataAdapter objects After storing the data in memory, close the connection Retrieve the data with the DataAdapter object and fill the DataSet object with it Then iterate through the data stored in memory to create the list The DataAdapter is the object that is used to transfer data from the physical database to the DataSet, the “in-memory” database

8 Demo Page_Load ASP.NET (MacDonald)‏ protected void Page_Load(object sender, EventArgs e) { string selectSql = "SELECT id, category FROM Categories "; OleDbConnection conn = new OleDbConnection(connectionString); OleDbCommand cmd = new OleDbCommand(selectSql, conn); OleDbDataAdapter adapter = new OleDbDataAdapter(cmd); ds = new DataSet(); try { conn.Open(); adapter.Fill(ds, "Categories"); cmd.CommandText = "select * from MenuItems"; adapter.Fill(ds, "MenuItems"); } catch (Exception error) { resultsLabel.Text = "ERROR: " + error.Message; } finally { conn.Close(); } if (!IsPostBack) { foreach (DataRow row in ds.Tables["Categories"].Rows) { ListItem newItem = new ListItem(); newItem.Text = row["category"].ToString(); newItem.Value = row["id"].ToString(); categoriesDropDownList.Items.Add(newItem); } The DataSet object can hold multiple tables What if we want to use the data from both tables on this page?

9 Demo categoriesDropDownList_SelectedIndexChanged ASP.NET (MacDonald)‏ protected void categoriesDropDownList_SelectedIndexChanged(object sender, EventArgs e) { if (IsPostBack) { StringBuilder sb = new StringBuilder(); sb.Append(" "); foreach (DataRow menuItemRow in ds.Tables["MenuItems"].Rows) { if (menuItemRow["category"].ToString() == categoriesDropDownList.SelectedValue) { sb.Append(" "); sb.Append(menuItemRow["item"].ToString()); sb.Append(" "); sb.Append(menuItemRow["description"].ToString()); sb.Append(" "); sb.Append(menuItemRow["price"].ToString()); sb.Append(" "); } sb.Append(" "); resultsLabel.Text = sb.ToString(); } An HTML table can be built by interacting with the data that is stored in memory inside the DataSet object

Data Binding

11 Data Binding  Binding a control to a data source  Single-value binding Inserted in almost any element in the aspx file Insert a variable, property, or expression into a page  Repeated-value binding Works with ASP.NET list controls that support data binding Set the control properties  DataBind() Method of the Page class Activates the data bindings, typically called in the Page_Load handler ASP.NET (MacDonald)‏

12 Single-Value Binding 1.Insert a data binding expression into the aspx code Could be a variable, property, or expression 2.Call DataBind() in the code ASP.NET (MacDonald)‏ Opening and closing symbols A data binding expression

13 Single-Value Binding Example ASP.NET (MacDonald)‏ Today is public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DataBind(); } 2. Call DataBind() to see the output on the page 1.Insert a data binding expression into the aspx code

14 Some questions about single-value binding  What are some problems with single-value binding? ANSWER: It mixes the code with the presentation layer instead of keeping it all in one place, inside the code-behind file  What shows up on the page when you forget to call DataBind() ?  What shows up on the page when the following data binding expression is used in the aspx code? ASP.NET (MacDonald)‏  What would the URL look like?

15 Repeated-Value Binding 1.Create a data object, such as an ArrayList, and fill it with data 2.Link the data object to a control Some controls that support data binding: ListBox, DropDownList, CheckBoxList, RadioButtonList, HtmlSelect, GridView, FormView, ListView 3.Call DataBind() in the code ASP.NET (MacDonald)‏

16 Repeated-Value Binding Example ASP.NET (MacDonald)‏ public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ArrayList categories = new ArrayList(); categories.Add("Sandwiches"); categories.Add("Salads"); categories.Add("Pasta"); categoriesDropDownList.DataSource = categories; DataBind(); } 1.Create a data object filled with data 2.Link the data object with the control – not necessary to use a loop to add the ListItems 3.Call DataBind() to see the output on the page Add a list control that supports data binding in the aspx code

17 Data Source Controls  We can use data source controls to interact with a database without having to write the data access code  SqlDataSource Connect to any data source that has an ADO.NET data provider  AccessDataSource Connect to an Access database file  ObjectDataSource  XmlDataSource Connect to an XML file  SiteMapDataSource ASP.NET (MacDonald)‏

18 Data Source Control Example ASP.NET (MacDonald)‏ First, add the db.mdb file to the App_Data folder in Visual Studio Then, add the connection string to the web.config file

19 Data Source Control Example ASP.NET (MacDonald)‏ <asp:SqlDataSource ID="sourceCategories" runat="server" /> Add a SqlDataSource control

20 Data Source Control Example ASP.NET (MacDonald)‏ <asp:SqlDataSource ID="sourceCategories" runat="server" ProviderName="System.Data.OleDb" ConnectionString=" " /> Add the namespace for the correct provider as the value of the “ProviderName” property Use the alias of the connection string that you created in the web.config file To refer to a connection string in the aspx file, use this syntax

21 Data Source Control Example ASP.NET (MacDonald)‏ <asp:SqlDataSource ID="sourceCategories" runat="server" ProviderName="System.Data.OleDb" ConnectionString=" " SelectCommand="select id, category from categories" /> The SQL command can be added as an inline string The SqlDataSource control can have one each of the following commands: SelectCommand InsertCommand UpdateCommand DeleteCommand (Although some controls do not support all operations)

22 Data Source Control Example ASP.NET (MacDonald)‏ <asp:SqlDataSource ID="sourceCategories" runat="server" ProviderName="System.Data.OleDb" ConnectionString=" " SelectCommand="select id, category from categories" /> <asp:Label ID="categoriesLabel" runat="server" Text="Categories: " /> Add the label control

23 Data Source Control Example ASP.NET (MacDonald)‏ <asp:SqlDataSource ID="sourceCategories" runat="server" ProviderName="System.Data.OleDb" ConnectionString=" " SelectCommand="select id, category from categories" /> <asp:Label ID="categoriesLabel" runat="server" Text="Categories: " /> <asp:DropDownList ID="categoriesDropDownList" runat="server" DataSourceID="sourceCategories" /> Add the DropDownList control Link the data object with the control using the “DataSourceID” property

24 Data Source Control Example ASP.NET (MacDonald)‏ <asp:SqlDataSource ID="sourceCategories" runat="server" ProviderName="System.Data.OleDb" ConnectionString=" " SelectCommand="select id, category from categories" /> <asp:Label ID="categoriesLabel" runat="server" Text="Categories: " /> <asp:DropDownList ID="categoriesDropDownList" runat="server" DataSourceID="sourceCategories" DataTextField="category" DataValueField="id" /> Set the “DataTextField” property and the “DataValueField” property with the values of the fields specified in the query

25 Advantages of the Data Source Controls  It was not necessary to write any C# code, such as creating the Connection, Command, and Reader objects; using try-catch-finally blocks; or using a loop to populate the list  The control could have been added and configured by using Visual Studio features such as the Design view, the Toolbox, and the Properties editor ASP.NET (MacDonald)‏

26 Data Controls Rich data controls allow you to bind an entire table of data, using a Data Source Control GridView DetailsView FormView ASP.NET (MacDonald)‏

27 GridView Example ASP.NET (MacDonald)‏ <asp:SqlDataSource ID="menuItemsSource" runat="server" ProviderName="System.Data.OleDb" ConnectionString=" " SelectCommand="select * from MenuItems" /> Add to your Web Site: The db.mdb file A connection string in web.config A SqlDataSource control in the aspx file

28 GridView Example ASP.NET (MacDonald)‏ <asp:SqlDataSource ID="menuItemsSource" runat="server" ProviderName="System.Data.OleDb" ConnectionString=" " SelectCommand="select * from MenuItems" /> <asp:GridView ID="GridView1" runat="server" DataSourceID="menuItemsSource" /> Add the GridView control Bind it to the SqlDataSource

29 GridView Example ASP.NET (MacDonald)‏ Change the GridView control in the Design view Auto Format... Edit Columns... Add New Column... Enable Paging Enable Sorting

Master Pages

31 Master Pages  Used to define the layout of multiple pages in your web site Page templates are used to define features such as headers, footers, navigation panels Use the.master file extension Must be used with content pages which are inserted at the location of the ContentPlaceHolder controls  Content pages Inserted into the master page layout Acquires the layout of the master page ASP.NET (MacDonald)‏

32 Master Page Example ASP.NET (MacDonald)‏ To add a master page: 1.In the “Website” menu, select “Add New Item” 2.Select “Master Page” The master directive identifies the master page Content pages go into the ContentPlaceHolder

33 Master Page Example ASP.NET (MacDonald)‏ Add elements to the master page outside of the ContentPlaceHolder.banner { font-family: Arial, Helvetica, sans-serif; background-color: #800000; color: #FFFFFF; font-weight: bold; padding: 5px; } Garden Fresh Sandwich Deli

34 Master Page Example ASP.NET (MacDonald)‏ To add a content page: 1.In the “Website” menu, select “Add New Item” 2.Select “Web Form” 3.Check “Select master page” in the dialog The page directive connects it to the master page The Content element with the “head” id should contain styles or scripts that are specific to a particular content page The page content should be inserted into this Content element

35 Master Page Example ASP.NET (MacDonald)‏ Sandwiches Salads Pasta

36 References MacDonald, Matthew, Beginning ASP.NET 3.5 in C# 2008: From Novice to Professional, Second Edition. Apress, Walther, Stephen. ASP.NET 3.5 Unleashed. SAMS, 2008.