views:

14

answers:

1

I need to connect two ObjectDataSource tags. First will have a query string parameter and the second has a parameter, which is one of the fields in the result set of the first one. The relationship is one-to-one, so I can't put second one in a grid and use control parameter

<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;
    }

This does not work because AdvertiserObjectDataSource_OnSelecting is called before OrderObjectDataSource_OnSelected.

Thank you for your help.

A: 

Just got it working, here is what I've done:

<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;
}
Mike