The last example shows how to display the employee anniversary for this month. First, add a GridView control to the page ProgrammaticParams. aspx, which is in the BasicReporting folder. Add a new ObjectDataSource control as its data source. Configure ObjectDataSource to use the class EmployeesBLL and specify the SelectMethod attribute as GetEmployeesByHiredDateMonth (month ).
Figure 6: Use the EmployeesBLL class
SelectMethod: select the GetEmployeesByHiredDateMonth (month) method.
The last screen requires us to provide the parameter source for the month parameter. Since we set this value for encoding, let the parameter source maintain its default option None and click "finish ".
Figure 8: Set the parameter source to None
This will create a Parameter object with no specified Parameter value in the SelectParameters set of ObjectDataSource.
- < asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}"
-
- SelectMethod="GetEmployeesByHiredDateMonth" TypeName="EmployeesBLL">
-
- < SelectParameters>
-
- < asp:Parameter Name="month" Type="Int32" />
-
- < /SelectParameters>
-
- < /asp:ObjectDataSource>
-
To encode this parameter value, we need to add an event Delegate to the Selecting event of ObjectDataSource. To achieve this, double-click ObjectDataSource in the design view. Another way is to select ObjectDataSource and click the yellow lightning icon in the Properties window. Then, double-click the selected column or enter the name of the event Delegate you want to use.
Figure 9: Click the lightning icon in the Properties window to list all events of the Web control.
Both methods can add an event Delegate to the Selecting event of ObjectDataSource in the Code hiding class of the page. In this event Delegate, we can use e. inputParameters [parameterName] reads the value of the Parameter. The value of parameterName is the value of the attribute Name in the <asp: Parameter> label. InputParameters can also be accessed according to the index. e. inputParameters [index]). To set the month parameter to the current month, add the following code to the Selecting event Delegate:
- protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
-
- {
-
- e.InputParameters["month"] = DateTime.Now.Month;
-
- }
-
When you access this page through a browser, we can see that only one employee was hired in the current month, January 1, March): Laura Callahan, who has been hired since January 1, March 1994.
Figure 10: employee anniversary is displayed in this month
In this way, the SelectMethod attribute is used to display objects within the specified range.
- ASP. NET 2.0 data Tutorial: insert, update, and delete data
- ASP. NET 2.0 data Tutorial: how to add parameterization to the data access layer
- ASP. NET 2.0 data Tutorial: create a data access layer
- ASP. NET 2.0 data Tutorial: create a Web project and configure database connection
- ASP. NET 2.0 data Tutorial: add field-level verification to DataRow