连接两个ObjectDataSource

时间:2010-10-26 15:00:57

标签: asp.net objectdatasource

我需要连接两个ObjectDataSource标记。 第一个将具有查询字符串参数,第二个具有参数,该参数是第一个结果集中的字段之一。这种关系是一对一的,所以我不能把第二个放在网格中并使用控制参数

<asp:ObjectDataSource ID="OrderObjectDataSource" runat="server" 
    SelectMethod="GetById" 
    TypeName=" MyProject.Business.Repositories.OrderRepository"
    OnSelected="OrderObjectDataSource_OnSelected">
    <SelectParameters>
        <asp:QueryStringParameter DefaultValue="86" Name="orderId" 
            QueryStringField="ioid" Type="Int32" />
    </SelectParameters>
    </asp:ObjectDataSource>
<asp:ObjectDataSource runat="server" ID="AdvertiserObjectDataSource"
     TypeName="MyProject.Business.Repositories.AdvertiserRepository"
     SelectMethod="GetAdvertiserById"
     OnSelecting="AdvertiserObjectDataSource_OnSelecting">
    <SelectParameters>
        <asp:Parameter Name="advertiserId" Type="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>
</asp:Content>


    private Order _order;
    protected void OrderObjectDataSource_OnSelected(object source, ObjectDataSourceStatusEventArgs e)
    {
        if (e.ReturnValue != null)
        {
            _order = (Order) e.ReturnValue;
            AdvertiserObjectDataSource.SelectParameters["advertiserId"].DefaultValue =
                        _order.AdvertiserId.ToString();
            AdvertiserObjectDataSource.Select();
        }
    }

    protected void AdvertiserObjectDataSource_OnSelecting(object source, ObjectDataSourceSelectingEventArgs e)
    {
        if (_order != null)
            e.InputParameters["advertiserId"] = _order.AdvertiserId;
    }

这不起作用,因为在OrderObjectDataSource_OnSelected之前调用了AdvertiserObjectDataSource_OnSelecting。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

刚刚开始工作,这就是我所做的:

<asp:ObjectDataSource ID="OrderObjectDataSource" runat="server"/>
<asp:ObjectDataSource runat="server" ID="AdvertiserObjectDataSource" />

private Order _order;

protected void Page_Load(object sender, EventArgs e)
{
     OrderObjectDataSource.TypeName = "MyProject.Business.Repositories.OrderRepository";
     OrderObjectDataSource.SelectMethod = "GetById";
     OrderObjectDataSource.SelectParameters.Clear();
     OrderObjectDataSource.SelectParameters.Add(new QueryStringParameter("orderId", 
                                                    DbType.Int32, Proposal_Id_QS_Key));
     OrderObjectDataSource.Selected += OrderObjectDataSource_OnSelected;    
     AdvertiserObjectDataSource.TypeName = "MyProject.Business.Repositories.AdvertiserRepository";
     AdvertiserObjectDataSource.SelectMethod = "GetAdvertiserById";
     AdvertiserObjectDataSource.SelectParameters.Clear();
     AdvertiserObjectDataSource.SelectParameters.Add(new Parameter("advertiserId", DbType.Int32));
     AdvertiserObjectDataSource.Selecting += AdvertiserObjectDataSource_OnSelecting;

     OrderObjectDataSource.Select();
}


protected void OrderObjectDataSource_OnSelected(object source, ObjectDataSourceStatusEventArgs e)
{
     if (e.ReturnValue != null)
     {
         _order = (Order) e.ReturnValue;
         AdvertiserObjectDataSource.SelectParameters["advertiserId"].DefaultValue =
         _order.AdvertiserId.ToString();
         AdvertiserObjectDataSource.Select();
      }
}


protected void AdvertiserObjectDataSource_OnSelecting(object source, ObjectDataSourceSelectingEventArgs e)
{
     if (_order != null)
          e.InputParameters["advertiserId"] = _order.AdvertiserId;
}