The ASP.NET DataList

The ASP.NET DataList

DataList controls offer more options than Repeaters. In addition to displaying rows of data, the DataList can also display data in columns. Neither the Repeater nor the DataGrid can display columns.

REMEMBER TO BIND

With ASP.NET data binding, you must call the control's DataBind method. If you don't, the entire DataGrid will not appear. Forgetting to do this is a common mistake when first working with ASP.NET.

For setting up this example, create a new Web Application project and set up tables for the Northwind Products database table. Include the ProductName, QuantityPerUnit, and UnitPrice columns in the DataAdapter configuration. Fortunately, the DataList does have a couple of editors, AutoFormat and Property Builder, that help configure its appearance. However, it is still up to the developer to fill in the template columns. Listing 16.7 shows a Web Form with an example of a DataList control that shows data in columns. The use of templates is the same as for the Repeater control.

Listing 16.7 A DataList Control Web Form (WebDataList.aspx)
 <%@ Page    language="c#"    Debug="true"    Codebehind="WebDataList.aspx.cs"    AutoEventWireup="false"    Inherits="WebDataList.WebDataList" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html>   <head>     <title></title>     <meta name="GENERATOR" content=       "Borland ASP.NET Designer for c# Package Library 7.1">   </head>   <body ms_positioning="FlowLayout">   <form runat="server">     <h1>Products:     </h1>     <p>       <asp:datalist id=dataList1 runat="server" bordercolor="#CC9966"                     borderstyle="None" backcolor="White" gridlines="Both"                     borderwidth="1px" cellpadding="4"                     repeatdirection="Horizontal" repeatcolumns="3">         <selecteditemstyle font-bold="True" forecolor="#663399"                            backcolor="#FFCC66">         </selecteditemstyle>         <alternatingitemstyle backcolor="Lime">         </alternatingitemstyle>         <itemstyle forecolor="#330099" backcolor="White">         </itemstyle>         <itemtemplate>         <table>           <tr>             <th align=right>Product Name:             </th>             <td>               <%# DataBinder.Eval(Container.DataItem,"ProductName") %>             </td>           </tr>           <tr>             <th align=right>Quantity/Unit:             </th>             <td>               <%# DataBinder.Eval(Container.DataItem,"QuantityPerUnit") %>             </td>           </tr>           <tr>             <th align=right>Unit Price:             </th>             <td>               <%# DataBinder.Eval(Container.DataItem,"UnitPrice") %>             </td>           </tr>         </table>         </itemtemplate>         <footerstyle forecolor="#330099" backcolor="#FFFFCC">         </footerstyle>         <headerstyle font-bold="True" forecolor="#FFFFCC" backcolor="#990000">         </headerstyle>         <alternatingitemtemplate>         <table>           <tr>             <th align=right>Product Name:             </th>             <td>               <%# DataBinder.Eval(Container.DataItem,"ProductName") %>             </td>           </tr>           <tr>             <th align=right>Quantity/Unit:             </th>             <td>               <%# DataBinder.Eval(Container.DataItem,"QuantityPerUnit") %>             </td>           </tr>           <tr>            <th align=right>Unit Price:            </th>            <td>              <%# DataBinder.Eval(Container.DataItem,"UnitPrice") %>            </td>          </tr>        </table>        </alternatingitemtemplate>      </asp:datalist>      </p>   </form>   </body> </html> 

Because of the GUI editors, additional style tags in Listing 16.7 were placed in strange positions within the file something that wouldn't happen if a developer were manually coding. The actual binding of the data source occurs in the Page_Load method in the WebDataList.aspx.cs file, shown in Listing 16.8. This is the same binding code as described for the Repeater in the preceding section. Figure 16.11 shows what the DataList control looks like when this program runs. Remember that properties may be set in the Object Inspector while in HTML edit mode also.

Listing 16.8 Code-Behind File for the DataList Example (WebDataList.aspx.cs)
 using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace WebDataList {    /// <summary>    /// Summary description for WebForm1.    /// </summary>    public class WebDataList : System.Web.UI.Page    {       protected System.Web.UI.WebControls.DataList dataList1;       protected Borland.Data.Provider.BdpConnection bdpConnection1;       protected Borland.Data.Provider.BdpDataAdapter bdpDataAdapter1;       protected Borland.Data.Provider.BdpCommand bdpSelectCommand1;       protected Borland.Data.Provider.BdpCommand bdpInsertCommand1;       protected Borland.Data.Provider.BdpCommand bdpUpdateCommand1;       protected Borland.Data.Provider.BdpCommand bdpDeleteCommand1;       protected System.Data.DataSet dataSet1;       protected System.Data.DataTable Table1;       protected System.Data.DataColumn dataColumn1;       protected System.Data.DataColumn dataColumn2;       protected System.Data.DataColumn dataColumn3;       private void Page_Load(object sender, System.EventArgs e)       {          if (!IsPostBack)          {             dataList1.DataSource = Table1;             dataList1.DataBind();          }       }       #region Web Form Designer generated code       override protected void OnInit(EventArgs e)       {          InitializeComponent();          base.OnInit(e);       }       /// <summary>       /// Required method for Designer support - do not modify       /// the contents of this method with the code editor.       /// </summary>       private void InitializeComponent()       {          // auto-generated code removed       }       #endregion    } } 
Figure 16.11. A Web Form with the ASP.NET DataList control.

graphics/16fig11.jpg



C# Builder KickStart
C# Builder KickStart
ISBN: 672325896
EAN: N/A
Year: 2003
Pages: 165

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net