开发者

How to link GridView controls across <asp:Content> placeholders?

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="cphLeft开发者_开发技巧Side" 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:

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

That's just psuedo, but hopefully clear enough.


Try this code

<SelectParameters>
    <asp:ControlParameter Name="orderId" ControlID="cphLeftSide$grdOrders" PropertyName="SelectedDataKey.Value" />
</SelectParameters>

That's Because your problem is in the refer parameter can't find the "grdorders" so you can use $ to make the parameter look in the other content place holder on the same page


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜