This article describes how to obtain data from the BLL layer without writing any code and bind the data.
I. Introduction
After the application architecture and website page layout are completed, you will begin to learn the common data display function. In the previous articles, we have learned how to get data from the Dal or BLL and bind the data to the Web Control of the page by coding. The syntax is as follows: bind the datasource property of the control and call the databind () method. This is usually the practice of ASP. net1.x, which can be continued in ASP. net2.0. However, ASP. net2.0 provides a set of new controls-data source controls. With these controls, you can easily bind the data obtained from BLL to the page without writing any code.
ASP. net2.0 is embedded with five data source controls: sqldatasource, accessdatasource, objectdatasource, xmldatasource, and sitemapdatasource. If necessary, you can also customize the data source control. Based on our application architecture, we will use objectdatasource to access classes in BLL.
Objectdatasource acts as the proxy for accessing objects. Configure objectdatasource by defining the underlying object, and match the select, insert, update, and delete methods of the object with the objectdatasource methods one by one. Then, you can bind the objectdatasource control to the data control, such as: gridview, detailsview, radiobuttonlist, and dropdownlist. During the lifecycle of the page, the Data Control binds data by triggering the select method of objectdatasource. If the data control supports insertion, update, and deletion, the insert, update, and delete methods of objectdatasource are called respectively. Then, objectdatasource calls the corresponding method of the underlying object, as shown in:
Objectdatasource can be inserted, updated, or deleted by triggering different methods. This article will focus on displaying data. Subsequent articles will introduce how to use objectdatasource and data control to modify data.
Step 1: Add and configure the objectdatasource Control
Open the simpledisplay. ASPX page in the basicreporting folder and switch to the design view. Drag an objectdatasource control from the toolbox to the blank area. The control is dimmed because there is no configuration. You can access data by calling specific methods of a specific object. The returned value of objectdatasource can be displayed through a data control.
Note: You can also add a data control to the page and then display the data by selecting <new data source> from the drop-down list.
Click Configure data source of objectdatasource to specify the underlying object corresponding to objectdatasource and map the methods one by one.
After you open the data source Configuration Wizard, first, determine the object associated with the objectdatasource. If "show only data components" is selected, only the classes modified by the dataobject attribute are listed in the drop-down list. In the current environment, only tableadapters in a strongly typed dataset and classes in BLL created above are included. You can also choose not to view all objects.
Select the productbll class.
Next, select the method to be triggered by objectdatasource. The default method is to obtain data. We can see getproductbyproductid, getproducts, getproductsbycategoryid and getproductsbysupplierid. Select the getproducts method and click Finish.
Manually configure objectdatasource
The wizard provides a quick way to specify objects and associate them. Of course, you can also configure objectdatasource through its properties (you can directly use the Properties window, or you can use the identification language ). You only need to specify the typename attribute of objectdatasource to the relevant class and specify the selectmethod attribute to trigger the corresponding method to obtain data.
<Asp: objectdatasource id = "objectperformance1" runat = "server"
Selectmethod = "getproducts" typename = "productsbll">
</ASP: objectdatasource
You may think that using the Wizard is much faster than configuring objectdatasource manually, because the wizard only lists the classes used for creation. If you want. the classes in the. NET Framework are bound to the objectdatasource. For example, a class bound to the member permission can access the user's account information or the class that represents the path to access the file system information, you need to manually configure the attributes of objectdatasource.
Step 2: Add a Data Control and bind it to objectdatasource
After the objectdatasource is added and configured, add a Data Control to accept the data returned by the objectdatasource method. Any data control can be bound to objectdatasource. The following shows the data bound to objectdatasource: gridview, detailsview, and formview.
Bind objectdatasource to girdview
Add a gridview on the page (simpledisplay. aspx) and select the one created in step 1 as the data source. This will automatically create a binding column for each attribute returned by the Select method of objectdatasource.
You can customize, sort, or remove fields in the gridview binding column.
Modify the automatically generated binding columns in girdview, and remove: productid, supplierid, categoryid, quantityperunit, unitsinstock, unitsonorder, and recordlevel. You only need to click the Red Cross. Use the up and down arrow buttons to adjust categoryname and suppliername to unitprice, set the htmlencode attribute of the price column to false, and dataformatestring to "{0: c} "to display it in currency format, set the horizontal alignment of price to right and discontinued alignment to center (set in itemstyle/horizontalalign attribute ).
<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false"
Datakeynames = "productid" performanceid = "objectperformance1"
Enableviewstate = "false">
<Columns>
<Asp: boundfield datafield = "productname"
Headertext = "product" sortexpression = "productname"/>
<Asp: boundfield datafield = "categoryname"
Headertext = "category" readonly = "true"
Sortexpression = "categoryname"/>
<Asp: boundfield datafield = "suppliername"
Headertext = "supplier" readonly = "true"
Sortexpression = "suppliername"/>
<Asp: boundfield datafield = "unitprice"
Dataformatstring = "{0: c}" headertext = "price"
Htmlencode = "false" sortexpression = "unitprice">
<Itemstyle horizontalalign = "right"/>
</ASP: boundfield>
<Asp: checkboxfield datafield = "discontinued"
Headertext = "discontinued" sortexpression = "discontinued">
<Itemstyle horizontalalign = "center"/>
</ASP: checkboxfield>
</Columns>
</ASP: gridview>
Set unified appearance
In this series of courses, we try not to use any style settings at the control level, but use an external Cascading Style Sheet for definition, if necessary. The styles.css file contains CSS classes such as datawebcontrolstyle, headerstyle, rowstyle, and alternatingrowstyle to modify the appearance of the Data Control. You can set the cssclass attribute of the gridview to datawebcontrolstyle, or set the cssclass attribute of its headerstyle, rowstyle, and alternatingrowstyle respectively.
A better management method is to use a topic for these data controls. A topic is a set of control-level property settings, images, and CSS classes. These sets are applied to the website page to provide a provided appearance.
The theme we created does not contain any images or CSS files, but contains two skins. A skin is a file that defines the default properties of Web controls. Specifically, we define a skin file for the gridview and detailsview to indicate cssclass-related attributes.
Right-click the project name, click Add new item, select the skin file template, and name it gridview. Skin.
The skin file will be saved as a topic and placed in the app_themes folder. Because we do not have such a folder, when we add the first skin file, vs automatically creates one for us. You only need to click "yes.
In this way, a new topic is created.
Name the gridview topic as datawebcontrols, and enter the following identification code in the gridview. Skin file:
<Asp: gridview runat = "server" cssclass = "datawebcontrolstyle">
<Alternatingrowstyle cssclass = "alternatingrowstyle"/>
<Rowstyle cssclass = "rowstyle"/>
<Headerstyle cssclass = "headerstyle"/>
</ASP: gridview>
In this way, you can use the datawebcontrols topic to specify the cssclass for any default properties of the gridview on any page. Add another skin named detailsview. Skin. Also in the datawebcontrols topic.
<Asp: detailsview runat = "server" cssclass = "datawebcontrolstyle">
<Alternatingrowstyle cssclass = "alternatingrowstyle"/>
<Rowstyle cssclass = "rowstyle"/>
<Fieldheaderstyle cssclass = "headerstyle"/>
</ASP: detailsview>
After the topic is defined, the topic is applied to the page. A topic is referenced Based on pages, or applied to all pages of the site. Configure the <system. Web> section of Web. config to apply the topic to all pages on the website:
<Pages stylesheettheme = "datawebcontrols"/>
This is all about theme. Attributes set by stylesheettheme in a topic cannot be overwritten by attributes set at the control level. To demonstrate that the topic setting takes precedence over the control-level setting, use the theme attribute instead of stylesheettheme. The only disadvantage is that theme cannot be set in the design view of vs through the theme attribute.
Display a record at a time in detailsview
The gridview control displays all records in the bound data source. This is time-consuming. If you only want to display one record at a time. However, detailsview provides this function, which can display two columns in the form of <Table>, and each row shows an attribute or column bound to the control. You can think that detailsview is to display only one row of records in the gridview flipped 90 degrees.
Add a detailsview control to the page and bind it to the objectdatasource. Like the gridview, bind each attribute returned by the Select method of the objectdatasource to the Bind Column of the detailsview. The only difference is that the binding columns of detailsview are horizontally arranged, rather than vertical (gridview ).
Like the gridview, you can also customize the binding column of the detailsview to display specific data. Displays the custom binding columns and apply style configurations, which are very similar to the gridview.
You may find that detailsview only displays the first record in the data source. We can set the detailsview page, so that you can preview all records in the data source at a time. You can set it by setting the intelligent tag of detailsview.
We will explain in detail the paging details in future courses.
A more flexible layout for displaying a record at a time
The display method of the detailsview may be a bit rough. We may need a more flexible way to display data. For example, in addition to simply displaying the product name, price, category, provider, and discount information, we may want to display the product name and price in the <H4> style, it may also make the category and provider Information appear in a small font under the product name and price. Because sometimes we may not be very concerned about this information.
You can use formview to implement the above flexible display function. However, compared with columns used by the gridview and detailsview, formview uses a template. In the template, You can freely combine Web controls, static HTML, and even write data binding code. If you are familiar with the repeater in ASP. net1.x, you can think of formview as a repeater that only displays one record.
Add a formview to simpledisplay. aspx. By default, it is displayed in gray, telling us to set at least the itemtemplate of the control.
You can use the Smart tag of formview to directly bind the control to the data source. This will automatically create an itemtemplate for you. (If the insertmethod and updatemethod of the data source are set, create insertitemtemplate and edititemtemplate in formview .) In this example, we bind data to formview and manually set itemtemplate. First, set the datasourceid attribute of formview to the ID of objectdatasource -- objectdatasource1. Next, create an itemtemplate to display the product name and price in the form of <H4>; provide the product category and provider in a small font.
<Asp: formview id = "formview1" runat = "server"
Performanceid = "objectperformance1" enableviewstate = "false">
<Itemtemplate>
<H4> <% # eval ("productname") %>
(<% # Eval ("unitprice", "{0: c}") %>) </H4>
Category: <% # eval ("categoryname") %>;
Supplier: <% # eval ("suppliername") %>
</Itemtemplate>
</ASP: formview>
<% # Eval (prppertyname) %> is the syntax used for binding. The eval method is used to return the formview control to the specific attributes of the current object.
Like detailsview, formview only displays the first record in objectdatasource. You can also facilitate all records by page.
Ii. Summary
The objectdatasource control provided by ASP. net2.0 allows you to retrieve data and display data from the objects defined in BLL without writing any line of code. Use objectdatasource to trigger a specific method and return results. These results can be bound to a data control. In this article, we learned how to bind bobjectdatasource to the gridview, detailsview, and formview.
So far, we have learned how to trigger a method without parameters through objectdatasource. However, if we need to trigger a method that requires parameter input, such as getproductsbycategoryid (categoryid) in productbll, what should we do? Next, we will introduce how to configure objectdatasource to trigger the acceptance of one or more input parameters.
Happy programming !!!