ASP.NET Logo Back to WebMatrix Home

 
ASP.NET Pages with Data
DataList and Templates
The DataList server control provides additional control over bound data through user-defined templates. The template allows you to control the look and feel of the list's Items, Header, Footer, etc.

In this walkthrough, you will develop an ItemTemplate that binds a Label to the ProductName column in the OrderDetails table.

  1. Open the myFirstDataPage.aspx file in Design view.

     
  2. Remove the DataGrid from the ASP.NET page.

     
  3. From the Web Controls tab in the Toolbox, drag a DataList control onto the ASP.NET page. Place the DataList in the same position on the form as the deleted DataGrid

    Add CB DataList

  4. Right-click on the DataList and select Edit Templates.

     
  5. You will see the Edit Templates dialog box. 

    Edit Templates Dialog

  6. Select ItemTemplates from the Select a Template to edit drop-down box.

     
  7. Select ItemTemplate from the Template Type drop-down box.

     
  8. Type ProductName: in the Template Design box.

     
  9. From the Web Controls tab in the Toolbox, drag a Label control onto the Template Design box, immediately to the right of ProductName:

    Place ItemTemplate Controls

  10. Select the Label in the Template Design box.

     
  11. Select the (DataBindings) property in the Properties window. 

    DataBindings Property

  12. You will see the DataBindings dialog box appear. The DataBindings dialog box displays a list of properties for the control and allows you to specify what data source properties are bound to. 

    DataBindings Dialog

  13. Select the Text node in the Bindable Properties tree view (default).

     
  14. Select the Custom binding expression radio button.

     

    Custom Binding Dialog

  15. Type DataBinder.Eval(Container.DataItem, "ProductName") in the Custom binding expression box. This expression binds the value of the OrderDetailID column to the Text property of the Label control for each row in the database table.

     
  16. Click OK.

     

    Note The Label will have square brackets surrounding it. This indicates that the control is now data bound.

  17. Click OK to close the Edit Templates dialog box.

     
  18. Select the Code tab.

     
  19. Modify the Button Click event code so that it populates the DataList with the results of calling the GetOrderDetails function:

     

    Sub Button1_Click(sender As Object, e As EventArgs)
    
        DataList1.DataSource = GetOrderDetails(CInt(TextBox1.Text))
        DataList1.DataBind()
        
    End Sub 
  20. Select the HMTL tab.

     
  21. Add attributes to the DataList HTML tag that renders a black border:

     

    <asp:DataList id="DataList1" runat="server"
        BorderColor="Black" BorderStyle="Solid" BorderWidth="2">
    </asp:datalist>
    
  22. Save and run the ASP.NET page.

     
  23. Your ASP.NET page will automatically appear in a new instance of your Web browser.

     
  24. Type 1 into the TextBox and press the Button.

     
  25. The DataList will show all of the rows in the OrderDetails table for the specified OrderID

    Final DataList

  26. Close the Web browser instance.

Next Step >>