How to link GridView controls to <asp: Content> tabs?
I am learning ASP.NET and I have a master-detail script using two GridViews in ASP.NET 3.5 and C #. GridView grdOrders lists the set of orders, and GridView grdOrderDetails lists the set of order details for the order. I have a main page that defines two areas of content using 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 put the grdOrders GridView in the cphLeftSide content placeholder and the GridView grdOrderDetails in the cphRightSide placeholder with this code:
<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 rights, I have 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 application / Test2. Could not find grdOrders control in ControlParameter 'orderId'. Description: An unhandled exception occurred during the execution of the current web request. Look at the stack trace for error information and it came from the code. Exception Details: System.InvalidOperationException: Could not find grdOrders control in ControlParameter 'orderId'.
How to connect two data sources? So when I select an order, it starts the usp_GetOrderDetails stored procedure and binds to the grdOrderDetails GridView? The code works fine if I put both GridViews and DataSources in the same
<asp:Content> tag.
Try this code
<SelectParameters> <asp:ControlParameter Name="orderId" ControlID="cphLeftSide$grdOrders" PropertyName="SelectedDataKey.Value" /> </SelectParameters> This is because your problem in the refer parameter cannot find "grdorders", so you can use $ to force the parameter to look in another content holder on the same page
The first solution that comes to mind is to do this in code. Within Page.Init try:
grdOrders.DataSource = sdsOrderDetails Similarly, you will most likely have to list your collection of selection options and explicitly link the control.
Update:. You can even try to omit the <asp:content> control identifier attribute, Visual Studio may complain, but this is a good chance that they will not behave like name containers as a result.