Click here to Skip to main content
15,997,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
pre lang="xml"><pre lang="C++">I am a newbie (with a little experience - mostly trial and error) who has followed a tutorial re a datalist pager. The tutrial expects its students to be more experienced than me. ie it expects students to know what goes at the top of the code behind page ( &quot;imports&quot; in VB / &quot;using&quot; in C# ) and it expects students to adjust Visual Studio property / events without any guidence in order for its code to work. I have managed adjustments in VWD user interface properties / events window which brought the pagers facial functions to life but it appears that I am missing the one vital ingredient for it to actually adjust the page size. My actual pager control ( see trial ftp page here - shifting.w02.winhost.com/WholesaleCSHARP_2.aspx ) seems to think its working.

Below I say what it actually does do :
It knows how many records are in the db field because it diplays the right amount of page numbers.and it adjusts the amount of page numbers according to the page size options in its dropdown list.
It highlights which page it believes it is on once a user chooses a page.
It knows which records it is supposed to be displaying - ie &quot;previous&quot; and &quot;next&quot; buttons react properly re clicking on last link or first link.

I think the problem is in the properties events section of VWD user interface or is another very basic mistake because I am as sure as I can be that my code is the same as the lessons which gets excellent response reviews from very many other people who have used it.
The lesson I followed is here - www.aspdotnetcodes.com/DataList_Dynamic_Paging_PagedDataSource.aspx - and its working example is here - www.aspdotnetcodes.com/Dynamic_Paging_DataList_Sample.aspxPlease can anyone help?</pre>

If its any help - I think the likely foundation of my mistake is basic - along the lines of this being the first time I have ever used C# instead of having the pageset up for VB - or my very limited knowledge re which events or properties to adjust from the default settings in my VWD 2008 express user interface to implement the pager. I adjusted a few events connected to the paging datalist and its associated controls which did made the controls on the pager spring to life.

The .aspx page code is below and below that is the .aspx.cs page

aspx code
:-


XML
<table style="width: 100%; height: 25px;" bgcolor="#FFCC99" border="2"
    cellpadding="10">
    <tr>
        <td rowspan="3">
            &nbsp;<asp:LinkButton ID="lnkbtnPrevious" runat="server"
                onclick="lnkbtnPrevious_Click">Previous</asp:LinkButton>
        </td>
        <td>
            <asp:DropDownList ID="ddlPageSize" runat="server" AutoPostBack="True"
                onselectedindexchanged="ddlPageSize_SelectedIndexChanged"
                ondatabound="Page_Load">
                <asp:ListItem>40</asp:ListItem>
                <asp:ListItem>50</asp:ListItem>
                <asp:ListItem>100</asp:ListItem>
            </asp:DropDownList>

        </td>
        <td >

            <asp:DataList ID="dlPaging" runat="server" OnItemCommand="dlPaging_ItemCommand"
                RepeatDirection="Horizontal" oneditcommand="dlPa"
                onitemdatabound="dlPaging_ItemDataBound" onupdatecommand="dlPaging_ItemCommand">
                <ItemTemplate>
                    <asp:LinkButton ID="lnkbtnPaging" runat="server" CommandArgument='<%# Eval("PageIndex") %>'
                    CommandName="lnkbtnPaging" Text='<%# Eval("PageText") %>'></asp:LinkButton>
                </ItemTemplate>
            </asp:DataList>
        </td>
        <td>
            <asp:LinkButton ID="lnkbtnNext" runat="server" onclick="lnkbtnNext_Click">Next</asp:LinkButton>
        </td>
    </tr>
</table>


.aspx.cs code

<pre lang="C++">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


public partial class WholesaleCSHARP_2 : System.Web.UI.Page
{
     PagedDataSource pds = new PagedDataSource();

     private void BindGrid()
     {
         string sql = "SELECT [ProductCode], [ImgUrl], [Featuring], [Title] FROM [TableSG1] ORDER BY [Featuring]DESC ";
         SqlDataAdapter da = new SqlDataAdapter(sql, "Data Source=tcp:s02.winhost.com;Initial Catalog=DB_5554_maindb1;User ID=DB_5554_maindb1_user;Password=bootsie23000;Integrated Security=False;");
         DataTable dt = new DataTable();
         da.Fill(dt);

         pds.DataSource = dt.DefaultView;
         pds.AllowPaging = true;
         pds.PageSize = Convert.ToInt16(ddlPageSize.SelectedValue);
         pds.CurrentPageIndex = CurrentPage;
         lnkbtnNext.Enabled = !pds.IsLastPage;
         lnkbtnPrevious.Enabled = !pds.IsFirstPage;

         DataList1.DataSource = dt;
         DataList1.DataBind();

         doPaging();
     }

     protected void Page_Load(object sender, EventArgs e)
     {
         if (!IsPostBack)
         {
             BindGrid();
         }
     }

     public int CurrentPage
     {
         get
         {
             if (this.ViewState["CurrentPage"] == null)
                 return 0;
             else
                 return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());
         }

         set
         {
             this.ViewState["CurrentPage"] = value;
         }
     }

     private void doPaging()
     {
         DataTable dt = new DataTable();
         dt.Columns.Add("PageIndex");
         dt.Columns.Add("PageText");
         for (int i = 0; i < pds.PageCount; i++)
         {
             DataRow dr = dt.NewRow();
             dr[0] = i;
             dr[1] = i + 1;
             dt.Rows.Add(dr);
         }

         dlPaging.DataSource = dt;
         dlPaging.DataBind();
     }

    protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("lnkbtnPaging"))
        {
            CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
            BindGrid();
        }
    }

    protected void lnkbtnPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        BindGrid();
    }

    protected void lnkbtnNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        BindGrid();
    }

    protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        CurrentPage = 0;
        BindGrid();
    }

    protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
        if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
        {
            lnkbtnPage.Enabled = false;
            lnkbtnPage.Font.Bold = true;
        }
    }
    protected void dlPa(object source, DataListCommandEventArgs e)
    {

    }
}



ANY HELP IS GRATELY APRECIATED :)
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900