4

我需要一些关于如何从访问数据库中分页数据的帮助。我使用asp:ListViewasp:DataPager喜欢我发现的一些示例,但是分页使用 javascript 并且我希望分页将是友好的。

我怎样才能把我的一些代码?

[编辑] 这是代码:

<asp:ListView ID="tblProjects" runat="server" OnPagePropertiesChanging="tblProjects_PagePropertiesChanging">
<LayoutTemplate>
    <ul class="ulProducts">
        <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
    </ul>
</LayoutTemplate>
<ItemTemplate>
    <li>
        <div class="divProduct">
            <a rel='pics' href='GalleryEditor/pictures/<%# Eval("BigImageName") %>'>
                <img src='GalleryEditor/pictures/<%# Eval("SmallImagesName") %>'
                    alt='<%# Eval("ImageDetail") %>' title='<%# Eval("ImageDetail") %>' />
            </a>
        </div>
    </li>
</ItemTemplate>
<EmptyDataTemplate>
    no data
</EmptyDataTemplate>

这是后面的代码:

protected void tblProjects_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    {
        //set current page startindex, max rows and rebind to false

        imagesPager.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);

        string page = Request.QueryString["p"];
        if (!(FormValidator.IsNumber(page)))
            page = "8800";

        buildProducts(page);

    }

    void buildTitleAndDescription(string page)
    {
        // set page title
        OleDbConnection conn0 = new OleDbConnection(@"Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" + Server.MapPath("GalleryEditor\\App_Data\\projects.mdb"));
        OleDbCommand comm0 = new OleDbCommand("select ProjectName from Project where PlaceID=" + page, conn0);
        OleDbDataReader reader0;

        conn0.Open();
        reader0 = comm0.ExecuteReader();
        reader0.Read();

        Page.Header.Title = reader0["ProjectName"].ToString();

        reader0.Close();
        conn0.Close();

        //if (IsPostBack)
        //{
        //    string Script = "<script type='text/javascript'>goToEnd();\n</script>";
        //    Page.ClientScript.RegisterStartupScript(this.GetType(), "onload", Script);
        //}

    }

    void buildProducts(string page)
    {

        // set page content
        OleDbConnection conn = new OleDbConnection(@"Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" + Server.MapPath("GalleryEditor\\App_Data\\sb.mdb"));
        OleDbCommand comm = new OleDbCommand("select SmallImagesName, BigImageName, ImageDetail from Images where PlaceID=" + page, conn);

        conn.Open();

        DataSet ds = new DataSet();
        OleDbDataAdapter da = new OleDbDataAdapter(comm);
        da.Fill(ds);

        tblProjects.DataSource = ds;
        tblProjects.DataBind();

    }

我不介意将代码更改为更好的代码,如果我从中学习,我将不胜感激。我发现的所有示例都是针对 SQL 的,并且我的数据需要处于可访问状态。

4

1 回答 1

0

这是一个 asp listview 示例,您必须按如下方式定义查询字符串。

    <asp:ListView ID="ListView1" runat="server" DataKeyNames="bd_book_code" 
    DataSourceID="SqlDataSource1" EnableModelValidation="True">
    <AlternatingItemTemplate>
        <tr style="">
            <td>
                <asp:Label ID="bd_book_codeLabel" runat="server" 
                    Text='<%# Eval("bd_book_code") %>' />
            </td>
            <td>
                <asp:Label ID="bd_isbnLabel" runat="server" Text='<%# Eval("bd_isbn") %>' />
            </td>
            <td>
                <asp:Label ID="bd_titleLabel" runat="server" Text='<%# Eval("bd_title") %>' />
            </td>
        </tr>
    </AlternatingItemTemplate>
    <EditItemTemplate>
        <tr style="">
            <td>
                <asp:Button ID="UpdateButton" runat="server" CommandName="Update" 
                    Text="Update" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                    Text="Cancel" />
            </td>
            <td>
                <asp:Label ID="bd_book_codeLabel1" runat="server" 
                    Text='<%# Eval("bd_book_code") %>' />
            </td>
            <td>
                <asp:TextBox ID="bd_isbnTextBox" runat="server" Text='<%# Bind("bd_isbn") %>' />
            </td>
            <td>
                <asp:TextBox ID="bd_titleTextBox" runat="server" 
                    Text='<%# Bind("bd_title") %>' />
            </td>
        </tr>
    </EditItemTemplate>
    <EmptyDataTemplate>
        <table runat="server" style="">
            <tr>
                <td>
                    No data was returned.</td>
            </tr>
        </table>
    </EmptyDataTemplate>
    <InsertItemTemplate>
        <tr style="">
            <td>
                <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
                    Text="Insert" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                    Text="Clear" />
            </td>
            <td>
                <asp:TextBox ID="bd_book_codeTextBox" runat="server" 
                    Text='<%# Bind("bd_book_code") %>' />
            </td>
            <td>
                <asp:TextBox ID="bd_isbnTextBox" runat="server" Text='<%# Bind("bd_isbn") %>' />
            </td>
            <td>
                <asp:TextBox ID="bd_titleTextBox" runat="server" 
                    Text='<%# Bind("bd_title") %>' />
            </td>
        </tr>
    </InsertItemTemplate>
    <ItemTemplate>
        <tr style="">
            <td>
                <asp:Label ID="bd_book_codeLabel" runat="server" 
                    Text='<%# Eval("bd_book_code") %>' />
            </td>
            <td>
                <asp:Label ID="bd_isbnLabel" runat="server" Text='<%# Eval("bd_isbn") %>' />
            </td>
            <td>
                <asp:Label ID="bd_titleLabel" runat="server" Text='<%# Eval("bd_title") %>' />
            </td>
        </tr>
    </ItemTemplate>
    <LayoutTemplate>
        <table runat="server">
            <tr runat="server">
                <td runat="server">
                    <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                        <tr runat="server" style="">
                            <th runat="server">
                                bd_book_code</th>
                            <th runat="server">
                                bd_isbn</th>
                            <th runat="server">
                                bd_title</th>
                        </tr>
                        <tr ID="itemPlaceholder" runat="server">
                        </tr>
                    </table>
                </td>
            </tr>
            <tr runat="server">
                <td runat="server" style="">
                    <asp:DataPager ID="DataPager1" runat="server" QueryStringField="pid">
                        <Fields>
                            <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="True" 
                                ShowNextPageButton="False" ShowPreviousPageButton="False" />
                            <asp:NumericPagerField />
                            <asp:NextPreviousPagerField ButtonType="Link" ShowLastPageButton="True" 
                                ShowNextPageButton="False" ShowPreviousPageButton="False" />
                        </Fields>
                    </asp:DataPager>
                </td>
            </tr>
        </table>
    </LayoutTemplate>
    <SelectedItemTemplate>
        <tr style="">
            <td>
                <asp:Label ID="bd_book_codeLabel" runat="server" 
                    Text='<%# Eval("bd_book_code") %>' />
            </td>
            <td>
                <asp:Label ID="bd_isbnLabel" runat="server" Text='<%# Eval("bd_isbn") %>' />
            </td>
            <td>
                <asp:Label ID="bd_titleLabel" runat="server" Text='<%# Eval("bd_title") %>' />
            </td>
        </tr>
    </SelectedItemTemplate>
</asp:ListView>

QueryStringField="pid" 会变魔术。

于 2013-02-05T12:20:15.787 回答