Introduction
In the previous tutorial we saw how to use two pages (a home page that lists vendors; A detail page that displays the products offered by the selected vendor) create a master/from report. This two-page report format can also be concentrated on one page. This tutorial will use a GridView that contains the name and unit price of the product and a selection button for each row. Clicking a selection button on a product displays all of the product's details on the DetailsView control on the same page.
Figure 1: Click the Select button to display the product details
Step 1: Create a GridView with selectable rows
Recall the previous cross-page master/detail report, which contains a hyperlink for each master record, and when clicked, go to the details page and pass the SupplierID value of the clicked row in the query string. This link is implemented by using HyperLinkField on the GridView. For a single page master/detail report, each GridView line requires a button that displays details when the button is clicked. The GridView control can contain a selection button in each row, and clicking the button causes a postback and makes the row the Selectedrow value of the GridView.
First, add a GridView control to the Detailsbyselecting.aspx page under the filtering folder, set the ID property to Productsgrid, Then add a new ObjectDataSource and name it Allproductsdatasource, which calls the Productsbll class's GetProducts () method
Figure 2: Create a ObjectDataSource and name it Allproductsdatasource
Figure 3: Using the PRODUCTSBLL class
Figure 4: Configuring the ObjectDataSource Call GetProducts () method
Edit the fields of the GridView, removing fields other than ProductName and UnitPrice. You can also customize the binding field as needed, such as formatting the Utitprice field as currency, and modifying the HeaderText property of the bound field. These actions can be completed in Design view, click the Edit column on the GridView smart tag, or manually configure the declaration syntax.
Figure 5: Removing columns other than ProductName and UnitPrice.
The final GridView mark is as follows:
<asp:gridview id= "Productsgrid" runat= "Server" autogeneratecolumns= "False" datakeynames= "ProductID
" Datasourceid= "Allproductsdatasource" enableviewstate= "False" >
<Columns>
<asp:boundfield Datafield= "ProductName" headertext= "Product" sortexpression= "ProductName"/> <asp:boundfield "datafield=
" UnitPrice "dataformatstring=" {0:c} "headertext=" unit Price "
htmlencode=" False "sortexpression=" UnitPrice "/>
</Columns>
</asp:GridView>
Next, we need to set the GridView to select rows, which will add a selection button for each row. To implement this feature, just select the Enable Selection checkbox on the GridView smart tag.
Figure 6: Allow the GridView row to be selected
Selecting the Enable selection item adds a command field to the Productsgrid GridView and sets the Showselectbutton property to true. So each row of the GridView has a selection button, as shown in Figure 6. By default, the selection button is presented as a link, but you can also use a button or Picture button instead to modify the CommandField ButtonType property.
<asp:gridview id= "Productsgrid" runat= "Server" autogeneratecolumns= "False" datakeynames= "ProductID
" Datasourceid= "Allproductsdatasource" enableviewstate= "False" >
<Columns>
<asp:commandfield showselectbutton= "True"/>
<asp:boundfield datafield= "ProductName" headertext= "Product" sortexpression= " ProductName "/> <asp:boundfield datafield= UnitPrice" dataformatstring= "
{0:c}" headertext= "Unit Price"
htmlencode= "False" sortexpression= "UnitPrice"/>
</Columns>
</asp:GridView>
Clicking the GridView Selection button causes a postback, and the Selectedrow property of the GridView is also updated. In addition to the Selectedrow attribute, the GridView also provides SelectedIndex, SelectedValue, and SelectedDataKey properties. The SelectedIndex property returns the index of the selected row, and the SelectedValue and SelectedDataKey properties return the value based on the DataKeyNames property of the GridView.
The DataKeyNames property lets each row associate one or more data fields, often used to uniquely identify the GridView row. The SelectedValue property returns the value of the first data field in the DataKeyNames of the selected row, SelectedDataKey returns the DataKey object of the selected row, which contains the values of all the specified data primary key fields for that row.
When you bind a data source to the GridView in Design view, DetailsView, FormView the DataKeyNames property is automatically set to the data field that is uniquely identified in the data source. Although this property is set automatically in the previous tutorial, the example can be run without having to specify the DataKeyNames property specifically. But for the GridView of the selectable rows in this tutorial, and for the new, update, delete tutorial that we're going to study in the GridView, The DataKeyNames property must be set appropriately. Ensure that the DataKeyNames property of the GridView is set to ProductID.
Browse through the work we've done in the browser. The GridView lists the name and unit price of all products and a selection button. Clicking the Select button triggers a postback. In the second step we'll see how to get a DetailsView to respond to the postback event and display the details of the selected product.
Figure 7: Each product line contains a select link button.
Highlight selected rows
The Productsgrid GridView has a SelectedRowStyle property that determines the appearance of the selected row. Reasonable use can show the currently selected row more clearly to improve the user experience. For this tutorial, we use a yellow background to highlight the selected row.
As with the previous tutorial, we should try to keep the appearance of beauty. Define a new CSS class in Styles.css and name it selectedrowstyle.
. SelectedRowStyle
{
background-color:yellow;
}
To apply this CSS class to all the SelectedRowStyle attributes of the GridView in this series of tutorials, edit the Gridview.skin skin file under the DataWebControls theme as follows:
<asp:gridview runat= "Server" cssclass= "Datawebcontrolstyle" >
<alternatingrowstyle cssclass= " AlternatingRowStyle "/>
<rowstyle cssclass=" RowStyle "/>
After modification, the selected line of the GridView is highlighted with a yellow background
Figure 8: Customizing the appearance of the selected row with the SelectedRowStyle property of the GridView
Step 2: Displays the selected product details in DetailsView
After completing the Productsgrid GridView, the rest is to add a DetailsView that displays the details of the selected product. Add a DetailsView control above the GridView and create a new data source named Productdetailsdatasource. Because we want this DetailsView to display the details of the selected product, configure Productdetailsdatasource to use the Getproductbyproductid (ProductID) method of the Productsbll class.
Figure 9: The Getproductbyproductid (ProductID) method that invokes the PRODUCTSBLL class
Let ProductID get the parameter value from the SelectedValue property of the GridView control. As we discussed earlier, the SelectedValue property of the GridView returns the first Data key value of the selected row. Therefore, the DataKeyNames property of the GridView must be set to ProductID so that the ProductID of the selected row can be returned through the SelectedValue property.
Figure 10: Associating the ProductID parameter to the SelectedValue property of the GridView.
After configuring Productdetailsdatasource ObjectDataSource and binding it to DetailsView, this tutorial is complete! No rows are selected on the first access, so the SelectedValue property of the GridView returns NULL. Because there are no products with ProductID value NULL, the Getproductbyproductid (ProductID) method does not return any records and DetailsView cannot be displayed (Figure 11). Clicking the GridView Selection button causes a postback and updates the DetailsView. The SelectedValue property of the GridView returns the ProductID of the selected row, Getproductbyproductid (ProductID) method returns information productsdatatable for a particular product. DetailsView displays these details (Figure 12).
Figure 11: Only the GridView is displayed on the first visit
Figure 12: Show details of the product after a row is selected
Summarize
In this and previous three tutorials, we've seen several techniques for displaying master/from reports. In this tutorial we studied the use of selectable rows of the GridView display master record and DetailsView selected master record display details. In the previous tutorial we saw how to display the master record in one page using dropdownlists and the detail record on another page.
The study of the master/from has ended in this article. From the beginning of the next chapter we will study the custom formatted GridView, DetailsView and FormView. We'll see how to customize these data-bound controls, show summary information in the GridView footer, and use templates to better control the layout.
I wish you a happy programming!