选择gridview中的行并在detailsview中显示

时间:2012-12-12 14:20:06

标签: c# asp.net gridview detailsview

我有一个带有选择链接的gridview。据说,当我点击选择链接时,应该出现DataKeyNames的特定细节。

但是,对于我的情况,每当我点击“选择”时,只有一条记录出来并且它没有改变并与所选的DataKeyNames的数据相匹配..我已经阅读了网上的许多例子,我做的完全一样但它无法正常工作..

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:WholesaleConnectionString %>" 
    SelectCommand="SELECT [poNum], [retailerID] FROM [PO]"></asp:SqlDataSource>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="poNum" DataSourceID="SqlDataSource1" >
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="poNum" HeaderText="poNum" InsertVisible="False" 
            ReadOnly="True" SortExpression="poNum" />
        <asp:BoundField DataField="retailerID" HeaderText="retailerID" 
            SortExpression="retailerID" />
    </Columns>
</asp:GridView>


<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:WholesaleConnectionString %>" 
    SelectCommand="SELECT [poNum], [quantity], [unitPrice], [totalAmt], [grandTotal] FROM [PO]">

    <SelectParameters>
    <asp:ControlParameter Name="poNum" ControlID="GridView1" PropertyName="SelectedValue" Type="Int32" />
    </SelectParameters>
   </asp:SqlDataSource>

<asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="SqlDataSource2" 
    Height="50px" Width="125px">
</asp:DetailsView>

1 个答案:

答案 0 :(得分:1)

DetailsView控件也经常用于主 - 细节场景,其中主控件的选定记录确定要在详细视图中显示的记录。以下ASP.NET程序显示如何使用GridView和DetailsView控件从数据库显示主 - 详细信息数据。在这里,我们将主数据用作销售数据,将详细信息用作商店数据。当用户选择一行销售数据时,它会在DetailsView中显示相应的商店详细信息。

例如:

<强> Default.aspx的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br /><b><u>Sales Details</u></b><br /><br />
        <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
        DataKeyNames="stor_id" AutoGenerateSelectButton="true"  AllowPaging="True" pagesize ="5"  />
        <br /><b><u>Store Details</u></b><br /><br />
        <asp:DetailsView id="DetailsView1" DataSourceID="SqlDataSource2"
        DataKeyNames="stor_id" AllowPaging ="true" Runat="server" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:SQLDbConnection %>"
        SelectCommand="select * from sales" />
        <asp:SqlDataSource ID="SqlDataSource2" runat="server"
        ConnectionString="<%$ ConnectionStrings:SQLDbConnection %>"
        SelectCommand="select * from stores WHERE stor_id=@stor_id" >
        <SelectParameters>
           <asp:ControlParameter Name="stor_id" ControlID="GridView1" />
        </SelectParameters>
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>