Working with Data six in asp.net 2.0: programming to set ObjectDataSource parameter values _ self-study process

Source: Internet
Author: User
Tags new set

Introduction

As we have seen in the tutorials in the previous section, there are many alternative ways to pass the value of the parameter to the Ojbectdatasource method. If the parameter value is hard-coded and comes from a Web control on the page, or another source that can be read by the data source parameter object, the value can be bound to an input parameter without having to write a line of code.

Sometimes, however, the parameter value comes from some source that is not yet computed in the built-in parameter object of the data source. If our site supports our considerations then we may want the parameters to be based on the currently logged-on user. Or we do some customization before the parameters are passed to the implied object of the ObjectDataSource.

Whenever the ObjectDataSource Seelect method is invoked, it triggers its selecting event first, then invokes the method of the ObjectDataSource suppressed object, then fires the ObjectDataSource ObjectDataSource Selected events (Figure A shows the order of these events). You can set or change the value of the parameter in the event delegate of the selecting event.

The selected and selecting events of Figure 1:objectdatasource are triggered before and after the method that invokes its suppressed object.

In this section we'll look at adding a method to accept an input parameter to the DAL and BLL layer, the parameter name is Month,int type, and the method returns a Employeesdatatable object that contains the employee for the specified month of the hire anniversary. Our example sets the encoding to be based on the current month, displaying a list of "hire anniversary employees this month".

Let's get started!
First Step: Add a method to the EmployeesTableAdapter

In our first example, we need to add a method to get the employees of those hire dates (hiredate) in a given month. In order to provide this functionality under our program architecture, we first need to create a method in the EmployeesTableAdapter mapped out through a particular SQL statement. To do this, first open the Northwind typed dataset and right-click on the EmployeesTableAdapter tab to select Add Query.

Figure 2: Adding a query to the EmployeesTableAdapter

Select the SQL statement that returns one or more rows and columns. When the specified SELECT statement window is reached, EmployeesTableAdapter has loaded the default SELECT statement. Simply, add a WHERE clause: where DATEPART (m, hiredate) = @Month. Where DatePart is a function in T-SQL that is used as part of the return date type; Here we use the DatePart function to return the month portion of the hire date (hiredate) column.

Figure 3: Returns only the HireDate column with a value less than or equal to the parameter @HiredBeforeDate

Finally, the default method names Fillby and Getdataby are changed to Fillbyhireddatemonth and Getemployeesbyhireddatemonth respectively.

Figure 4: Choosing a more appropriate method name than Fillby and Getdataby

Click Finish to end the wizard and return to the design interface of the dataset. At this point EmployeesTableAdapter will include a new set of methods to get the employees hired in the specified month.

Figure 5: The new method appears in the design interface of the dataset

Step Two: Add a method to the business Logic Layer Getemployeesbyhireddatemonth (month)

Because our program architecture uses a separate layer to handle business logic and data logic, we need to add a method to the BLL that calls the Dal method to get the employee employed in the specified month. Open the file EmployeesBLL.cs and add the following method:

[System.ComponentModel.DataObjectMethodAttribute (System.ComponentModel.DataObjectMethodType.Select, false)]

Public northwind.employeesdatatable getemployeesbyhireddatemonth (int month)

{

 return Adapter.getemployeesbyhireddatemonth (month);

}

Like other methods in this class, Getemployeesbyhireddatemonth (month) simply calls the DAL and returns the result.

Step three: Show employee of employment anniversary in this month

The last example shows how to show an employee who has an anniversary for hire in the month. First, add a GridView control to the page programmaticparams.aspx, the page in the folder basicreporting. Add a new ObjectDataSource control as its data source. The configuration ObjectDataSource uses the class EMPLOYEESBLL and specifies that the SelectMethod property is Getemployeesbyhireddatemonth (month).

Figure 6: Using the EMPLOYEESBLL class

Figure 7: Selecting the Getemployeesbyhireddatemonth (month) method

The last screen requires that we provide the parameter source for the month parameter. Now that we set the encoding to this value, let the parameter source keep its default option None and click "Finish".

Figure 8: Having the parameter source set to None

This creates a parameter object that does not specify a parameter value in the SelectParameters collection 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 ObjectDataSource selecting event. To achieve this, double-click on the ObjectDataSource in the Design view. Another way is to select ObjectDataSource to click on the Yellow Lightning Flash icon in the Properties window, and then double-click in the Selecting column or enter a name for the event delegate you want to use.

Figure 9: Clicking the lightning Bolt icon in the Properties window lists all events for Web controls

Both approaches can add an event delegate to the ObjectDataSource selecting event in the page's code-behind class. In this event delegate, we can use E. Inputparameters[parametername] reads the value of the parameter, where the parametername value is <asp:Parameter> The value of the attribute name in the tag (InputParameters can also be accessed by index, in E. Inputparameters[index]). In order to set the month parameter to the current month, you need to add the following code to the selecting event delegate:

protected void Objectdatasource1_selecting (object sender, ObjectDataSourceSelectingEventArgs e)

{

 E. inputparameters["month"] = DateTime.Now.Month;

}

When the page is accessed through a browser, we can see that only one employee is employed in the current month (March): Laura Callahan, who has been employed since March 1994.

Figure 10: Employee of the Year in the month of employment is shown

Summarize

While it is possible to declare ObjectDataSource parameter values in a specific way without writing code, it is also easy to set parameter values programmatically. All we need to do is add an event delegate to the ObjectDataSource selecting event, which fires before invoking the implied object, and manually setting one or more parameter values through the InputParameters collection.

This section concludes the basic chapter. In the next section, we begin the master data chapter, which we will look at to allow visitors to filter data and master data processing techniques.

I wish you a happy programming!

Introduction of the author

Scott Mitchell, with six asp/asp. NET book, is the founder of 4GuysFromRolla.com, has been applying Microsoft Web technology since 1998. Scott is an independent technical consultant, trainer, writer, recently completed a new book to be published by Sams Press, proficient in asp.net 2.0 within 24 hours. His contact email is mitchell@4guysfromrolla.com, or he can contact him through his blog Http://ScottOnWriting.NET.

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.