1 / 15

Internetteknologi (ITNET2)

Internetteknologi (ITNET2). Presentation 21: ASP.NET Advanced. Agenda. User Controls Master & Content Pages Data Binding (Databases, Collections & XML). What is a User Control?. User controls simplify the reuse of code and UI components within a Web application

essien
Download Presentation

Internetteknologi (ITNET2)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Internetteknologi (ITNET2) Presentation 21: ASP.NET Advanced

  2. Agenda • User Controls • Master & Content Pages • Data Binding (Databases, Collections & XML)

  3. What is a User Control? • User controls simplify the reuse of code and UI components within a Web application • A user control is a user-defined Web server control with an .ascx extension • Contains HTML, but not the <HTML>, <BODY>, or <FORM> tags or • Contains code to handle its own events <%@ Control Language="vb" %> <%@ Control Language="c#" %>

  4. Control1.ascx Page1.aspx Page2.aspx Why Use User Controls? • Reuse user interface and code • Must live in Web form! Application A Application B Page3.aspx

  5. Master & Content Pages • Structuring Mechanism • Replaces “Include” • Master Pages “includes” different Content Pages • Master Pages attributes reachable from Content Pages Master.FooterText = "This is a custom footer"; AdRotator ad = (AdRotator)Master.FindControl("MyAdRotator");

  6. Master & Content Pages Replacing placeholder content

  7. Data Binding • Goal: • To able to connect to data sources to obtain data • Relational Databases (SQL) • Object Databases and O/R mappers • XML files

  8. Data Sources • SqlDataSource • Enables binding to a SQL database represented by an ADO.NET provider, such as Microsoft™ SQL Server, OLEDB, ODBC, or Oracle. • ObjectDataSource • Enables binding to a middle-tier object such as a data access layer or business component. • AccessDataSource • Enables binding to a Microsoft™ Access (Jet) database. • XmlDataSource • Enables binding to an XML file or document. • SiteMapDataSource • Enables binding to the hierarchy exposed by an ASP.NET 2.0 site navigation provider.

  9. Data Bound Controls • GridView • Renders data in a grid format. This control is an evolution of the DataGrid control, and can automatically take advantage of data source capabilities. • DetailsView • Renders a single data item in a table of label/value pairs, similar to the form view in Microsoft™ Access. This control can also automatically take advantage of data source capabilities. • FormView • Renders a single data item at a time in a form defined by a custom template. Renders a single data item in a table of label/value pairs, similar to the form view in Microsoft™ Access. This control can also automatically take advantage of data source capabilities. • TreeView • Renders data in a hierarchical tree view of expandable nodes. • Menu • Renders data in a hierarchical dynamic menu (including flyouts).

  10. Displaying Data from a RDBMS in a Grid View • Create a DB • Create a Connection to DB (Web.Config) • Create a SQL Data Source & Grid View <configuration> <connectionStrings> <add name="Pubs“ connectionString="Server=(local);Integrated Security=True;Database=pubs;" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration> <form runat="server"> <asp:GridView ID="GridView1" DataSourceID="SqlDataSource1" runat="server"/> <asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT [au_id], [au_lname], [au_fname] FROM [authors]" ConnectionString="<%$ ConnectionStrings:Pubs %>" /> </form> http://www.asp.net/QuickStart/aspnet/samples/data/GridViewBoundFields_cs.aspxa

  11. Paging & Sorting • Paging is supported at GUI • Sorting is supported at GUI • Columns may be automatic or manually set up <asp:GridView ID="GridView1" AllowSorting="true" AllowPaging="true" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False"> • … • <Columns> • <asp:BoundField HeaderText="ID" DataField="au_id" SortExpression="au_id" /> • … • <Columns> • </asp:GridView>

  12. Using a DAL public class MyDataLayer { public DataView GetRecords(); public DataView GetRecordsByCategory(String categoryName); public DataView GetRecordByID(int recordID); public int UpdateRecord(int recordID, String recordData); public int DeleteRecord(int recordID); public int InsertRecord(int recordID, String recordData); } • Use an object as a DAL for controlled access to DB • Server Control (ObjectDataSource) using MyDataLayer <asp:ObjectDataSource TypeName="MyDataLayer" SelectMethod="GetRecords"UpdateMethod="UpdateRecord"DeleteMethod="DeleteRecord"InsertMethod="InsertRecord" runat="server"/>

  13. Using a Business Logic Layer Provides us with a nice OO decoupling Value objects must have get/set methods http://www.asp.net/QuickStart/util/srcview.aspx?path=~/aspnet/samples/data/GridViewObject.src

  14. Using the VS 5.0 DataSet Wizard • DAL / BLL is tedious work • VS 5.0 has wizard

  15. Binding the DataTable to View Control • A DataTable and a TableAdapter is constructed • Add an ObjectDataSource • TypeName="DataSet1TableAdapters.personTableAdapter” • Add a GridView and/or a FormView Server Control

More Related