views:

51

answers:

2

I am learning ASP.NET and I have a master-detail scenario using two GridViews in ASP.NET 3.5 and C#. GridView grdOrders lists a set of orders and the GridView grdOrderDetails lists a set of order details for an order. I have a master page which defines two content areas with this code:

<div class="leftColumn">
    <asp:ContentPlaceHolder id="cphLeftSide" runat="server" />
</div>
<div class="rightColumn">
    <asp:ContentPlaceHolder ID="cphRightSide" runat="server" />
</div>

On my orders.aspx content page I have put the grdOrders GridView in the cphLeftSide content placeholder and GridView grdOrderDetails in the cphRightSide placeholder with code like this:

<asp:Content ID="leftContent" ContentPlaceHolderID="cphLeftSide" runat="server">
    <h2>Orders</h2>
    <asp:GridView ID="grdOrders" DataSourceID="sdsOrders"...></asp:GridView>
<asp:SqlDataSource ID="sdsOrders" runat="server" ConnectionString="<%$ ConnectionStrings:csOrders %>" 
    SelectCommand="usp_GetOrders" SelectCommandType="StoredProcedure">></asp:SqlDataSource>
</asp:Content>

In the rightside I have the orderDetails content with code like this:

<asp:Content ID="rightContent" ContentPlaceHolderID="cphRightSide" runat="server">
    <h2>Order details</h2>
    <asp:GridView ID="grdOrderDetails" DataSourceID="sdsOrderDetails"...></asp:GridView>
<asp:SqlDataSource ID="sdsOrderDetails  runat="server" 
    ConnectionString="<%$ ConnectionStrings:csOrders %>" 
    SelectCommand="usp_GetOrderDetails" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:ControlParameter Name="orderId" ControlID="grdOrders" PropertyName="SelectedDataKey.Value" />
    </SelectParameters>
</asp:Content>

When I run my code I get this error:

Server Error in '/Test2' Application. Could not find control 'grdOrders' in ControlParameter 'orderId'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: Could not find control 'grdOrders' in ControlParameter 'orderId'.

How do I connect the two data sources? So that when I select an order, it runs the usp_GetOrderDetails stored procedure and binds to the grdOrderDetails GridView? The code runs fine if I put both GridViews and DataSources in the same

<asp:Content> 

tag.

A: 

The first solution that springs to mind is to do it in code behind. Within Page.Init, try:

grdOrders.DataSource = sdsOrderDetails

Likewise, you will most likely have to enumerate your select parameter collection and associate the control explicitly.

Update: You could even try ommitting the ID attribute of the <asp:content> controls, Visual Studio might complain but theres a good chance that they won't behave as naming containers as a consequence.

Rabid
+1  A: 

When your Order gridview is selected (whatever the event is called -- onSelected or something), do this in your code-behind:

  • sdsOrderDetails.parameters("orderID").defaultvalue = grdOrders.selectedvalue

That's just psuedo, but hopefully clear enough.

dave