ASP. NET 2.0 data Tutorial: Use of the SelectMethod attribute

Source: Internet
Author: User

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.

 
 
  1. < asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}" 
  2.  
  3.     SelectMethod="GetEmployeesByHiredDateMonth" TypeName="EmployeesBLL">  
  4.  
  5.     < SelectParameters>  
  6.  
  7.         < asp:Parameter Name="month" Type="Int32" />  
  8.  
  9.     < /SelectParameters>  
  10.  
  11. < /asp:ObjectDataSource>  
  12.  

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:

 
 
  1. protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)  
  2.  
  3. {  
  4.  
  5.     e.InputParameters["month"] = DateTime.Now.Month;  
  6.  
  7. }  
  8.  

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.

  1. ASP. NET 2.0 data Tutorial: insert, update, and delete data
  2. ASP. NET 2.0 data Tutorial: how to add parameterization to the data access layer
  3. ASP. NET 2.0 data Tutorial: create a data access layer
  4. ASP. NET 2.0 data Tutorial: create a Web project and configure database connection
  5. ASP. NET 2.0 data Tutorial: add field-level verification to DataRow

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.