详细信息视图控件仅显示 ASP.NET Web SIte 的一条记录

本文关键字:SIte Web 一条 记录 NET ASP 视图 控件 显示 详细信息 | 更新日期: 2023-09-27 17:56:10

我确信有一个简单的答案,但是为什么在我的页面中使用绑定到访问数据源的详细信息视图控件时,为什么只显示表的第一条记录?

当我将下拉列表绑定到同一数据源时,下拉列表将显示表中的所有记录。

在另一页上,我使用了一个数据列表,它显示了我创建的 select 语句的所有记录。

是否需要 for 循环将每条记录加载到详细信息视图控件中?

感谢您的任何建议,并为菜鸟问题感到抱歉。

编辑:这也是我所引用的页面的代码

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"            Inherits="_Default" %>
<!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 runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <br />
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            DataSourceID="AccessDataSource2" DataTextField="Name" 
            DataValueField="ProductID">
        </asp:DropDownList>
        <asp:AccessDataSource ID="AccessDataSource2" runat="server" 
            DataFile="~/App_Data/Halloween.mdb" 
            SelectCommand="SELECT [ProductID], [Name] FROM [Products]">
        </asp:AccessDataSource>
        <br />
        <br />
        <br />
        <asp:DetailsView ID="DetailsView1" runat="server" 
            DataSourceID="AccessDataSource1" Height="50px" Width="125px" 
            DataKeyNames="ProductID">
            <Fields>
                <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" 
                    SortExpression="ProductID" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" 
                    SortExpression="CategoryID" />
            </Fields>
        </asp:DetailsView>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
            DataFile="~/App_Data/Halloween.mdb" 
            SelectCommand="SELECT [ProductID], [Name], [CategoryID] FROM [Products] ORDER BY [CategoryID]">
        </asp:AccessDataSource>
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
    </div>
    </form>
</body>
</html>

详细信息视图控件仅显示 ASP.NET Web SIte 的一条记录

我已经找到了答案,默认情况下,DetailsView 控件在绑定到数据源时的行为与下拉列表或列表视图控件的行为不同。详细信息视图控件通常一次显示表中的一条记录。您可以"启用分页",它允许设置"寻呼机设置",以便超链接将导航您浏览表中的每条记录。希望我的发现将来能帮助其他人。

下面是 DetailsView 控件的代码,以及它与上述表示法的不同之处。

<asp:DetailsView ID="DetailsView1" runat="server" 
            DataSourceID="AccessDataSource1" Height="50px" Width="125px" 
            DataKeyNames="ProductID" AllowPaging="True" AutoGenerateRows="False">
            <PagerSettings Mode="NextPreviousFirstLast" />
            <Fields>
                <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" 
                    SortExpression="ProductID" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" 
                    SortExpression="CategoryID" />
            </Fields>
        </asp:DetailsView>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
            DataFile="~/App_Data/Halloween.mdb" 
            SelectCommand="SELECT [ProductID], [Name], [CategoryID] FROM [Products] ORDER BY [CategoryID]">
        </asp:AccessDataSource>