EnableInsert="True"
EnableUpdate="True"
TableName="titles">
Select the GridViewcontrol's SmartTag, and check the five checkboxes (see Figure 17-11).
Figure 17-11
This makes the GridViewlook like Figure 17-12. The column names are now clickable and that new column containing Edit, Delete, and Select is added to the GridViewcontrol. Also, paging is now enabled (located at the bottom of the GridViewcontrol).
Figure 17-12
Click the Auto Format link in the SmartTag of the GridViewcontrol, and select the Sand and Sky scheme.
The GridViewcontrol contains all the fields of the titlestable, but there are some that you don't really need. So select the notescolumn, and remove it by choosing Remove Column from GridView Tasks (see Figure 17-13). Delete the advance, royalty, and ytd_salescolumns as well.
Figure 17-13
The GridView control should now look like Figure 17-14.
Figure 17-14
Now, to debug the application, press F5. You are asked to modify the Web.configfile for debugging; click OK. You also are prompted that script debugging is disabled in Internet Explorer; click Yes to continue debugging.
Figure 17-15 shows the GridViewcontrol displaying the rows in the titles table. You can sort the rows by clicking on the column headers, and edit and delete records.
Figure 17-15
Displaying Publisher's Name
As Figure 17-15 shows, the publisher's ID appears in the GridViewcontrol under the pub_idfield. It would be helpful to the user if the publisher's name displayed instead of its ID. To do that, switch to the source view of Default.aspxand within the element, replace the following element:
DataField="pub_id"
HeaderText="pub_id"
SortExpression="pub_id"/>
with this:
HeaderText="Publisher">
Essentially, this changes the header for the publisher column in the GridView to Publisher, and the values are now derived from the publisher.pub_nameproperty of the DataClassesDataContextclass.
Press F5 to debug the application again to see the publishers' names instead of the publishers' IDs (see Figure 17-16).
Figure 17-16
Displaying Titles from a Selected Publisher
So far, all the titles in the titlestable are displayed in the GridViewcontrol. You might want to restrict the titles displayed to a particular selected publisher. To do so, insert another LinqDataSourcecontrol to the Default.aspxpage by adding the following highlighted code:
ID="LinqDataSource1"
runat="server"
ContextTypeName="DataClassesDataContext"
EnableDelete="True"
EnableInsert="True"
EnableUpdate="True"
TableName="titles">
ID="LinqDataSource2"
runat="server"
ContextTypeName="DataClassesDataContext"
OrderBy="pub_name"
Select="new(pub_name, pub_id)"
TableName="publishers">
Notice that the second LinqDataSourcecontrol has the Selectattribute where you can specify the name of the fields you want to retrieve ( pub_nameand pub_id, in this example).
Add a DropDownListcontrol to the top of the page by adding the following highlighted code:
Display titles by publisher:
ID="DropDownList1"
runat="server"
DataSourceID="LinqDataSource2"
DataTextField="pub_name"
DataValueField="pub_id"
AutoPostBack="True">
...
...
This addition binds a DropDownListcontrol to the LinqDataSourcecontrol. The DropDownListcontrol will display the list of publisher names ( pub_name), and each publisher's name has the pub-idas its value.
Default.aspxshould now look like Figure 17-17 in design view. You will see the text "Display titles by publisher:" as well as a dropdown list control.
Figure 17-17
To configure the first LinqDataSourcecontrol so that the GridView control will only display titles from the selected publisher, click on the SmartTag of the GridViewcontrol, and click the Configure Data Source link (see Figure 17-18).
Figure 17-18
Click Next, and then click the Where button. Enter the following values in the dialog (see Figure 17-19).
| Condition |
Value |
| Column |
pub_id |
| Operator |
== |
| Source |
Control |
| Control ID |
DropDownList1 |
Figure 17-19
Click Add, OK, and then Finish. Visual Studio 2008 will ask if you want to regenerate the GridView columns fields and data keys. Click No.
This will make the GridViewcontrol display titles whose pub_idfile match the pub-idvalue of the selected publisher in the DropDownList1control.
Читать дальше