ASP.NET Logo Back to WebMatrix Home

 
Build an End-to-End Application
Create Master/Detail Data page
In this walkthrough, you will create a new Data Page based on the Master - Details Grid project template. You will bind the Data Page to the Orders and OrderDetails tables in the Orders database.
  1. Select New from the File menu. You will see the New File dialog box appear.
  2. Select Data Pages from the Templates pane.
  3. Select the Master - Details Grids template.
  4. Type a file path in the Location box.
  5. Type Default.aspx in the Filename box.
  6. Select Visual Basic in the Language drop-down box.
  7. Click OK. The Default.aspx file will open in Design view. 

    New File Dialog

  8. Select the Code tab.

    Note The ASP.NET Web Matrix has automatically placed code to bind the MasterGrid to a subroutine called BindMasterGrid and the DetailsGrid to a subroutine called BindDetailGrid.

  9. Modify the ConnectionString and CommandText variables in the BindMasterGrid subroutine so they reference the columns in the Orders table in the Orders database.

     

    Dim ConnectionString As String = "server=(local);database=Orders;Integrated Security=SSPI"
    
    Dim CommandText As String = "select OrderID, OrderDate, CustomerName from Orders"
    
  10. Modify the ConnectionString and CommandText variables in the BindDetailGrid subroutine so they reference the columns in the OrderDetails table in the Orders database.

     

    Dim ConnectionString As String = "server=(local);database=Orders;Integrated Security=SSPI"
    
    Dim CommandText As String = "select OrderDetailID, ProductName, Quantity, UnitPrice " & _
        "from OrderDetails where OrderID = '" & filterValue & "'"                            
    
  11. Select the Design tab.

     
  12. Set the DataKeyField property of the MasterGrid to OrderID. The DataKeyField property is used to filter rows in the detail grid. 

    DataKeyField Property

  13. Replace the default page header text (Master - Detail Grids) with a Label control from the Web Controls tab in the Toolbox.

     
  14. Set the ID property of the Label to Welcome

    Updated Form

  15. Select the Code tab.

     
  16. Modify the Page_Load event code so it sets the Text property of the Label to the username of the current user and binds the data grids:

     

    Sub Page_Load(Sender As Object, E As EventArgs)
    
        Welcome.Text = "Hello, " + User.Identity.Name
    
        If Not Page.IsPostBack Then
    
            ' Databind the master grid on the first request only
            ' (viewstate will restore these values on subsequent postbacks).
    
            MasterGrid.SelectedIndex = 0
            BindMasterGrid()
            BindDetailGrid()
    
        End If
    
    End Sub
    
  17. Save and run the ASP.NET page.

     
  18. Your ASP.NET page will automatically appear in a new instance of your Web browser. Select a row in the MasterGrid. Note how the DetailsGrid updates with each selected MasterGrid row.  Note that the username may or may not show up depending upon your file system security settings. By default on most servers, the username will not show up -- because a user accessing the page does not need to be logged in to view it (anonymous access is allows). We will be changing these defaults in the next few walkthroughs to force users to login.

    Master Detail DataGrids

  19. Close the Web browser instance.

Next Step >>