150 likes | 291 Views
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
E N D
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 • 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#" %>
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
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");
Master & Content Pages Replacing placeholder content
Data Binding • Goal: • To able to connect to data sources to obtain data • Relational Databases (SQL) • Object Databases and O/R mappers • XML files
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.
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).
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
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>
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"/>
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
Using the VS 5.0 DataSet Wizard • DAL / BLL is tedious work • VS 5.0 has wizard
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