Componentization
<asp:BoundField DataField=”LastName” HeaderText=”LastName” SortExpression=”LastName” />
<asp:BoundField DataField=”Position” HeaderText=”Position” SortExpression=”Position” />
<asp:BoundField DataField=”DateJoined” HeaderText=”DateJoined”
SortExpression=”DateJoined” />
<asp:BoundField DataField=”DateLeft” HeaderText=”DateLeft” SortExpression=”DateLeft” />
</Columns>
</asp:GridView>
The GridView control binds each of the fields in the dataset to a column in a table on the display. The ObjectDataSource takes five attributes. The ID and runat attributes are standard, the
OldValuesParameterFormatString is set to the original setting, the SelectMethod attribute specifies the name of the actual method, and the TypeName specifies the PlayersTableAdapter. These are all the instructions needed to be able to bind the return PlayersTable to the GridView control. The final display looks the same as a normal GridView control; it’s only the plumbing underneath that has routed your data from the ObjectDataSource control that is different.
The Wrox United ObjectDataSource
This data so far has been static; so as we mentioned previously, in this next Try It Out you go one further and use the ObjectDataSource control to be able to edit and update your squad’s details. In the Admin section of the Wrox United site there is a page called EditSquad.aspx, which is used to change the players’ details. However, it uses the SqlDataSource control for the details. This can be replaced with an ObjectDataSource control. It has the insert, update, select, and delete methods to map neatly to the methods in a simple class.
Try It Out |
The Wrox United ObjectDataSource |
1.Open the Wrox United application from the chapter samples (C:\BegASPNET2\Chapters\ Begin\Chapter10\WroxUnited) in Visual Web Developer.
2.Right-click the App_Code folder and select Add New Item from the list.
3.From the Visual Studio installed templates, click DataSet.
4.Rename the DataSet wroxunited.xsd and click Add.
5.Next, VWD will start the TableAdapter Configuration Wizard. Be patient here, because this one really does take a while to kick in.
6.When it finally arrives, select wroxunited (Web.config), and click Next.
7.Next, you get a page where you can choose to use SQL statements or stored procedures. Select the Use SQL statements radio button and click Next.
8.On the next wizard screen you can define the SQL statement. Type the following SQL statement into the “What data should be loaded into the table” area of the dialog: