stand on the shoulders of giants

【ASP.NET Step by Step】之六 Programmatically Setting the ObjectDataSource's Parameter Values

1. 有时我们不通过控件获取ObjectDataSource所需要的参数,这时我们用这种方法:

时序图
The ObjectDataSource's Selected and Selecting Events Fire Before and After Its Underlying Object's Method is Invoked

The ObjectDataSource's Selected and Selecting Events Fire Before and After Its Underlying Object's Method is Invoked
因此,我们以在Selecting事件的事件委托中对参数的值进行设置或更改。
2. 步骤
     第一步: 添加一个Query方法到 EmployeesTableAdapter, FillByHiredDateMonth / GetEmployeesByHiredDateMonth
               SELECT   * FROM         Employees
               WHERE    DATEPART(m, HireDate)=@Month 
                DATEPART is a T-SQL function that returns a particular date portion of a datetime type 这里我们用它来返回HireDate column中的月份
     第二步: 在业务逻辑层添加方法 GetEmployeesByHiredDateMonth(month
     第三部:配置数据源

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}"
    SelectMethod="GetEmployeesByHiredDateMonth" TypeName="EmployeesBLL">
    <SelectParameters>
        <asp:Parameter Name="month" Type="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>

在这个事件委托里,我们可以通过使用e.InputParameters[parameterName]读取参数的值,其中parameterName的值是<asp:Parameter>标签里的属性Name的

//返回雇用周年纪念日在本月份的员工

protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
    e.InputParameters["month"] = DateTime.Now.Month; 
    //DateTime.Now.Month 读取当前月份,对ObjectDataSource来说是程序式的参数
}

posted @ 2008-12-01 17:50  DylanWind  阅读(206)  评论(0)    收藏  举报