Creating a Second Guestbook Listing Page


To display the guestbook using the DataList control, you ll create a second guestbook listing page. You ll start by creating the page and configuring it to read data from the database.

Create the View Guestbook page and add a data control

  1. Create a new page in Web Matrix, and name it  ViewGuestbookList.aspx.

  2. Add a heading at the top like Guestbook Entries.

  3. Make sure you have a connection to the WebMatrix database in MSDE. If you closed Web Matrix, you ll need to reconnect to the database. Follow the steps under Creating the Guestbook Database Table in Chapter 10.

  4. From the Data window, drag the Guestbook table onto the page. Web Matrix creates a SqlDataSourceControl control and an MxDataGrid control. Select the MxDataGrid control and delete it.

    Did that confuse you? Dragging a table onto the page and deleting the MxDataGrid control is a shortcut. You need a SqlDataSourceControl control on the page that retrieves data from the Guestbook table. In theory, you could drag a SqlDataSourceControl control from the Toolbox and then configure its ConnectionString and SelectCommand properties by hand. But it s much faster to let Web Matrix configure the SqlDataSourceControl control for us by dragging a table onto the page. Of course, in the process you got an MxDataGrid control for free. You don t need the grid in this page, so you can just delete the control.

    Note 

    To be clear, dragging a table onto the page and then deleting the grid is something of a workaround. Ideally, Web Matrix should help us configure the SqlDataSourceControl control by providing builders for the connection string and the SQL Select command. However, Web Matrix doesn t include any builders for the connection string and SQL statements, and building those property values by hand is prone to error. So you use the shortcut just described, which is slightly unorthodox but fast and effective.

  5. Select the SqlDataSourceControl control and in the Properties window, note that the ConnectionString and the SelectCommand properties are filled in.

The ConnectionString property is filled in with the server name for your instance of MSDE, the name of the database, and the login credentials. The SelectCommand property is set to SELECT * FROM [GuestBook]. Because the asterisk (*) means everything in SQL, this statement returns all the columns and rows defined in the Guestbook table.

Important 

Make a note of the column names in the Guestbook table. You ll need the names of the columns later when you re creating the template for the DataList control. If you don t remember the exact names of the columns, you can double-click the Guestbook table in the Data window. Doing so produces a data listing with the column names on the top.

You ve created the page and configured it to be able to access the Guestbook table in the WebMatrix database. Now it s time to add the DataList control and create a template to display the data.

Add a DataList control and lay out a template

  1. From the Toolbox, drag a DataList control onto the page. Your page will look something like this:

    click to expand

As you might already understand, Web Matrix uses the gray boxes on the page to display controls that don t have a visual representation. The SqlDataSourceControl doesn t render anything. The DataList control will eventually render individual items, but not until you create the template with the data display layout.

  • Select the DataList control.

  • In the Properties window, go to the drop-down list for the DataSource property and select SqlDataSourceControl1.

    By setting this property, you re configuring the DataList control so that it will get its data from the query defined in the SqlDataSourceControl. Notice that you don t need to specify any connection information or a SQL statement; that information is encapsulated in the SqlDataSourceControl and you can simply point the DataList control at it.

  • Right-click the DataList control, and choose Edit Templates.

    Web Matrix displays the Edit DataList1 Templates dialog box, as shown here:

    click to expand

  • Under Select A Template To Edit, select Item Templates from the drop- down list. In the right-hand drop-down list, make sure that ItemTemplate is selected.

    The Template Design box is a small editor that looks like the page itself. You ll be able to add text and controls here.

    Note 

    Don t save the page while you re working in the template editor a small Web Matrix bug will make some control properties unavailable if you do.

  • In the Template Design box, type in Name:. Select the text, and make it bold.

  • From the Toolbox, drag a Label control into the Template Design box, putting it right after the Name: text. Select the Label control, and in the Properties window, set its ID property to labelGuestName and clear its Text property.

    The template will look like this:

    click to expand

  • Type in several spaces, and then type in E-mail:. Make the text bold, too.

  • Drag another Label control to the right of E-mail:. Set its ID property to labelEmail and clear its Text property as well.

  • Press SHIFT+ENTER to move to the next line, and then type When:. Make the text bold. I recommend SHIFT+ENTER instead of ENTER so that you end up with single-line spacing.

  • Drag another Label control to the right of When:, name it labelEntryDate, and clear its text.

  • Press SHIFT+ENTER to make a third line, and type Comment:. Make the text bold.

  • Press SHIFT+ENTER one more time to create a fourth line. Drag a fourth Label control to the new line underneath Comment:, name it labelComment, and clear its text.

    The template editor will look like this:

    click to expand

  • The various Label controls you ve added to the template are placeholders for the data from the WebMatrix database.

    Important 

    Don t close the template editor yet! If you do, you might not be able to continue editing the template, as explained later in the chapter in the sidebar Trouble in the Template Editor.




    Microsoft ASP. NET Web Matrix Starter Kit
    Microsoft ASP.NET Web Matrix Starter Kit (Bpg-Other)
    ISBN: 0735618569
    EAN: 2147483647
    Year: 2003
    Pages: 169
    Authors: Mike Pope
    BUY ON AMAZON

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