About

SubSonic is a .net open source project developed by Rob Conery and a core team of developers including Eric Kemp, Scott Watermasysk, Jon Galloway, Phil Haack, and Gavin Joyce. The current stable release is version 2.0.3. Nightly builds are available in our SVN respository.

Tags

Webcast: Using Paging

Paging isn't always the easiest thing to figure out - and sometimes not the most performant thing you can do to a we site. Here's a webcast to show you how to set it up both ways: the ASP way, and the SubSonic way.

The code for this web cast is down below...

The HTML:

<table>
    <tr>
        <td>
            <asp:GridView ID="GridView1" runat="server" />

        </td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="btnFirst" runat="server" Text="<< First" CommandArgument="First"  OnClick="pager_Click"/>

            <asp:Button ID="btnPrev" runat="server" Text="< Previous" CommandArgument="Prev"  OnClick="pager_Click"/>

            Page
            <asp:DropDownList ID="ddlPages" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlPages_SelectedIndexChanged">

            </asp:DropDownList> of <asp:Label ID="lblPageCount" runat="server"></asp:Label>

            <asp:Button ID="btnNext" runat="server" Text="Next >" CommandArgument="Next"  OnClick="pager_Click"/>

            <asp:Button ID="btnLast" runat="server" Text="Last >>" CommandArgument="Last"  OnClick="pager_Click"/>

        </td>
    </tr>
</table>

 

Code Behind

    const int PAGE_SIZE = 10;

    protected void Page_Load(object sender, EventArgs e) {
        if (!Page.IsPostBack) {
            LoadGrid(1);
            SetupPaging();
        }
    }

    void LoadGrid(int pageNumber) {
        Query q = new Query(Northwind.Tables.Product);
        q.PageSize = 10;
        q.PageIndex = pageNumber;
        GridView1.DataSource = q.ExecuteDataSet();
        GridView1.DataBind();

        ResolvePagerView(pageNumber);
    }

    void SetupPaging() {
        int pageCount = 0;
        int totalRecords = 0;
        //check to see if the totals have been set

        if (lblPageCount.Text == string.Empty) {
            totalRecords = new Query(Northwind.Tables.Product).GetCount(Northwind.Product.Columns.ProductID);
        }

        pageCount = totalRecords / PAGE_SIZE;
        if (totalRecords % PAGE_SIZE > 0)
            pageCount++;

        lblPageCount.Text = pageCount.ToString();

        //load up the list items
        for (int i = 1; i <= pageCount; i++) {
            ddlPages.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }

    }
    void ResolvePagerView(int currentPage) {
        int pageCount = 1;

        int.TryParse(lblPageCount.Text, out pageCount);

        int nextPage = currentPage + 1;
        int prevPage = currentPage - 1;

        btnPrev.Enabled = true;
        btnNext.Enabled = true;
        btnLast.Enabled = true;
        btnFirst.Enabled = true;

        if (currentPage == pageCount) {
            btnNext.Enabled = false;
            btnLast.Enabled = false;
        } else if (currentPage == 1) {
            btnPrev.Enabled = false;
            btnFirst.Enabled = false;
        }

    }
    protected void pager_Click(object sender, EventArgs e) {
        Button btn = (Button)sender;
        string pageCommand = btn.CommandArgument;
        int currentPage = ddlPages.SelectedIndex + 1;

        switch (pageCommand) {
            case "First":
                currentPage = 1;
                break;
            case "Prev":
                currentPage--;
                break;
            case "Next":
                currentPage++;
                break;
            case "Last":
                currentPage = int.Parse(lblPageCount.Text);
                break;
        }

        //reset the DropDown

        ddlPages.SelectedValue = currentPage.ToString();

        //reload the grid
        LoadGrid(currentPage);

    }
    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e) {
        //reload the grid
        LoadGrid(ddlPages.SelectedIndex + 1);

    }
#1 Edward on 3.21.2008 at 4:09 PM

I tried this with an Oracle Database and got the following error:

SQL Error: ORA-00911: invalid character

00911. 00000 - "invalid character"

*Cause: identifiers may not start with any ASCII character other than letters and numbers. $#_ are also allowed after the first character. Identifiers enclosed by doublequotes may contain any character other than a doublequote. Alternative quotes (q'#...#') cannot use spaces, tabs, or carriage returns as delimiters. For all other contexts, consult the SQL Language Reference Manual.

So I grabbed the query it generated, using q.GetSql, and the problem is the query uses underscores. Is there an option to stop it from using underscores? Or another way to fix this? Thanks.

#2 Rob Conery on 3.24.2008 at 4:35 AM

Can you post this to our forums please...

#3 Edward on 3.25.2008 at 5:32 PM

Done: forums.subsonicproject.com/.../2927.aspx

Subscribe