Binding Controls


The next step for creating the guestbook listing page is to bind the controls in the template to the data. To bind controls, you create a data-binding expression for each Label control that you added to the template. A data- binding expression is a short line of code that runs after the SQL query has executed and that sets the property of a control to a data value. That description of a data-binding expression probably sounds more complicated than it really is, as you ll see in a moment.

Earlier in the chapter, I recommended that you make a note of the column names for the Guestbook table. You ll need those names now to perform data binding.

Bind the template controls to data

  1. In the template editor, select the labelGuestName control.

  2. In the Properties window, scroll up to the top property, which is (DataBindings). In the Data Bindings box, click the ellipsis button. Web Matrix displays the following DataBindings dialog box:

    click to expand

At this point, you run into a limitation in Web Matrix. Ideally, the DataBindings dialog box would create the data-binding expression for you. However, the DataBindings dialog box can t completely define data-binding expressions for you if you re using the SqlDataSourceControl control, as you are. Fortunately, however, the dialog box can do most of the binding for you, and you ll need to do only a small bit of the work yourself.

  • Make sure that under Bindable Properties on the left, the Text property is selected. You can actually read database data into any property of the Label control (although of course it doesn t make sense for most properties). In this case, you want the Label control s Text property to get its value from the SQL query.

  • Under Binding For Text on the right, open the Container node and select DataItem.

    When the control is performing its data binding later, the DataItem will contain the current row out of the query s result set. Remember that when the page is running, the DataList control will use the template to create the display for each record row out of the result set in turn. By referencing the DataItem, you re specifying that the Label control get its data from whatever data record the DataList control is processing at that moment.

  • Click the Custom Binding Expression option button toward the bottom of the dialog box. Notice that Web Matrix has built an expression out of the choices you ve made so far. You just need to complete the expression. You re working with the labelGuestName control at the moment, so modify the data-binding expression by adding the name highlighted in the following line:

    DataBinder.Eval(Container, "DataItem.GuestName")

    In other words, you re adding a period plus the name of the data column from the Guestbook table whose data you want. When you re finished, the dialog box will look like this:

    click to expand

  • As you can see, the data-binding expression consists of a call to the Eval method of the DataBinder object. You use this method to bind labels to fields in the data source. The first parameter passed to the method is a reference to the object that owns the data. In this data- binding expression, the object that owns the data is Container, which is a special object in the DataList control used to hold an item template. The second parameter specifies where the data actually is. The DataItem reference, as I just noted, refers to the current data row. You ve added the name of the column to extract. You don t actually need to understand the syntax of the data-binding expression in detail; you can just use the syntax illustrated in the preceding example whenever you re binding a control in a template to data.

  • Click OK to return to the template editor.

  • Select the labelEmail control and repeat steps 2 through 6 to create its data-binding expression. The only difference will be that you use a different column name in the data-binding expression. For the labelEmail control, the data-binding expression will read as follows:

    DataBinder.Eval(Container, "DataItem.EmailAddress")
  • Repeat the same process for the labelEntryDate control, creating this data- binding expression:

    DataBinder.Eval(Container, "DataItem.EntryDate")
  • Finally, create a data-binding expression for the labelComment control, which will look like this:

    DataBinder.Eval(Container, "DataItem.Comment")
  • That s it for binding the controls in the template you re done for now with the Item template. Don t close the template editor yet!




    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