EnableInsert="True"
EnableUpdate="True"
TableName="titles">
Select the GridView
control's SmartTag, and check the five checkboxes (see Figure 17-11).
Figure 17-11
This makes the GridView
look like Figure 17-12. The column names are now clickable and that new column containing Edit, Delete, and Select is added to the GridView
control. Also, paging is now enabled (located at the bottom of the GridView
control).
Figure 17-12
Click the Auto Format link in the SmartTag of the GridView
control, and select the Sand and Sky scheme.
The GridView
control contains all the fields of the titles
table, but there are some that you don't really need. So select the notes
column, and remove it by choosing Remove Column from GridView Tasks (see Figure 17-13). Delete the advance
, royalty
, and ytd_sales
columns 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.config
file 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 GridView
control 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 GridView
control under the pub_id
field. 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.aspx
and 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_name
property of the DataClassesDataContext
class.
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 titles
table are displayed in the GridView
control. You might want to restrict the titles displayed to a particular selected publisher. To do so, insert another LinqDataSource
control to the Default.aspx
page 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 LinqDataSource
control has the Select
attribute where you can specify the name of the fields you want to retrieve ( pub_name
and pub_id
, in this example).
Add a DropDownList
control 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 DropDownList
control to the LinqDataSource
control. The DropDownList
control will display the list of publisher names ( pub_name
), and each publisher's name has the pub-id
as its value.
Default.aspx
should 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 LinqDataSource
control so that the GridView control will only display titles from the selected publisher, click on the SmartTag of the GridView
control, 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 GridView
control display titles whose pub_id
file match the pub-id
value of the selected publisher in the DropDownList1
control.
Читать дальше