1

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

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

在另一个页面上,我使用了一个数据列表,它显示了我创建的 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>
4

1 回答 1

1

我找到了答案,DetailsView 控件在绑定到数据源时的默认行为与下拉列表或 ListView 控件不同。DetailView 控件通常一次显示一个表中的一条记录。您可以“启用分页”,它允许设置“分页器设置”,以便超链接将引导您浏览表中的每条记录。希望我的发现能在未来对其他人有所帮助。

这是 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>
于 2013-01-28T17:37:30.890 回答