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.
When your Order gridview is selected (whatever the event is called — onSelected or something), do this in your code-behind:
That’s just psuedo, but hopefully clear enough.