740 likes | 757 Views
Learn how to bind data to web controls, create single-expression bindings, repeated-expression bindings, and bind data to dropdown lists, list boxes, radio button lists, and more with this comprehensive guide to data binding in ASP.NET.
E N D
Chapter Objectives Introduction to ASP.NET, Second Edition
Data Binding Techniques • Data source – represents data • A simple hash table, an array, or a database • Web controls used to display data • Sometimes called Data controls because their primary function is to display data • Data can be displayed as a simple list, a drop-down list, or a table • Data binding - process of assigning data source to a Web control • Data can be bound to a variety of Web controls Introduction to ASP.NET, Second Edition
Data Binding Techniques (continued) Introduction to ASP.NET, Second Edition
Single-Expression Binding • Bind a single value or expression to a control • Pound sign (#) before the expression name • Enclose within inline server tags (<% %>) <% dim ImageURL As String = "logo.gif" %> <img name="Logo" src="<%# ImageURL %>" alt="Logo" /> Introduction to ASP.NET, Second Edition
Single-Expression Binding (Page 316) SingleBind.aspx • Create a Property • Create a Function to return a value • Bind the data in the Page_Load procedure • Return an Expression from a Property or Function • Label and text box - bound to the value from the ImageURL property • Image button - bound to the value from the GetImageURL function Introduction to ASP.NET, Second Edition
SingleBind.aspx (continued) Introduction to ASP.NET, Second Edition
SingleBind.aspx (continued) • Label and text box controls are bound to the value from the ImageURL property and the image button control is bound to the value from the GetImageURL function • Change the href property to images/<%# ImageURL %> • Change the src property of imgHTMLImageURL to <%# GetImageURL %> • Change the lblImageURL text property to <%# ImageURL %> • Change the txtImageURL value property to <%# ImageURL %> • Change the imgImageURL imageURL property to <%# GetImageURL %> Introduction to ASP.NET, Second Edition
SingleBind.aspx (continued) Introduction to ASP.NET, Second Edition
SingleBind.aspx (continued) Introduction to ASP.NET, Second Edition
Repeated-Expression Binding • Repeated expression bound to data control • collection, such as a hash table, or an ArrayList • a DataSet, DataView, or DataReader • Data controls - inherit from the ListControl and BaseDataList classes. • Controls share properties such as DataSource and templates • Repeater - inherits directly from the System.Web.UI.Control class Introduction to ASP.NET, Second Edition
Repeated-Expression Binding (continued) Introduction to ASP.NET, Second Edition
Repeated-Expression Binding (continued) • Data Controls • DataSet • A cached set of records • Contain one or more DataTables • DataMember - identify which table is bound • DataView - • Subset of rows of data from a DataTable • DataReader • Read-only, forward-only stream of data Introduction to ASP.NET, Second Edition
Data Controls • Data Binding Web Controls • DropDownList - displays one value at a time using <SELECT> • RadioButtonList, CheckBoxList - groups controls • ListBox control - displays all values - <SELECT> • Repeater - a small, lightweight control displays data. Repeats HTML content you define • DataList - displays the data as a basic list • DataGrid - repeats content once for each data row places the data in a table Introduction to ASP.NET, Second Edition
Binding Data to a DropDownList Control DropDownList.aspx (Page 323) <asp:dropdownlist id="MyDDL1" runat="server"> </asp:dropdownlist> • Assign • DataSource property • DataTextField – what’s displayed • DataValueField – Used by control as the value • DataTextFormatString – how to display - currency Introduction to ASP.NET, Second Edition
Binding Data to a DropDownList ControlDropDownList.aspx (Page 323) MyDD1.DataSource = arr1 MyDD2.DataSource = MyHash MyDD2.DataTextField = "Key" MyDD2.DataValueField = "Value" MyDD3.DataSource = MyHash MyDD3.DataTextField = "Value" MyDD3.DataValueField = "Key" MyDD3.DataTextFormatString = "{0:C}" Page.DataBind() Introduction to ASP.NET, Second Edition
DropDownList.aspx (continued) Introduction to ASP.NET, Second Edition
Binding Data to a ListBox Control ListBox.aspx (Page 326) <asp:listbox id="MyLB1" runat="server"> </asp:listbox> MyLB1.DataSource = arr1 MyLB2.DataSource = MyHash MyLB2.DataTextField = "Key" MyLB2.DataValueField = "Value" MyLB3.DataSource = MyHash MyLB3.DataValueField = "Key" MyLB3.DataTextField = "Value" MyLB3.DataTextFormatString = "{0:C}" Page.DataBind() Introduction to ASP.NET, Second Edition
ListBox.aspx (continued) Introduction to ASP.NET, Second Edition
Binding Data to a RadioButtonList and a CheckBoxList Control CheckBoxRadio.aspx (Page 327) <asp:checkboxlist id="MyCBL1" runat="server"></asp:checkboxlist> <asp:radiobuttonlist id="MyRBL1" runat="server"> </asp:radiobuttonlist> • ArrayList and HashTable bind the RadioButtonList and CheckBoxList controls • Each item - <input> • Type - radio or checkbox • DataTextField - text displayed • DataValueField - value of items Introduction to ASP.NET, Second Edition
CheckBoxRadio.aspx (continued) CheckBoxList1.DataSource = arr1 RadioButtonList1.DataSource = arr1 CheckBoxList2.DataSource = MyHash CheckBoxList2.DataTextField = "Key" CheckBoxList2.DataValueField = "Value" RadioButtonList2.DataSource = MyHash RadioButtonList2.DataValueField = "Key" RadioButtonList2.DataTextField = "Value" RadioButtonList2.DataTextFormatString = "{0:C}" Page.DataBind() Introduction to ASP.NET, Second Edition
CheckBoxRadio.aspx (continued) Introduction to ASP.NET, Second Edition
Binding to a DataGrid Control DataGridSimple.aspx (Page 329) <asp:DataGrid id="MyDG1" runat="server"></asp:DataGrid> • Data Grid can bound to various data source, in this example you will insert columns to bound to key and value items in hash table. • AutoGenerateColumns - to specify columns • HeaderText property • Change the column headings • String which can contain HTML • DataFormatString format • {0:C} Format the value as currency Introduction to ASP.NET, Second Edition
DataGridSimple.aspx (continued) Introduction to ASP.NET, Second Edition
DataGridSimple.aspx (continued) Introduction to ASP.NET, Second Edition
Binding to a DataList Control DataList.aspx (Page 332) <asp:DataList id="MyDL1" runat="server"> </asp:DataList> • DataList Control allows you to display values as a simple list. You need to identify the columns to bind to the data • DataList - a visual gray box in Design View • Auto Format schemes – DataGrid & DataList • Templates store data binding instructions • ItemTemplate - required to display the data • DataRow is referenced as a DataItem object • DataItem – display data Introduction to ASP.NET, Second Edition
DataList.aspx (continued) • DataBinder.Eval evaluates the entire expression with the parameters passed • Identify columns to bind to the data • DataList - bound to HashTable key and value <ItemTemplate> <b><%# Container.DataItem.Key %></b> (<%# DataBinder.Eval(Container.DataItem, "Value", "{0:C}") %>) </ItemTemplate> Introduction to ASP.NET, Second Edition
DataList.aspx (continued) Introduction to ASP.NET, Second Edition
Binding Data to a Repeater Control Repeater.aspx (Page 335) • Used to create tables, comma-delimited lists, bulleted lists, and numbered lists • Use HTML View - modify the control • Data inserted with an ItemTemplate • Templates available include the header, footer, alternating, item, and separator • Position the Repeater, use HTML <div> tag or ASP.NET Panel control Introduction to ASP.NET, Second Edition
Repeater.aspx (continued) • Container.DataItem represents each item • Key - key from the data source • Value - value from the data source <asp:Repeater id="Repeater1" runat="server"> <ItemTemplate> <b><%# Container.DataItem.Key %></b> (<%# Container.DataItem.Value %>)<br /> </ItemTemplate> </asp:Repeater> Introduction to ASP.NET, Second Edition
Repeater.aspx (continued) Introduction to ASP.NET, Second Edition
Binding a DataView Object to a DataGrid Control • DataReader & DataSet Data objects • Building Connections to a Database • Connection String Dim CS As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data " _ & "Source=C:\Inetpub\wwwroot\Chapter7\TaraStore.mdb" Dim objCS As New System.Data.OleDb.OleDbConnection(CS) • Import Data namespaces – First line of code Imports Namespace="System.Data.OleDb" Introduction to ASP.NET, Second Edition
Binding a DataView Object to a DataGrid Control (continued) Dim SQL As String = _ "SELECT * FROM Products WHERE SubCategoryID=1" Dim objDA As New _ System.Data.OleDb.OleDbDataAdapter(SQL, objCS) Dim objDS As New DataSet() objDA.Fill(objDS, "Products") Dim objDV As DataView objDV = objDS.Tables(0).DefaultView MyDG1.DataSource = objDV Page.DataBind() Introduction to ASP.NET, Second Edition
Binding a DataView Object to a DataGrid Control (continued) • DataAdapter - Data tab in Toolbox • Creates and manages the Connection object • Query Builder to generate the SQL Statement • Go to Data menu • Generate Dataset • Preview Data - • Dataview - Data tab in Toolbox • Table property set to DataTable (Categories) Introduction to ASP.NET, Second Edition
Binding a DataView Object to a DataGrid Control (continued) • Dataview - Data tab in Toolbox • Table property set to DataTable (ie Categories) • DataGrid.DataSource – set to DataView • Page_Load handler • Fill the DataAdapter • DataAdapter1.Fill(DataSetName1) • Bind the data control to the data source • Page.DataBind() Introduction to ASP.NET, Second Edition
Binding a DataGrid Control to the TaraStore Access Database DataGridDisplay.aspx (Page 338) Introduction to ASP.NET, Second Edition
DataGridDisplay.aspx (continued) Introduction to ASP.NET, Second Edition
DataGridDisplay.aspx (continued) Introduction to ASP.NET, Second Edition
DataGridDisplay.aspx (continued) Introduction to ASP.NET, Second Edition
Common Database Error Messages • ASP.NET Machine account • MachineName\ASPNET • Used by the Web server to run ASP.NET pages • Does not have NTFS permissions to manage the database • Go to Windows Explorer to the file • Set NTFS permissions to Modify Introduction to ASP.NET, Second Edition
Common Database Error Messages (continued) Introduction to ASP.NET, Second Edition
Microsoft Access Database Connection Error Messages • Permission to the directory that contains the file • Creates a Microsoft Access LDB file • Databasename.ldb • Record locking and other settings can be set within Access Introduction to ASP.NET, Second Edition
Microsoft Access Database Connection Error Messages (continued) Introduction to ASP.NET, Second Edition
Microsoft Access Database Connection Error Messages (continued) Introduction to ASP.NET, Second Edition
Modifying Data Columns in the DataGrid Control • Default - all columns displayed • AutoGenerateColumns - all columns generated • Columns - build columns manually • Bound columns – bound to data with DataField • Unbound columns do not automatically contain data • HeaderText - modify the column heading message Introduction to ASP.NET, Second Edition
Modifying Data Columns in the DataGrid Control (continued) • Columns have a header, footer, item section • Set style using properties in Properties window or IntelliSense <asp:BoundColumn HeaderText="Category ID" DataField="CategoryID" > ItemStyle-Font-Name="Trebuchet MS" ItemStyle-ForeColor="DarkSlateGray"> </asp:BoundColumn> Introduction to ASP.NET, Second Edition
Modifying Data Columns in the DataGrid Control (continued) • HeaderStyle, FooterStyle, ItemStyle tags • Set style using Style Templates <asp:BoundColumn HeaderText="Name" DataField="CategoryName" > <ItemStyle Font-Names="Trebuchet MS" ForeColor="DarkSlateGray"> </ItemStyle> </asp:BoundColumn> Introduction to ASP.NET, Second Edition
DisplayGridColumns.aspx (Page 348) <Columns> <asp:BoundColumn DataField="CategoryID" HeaderText="Category ID"> <ItemStyle Font-Names="Trebuchet MS" HorizontalAlign="Center" ForeColor="DarkSlateGray"> </ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="CategoryName" HeaderText="Name"> <ItemStyle Font-Names="Trebuchet MS" ForeColor="DarkSlateGray"> </ItemStyle> </asp:BoundColumn> </Columns> Introduction to ASP.NET, Second Edition
DisplayGridColumns.aspx (continued) Introduction to ASP.NET, Second Edition
DisplayGridColumns.aspx (continued) Introduction to ASP.NET, Second Edition