solomon_13000 14 Junior Poster in Training

I attempted to set up pagination for the datalist control. However when I click next link the next image doesn't display. Then when I click previous the first image is displayed. Did I miss something in the code?

<table border="0" width="800" bgColor="#ffffff" height="644">
        <tbody>
        <tr bgColor="#ffffff">
          <td height="173" colspan="2" bgcolor="#CCCCCC"><table width="796">
            <tr>
              <td width="303"><img src="images/logo.jpg" width="302" height="195" align="left"></td>
              <td width="497"><center><img src="images/word_bnr.jpg" width="433" height="162"></center></td>
            </tr>
          </table></td>
        </tr>
        <tr bgColor="#ffffff">
          <td height="20" colspan="2" bgcolor="#009900" align="left">
		  <span class="ex3">-:::You are currently @ the <b>GALLERY </b> section:::-</span>		  </td>
        </tr>
        <tr>
          <td rowspan="2" width="583">
		  <center>
		  <table width="569">
            <tr>
              <td height="20" width="561">&nbsp;</td>
            </tr>
            <tr>
              <td align="left">
                  <asp:DataList ID="DataList1" runat="server" RepeatColumns="3" 
                      RepeatLayout="Table">
                  <ItemTemplate>
                     <br />
                     <table cellspacing="0">
                     <tr>   
                        <td><asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("FilePath")%>' /></td>
                     </tr>
                     </table>   
                     <br />
                  </ItemTemplate>
                  </asp:DataList>
                </td>
            </tr>
            <tr>
              <td align="left">
              <asp:Panel ID="pnlPager" runat="server">
                  <asp:LinkButton ID="lnkPrev" runat="server" CommandName="Previous" Text="Previous" OnClick="Pager_Click">
                  </asp:LinkButton>
                   &nbsp;
                  <asp:LinkButton ID="lnkNext" runat="server" CommandName="Next" Text="Next" OnClick="Pager_Click">
                  </asp:LinkButton>
              </asp:Panel>
              </td>
            </tr>
            <tr>
              <td class="style1"></td>
            </tr>
            <tr>
              <td align="left">&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td align="left">&nbsp;</td>
            </tr>
            <tr>
              <td align="left">&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
            </tr>
          </table>
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 MySql.Data.MySqlClient;

public partial class Gallery : System.Web.UI.Page
{
    private int CurrentPage = 1;
    private int ItemsPerPage = 1;
    private int pageNo = 0;

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

    public void fillDataList()
    {
        string MyConString = "SERVER=localhost;DATABASE=mysql;UID=root;PASSWORD=abc123";
        MySqlConnection connection = new MySqlConnection(MyConString);
        MySqlDataAdapter adapter = new MySqlDataAdapter("select * from gallery", connection);
        DataSet ds = new DataSet();
        adapter.Fill(ds, "gallery");
        PagedDataSource pd = new PagedDataSource();
        pd.DataSource = ds.Tables[0].DefaultView;
        pd.AllowPaging = true;
        pd.PageSize = ItemsPerPage;
        pd.CurrentPageIndex = pageNo;
        DataList1.DataSource = pd;
        DataList1.DataBind();
    }

    public void Pager_Click(Object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton)sender;
        
        switch (lb.CommandName)
        {
            case "Previous":
                pageNo = this.CurrentPage - 1;
                break;

            case "Next":
                pageNo = this.CurrentPage + 1;
                break;
        }
        
        fillDataList();
    }
    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

Your help is kindly appreciated.

Thank You.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.