paging on a datalist with custom object(Arraylist) as datasource

i'm reading directory contents and populating the names of the files in an objectCollection that inherits from the arraylistclass and using it as a datasource for a datalist. How can I enable paging for this datalist. There is a lot of examples using the dataadapter, but i'm not connecting to a db. please advice
vinny45Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

davidlars99Commented:
I think for you to better understand paging without database would be to look at something that looks like a little shopping cart example, you can also use arrays for "Data Items" or you can even generate columns by "For.. Next" loop and put them into "DataSet" like so...  :)

----------------
example.aspx
----------------

<%@ Import Namespace="System.Data" %>
<%@ Page Language="VB" Debug="True" %>
<HTML>
<HEAD>
<Script Language="vb" Runat="Server">

Dim ds As DataSet, _
      dt As DataTable, _
      dc As DataColumn, _
      dr As DataRow, _
      PKColumn(1) As DataColumn
      
Private Sub Page_Load(s As Object, e As EventArgs)
      ds=New DataSet()
      dt=New DataTable("Cart")
      
      dc=New DataColumn("Item ID", GetType(Integer))
      dc.AutoIncrement=True
      dc.AutoIncrementSeed=1
      dc.AutoIncrementStep=1
      dc.Unique=True
      dt.Columns.Add(dc)
      PKColumn(0)=dc
      dt.PrimaryKey=PKColumn
      
      dc=New DataColumn("Item Name", GetType(String))
      dt.Columns.Add(dc)
      
      dc=New DataColumn("Quantity", GetType(Integer))
      dt.Columns.Add(dc)
      
      dc=New DataColumn("Price", GetType(Decimal))
      dt.Columns.Add(dc)
      
      dr=dt.NewRow()
      dr("Item Name")="Data Item 1"
      dr("Quantity")=1
      dr("Price")=19.99
      dt.Rows.Add(dr)
      
      dr=dt.NewRow()
      dr("Item Name")="Data Item 2"
      dr("Quantity")=1
      dr("Price")=49.99
      dt.Rows.Add(dr)
      
      dr=dt.NewRow()
      dr("Item Name")="Data Item 3"
      dr("Quantity")=1
      dr("Price")=69.99
      dt.Rows.Add(dr)
      
      dr=dt.NewRow()      
      dr("Item Name")="Data Item 4"
      dr("Quantity")=1
      dr("Price")=19.99
      dt.Rows.Add(dr)
      
      dr=dt.NewRow()
      dr("Item Name")="Data Item 5"
      dr("Quantity")=1
      dr("Price")=49.99
      dt.Rows.Add(dr)
      
      dr=dt.NewRow()
      dr("Item Name")="Data Item 6"
      dr("Quantity")=1
      dr("Price")=69.99
      dt.Rows.Add(dr)      
      
      dr=dt.NewRow()      
      dr("Item Name")="Data Item 7"
      dr("Quantity")=1
      dr("Price")=19.99
      dt.Rows.Add(dr)
      
      dr=dt.NewRow()
      dr("Item Name")="Data Item 8"
      dr("Quantity")=1
      dr("Price")=49.99
      dt.Rows.Add(dr)
      
      dr=dt.NewRow()
      dr("Item Name")="Data Item 0"
      dr("Quantity")=1
      dr("Price")=69.99
      dt.Rows.Add(dr)            
      
      ds.Tables.Add(dt)
      
      cart.DataSource=ds.Tables("cart").DefaultView
      cart.DataBind()
      
      Dim total As Decimal
      total=dt.Compute("Sum(Price)", Nothing)
      lblmsg.Text="Order Total: " & total.ToString()
End Sub

Private Sub BindData()
      cart.DataSource=ds.Tables("cart").DefaultView
      cart.DataBind()
End Sub

Private Sub dg_Paging(o As Object, e As DataGridPageChangedEventArgs)
      cart.CurrentPageIndex=e.NewPageIndex
      BindData()
End Sub

</Script>
<TITLE></TITLE>
</HEAD>
<BODY>

<form runat="server">
      <asp:datagrid id="cart" runat="server" autogeneratecolumns="true"
            allowpaging="true"
            pagesize="2"
            pagerstyle-mode="numericpages"
            onpageindexchanged="dg_Paging"
      />
      <br>
      <asp:label id="lblmsg" runat="server" />
</form>

</BODY>
</HTML>
0
davidlars99Commented:
try to change above datagrid with this

<asp:datagrid id="cart" runat="server" autogeneratecolumns="true"
      allowpaging="true"
      pagesize="2"
      pagerstyle-nextpagetext="Next &gt;"
      pagerstyle-prevpagetext="&lt; Prev"
      onpageindexchanged="dg_Paging"
/>


this will present paging with "Next... Prev..." link buttons
0
vinny45Author Commented:
good examples but i need one for a datalist
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

davidlars99Commented:
like this..?


<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>
<%@ Page Language="C#" Debug="True" %>
<html>
<script language="C#" runat="server">

string [] arr;

void Page_Load( Object sender , EventArgs e){
      arr=Directory.GetDirectories("C:\\Windows");
      if(!IsPostBack){
            Session["i"]=0;
            BuildGrid();
      }
}

public void BuildGrid(){      
      DataTable dt=new DataTable("Windows");
      DataColumn dc=new DataColumn("Item", Type.GetType("System.String"));
      dt.Columns.Add(dc);
      DataRow dr;
      int i;
            
      for (i=(int)Session["i"];i<((int)Session["i"]+(int.Parse(PageSize.Value)));i++){
            dr=dt.NewRow();
            dr["Item"]=arr[i];
            dt.Rows.Add(dr);
      }
    MyDataList.DataSource = dt;
    MyDataList.DataBind();

    TotalSize.Value=(arr.Length-2).ToString();
    lblmsg.Text="(Total Count: "+(arr.Length-2).ToString()+")";
  }

public void Page_DataList(object sender, EventArgs e){
    if(((LinkButton)sender).ID=="Prev"){
                  CurrentPage.Value=(int.Parse(CurrentPage.Value)-1).ToString();
                  Session["i"]=(int)Session["i"]-(int.Parse(PageSize.Value));
                  
    }
    else if(((LinkButton)sender).ID=="Next"){
                  CurrentPage.Value=(int.Parse(CurrentPage.Value)+1).ToString();
                  Session["i"]=(int)Session["i"]+(int.Parse(PageSize.Value));
    }
    BuildGrid();
}

 
</script>
<body>
<h3>Directory Listing <asp:label id="lblmsg" runat="server" /></h3>
<form runat="server">
      <asp:DataList id="MyDataList" RepeatColumns="1" RepeatDirection="Horizontal" runat="server">
            <ItemTemplate>
                  <%# DataBinder.Eval(Container.DataItem, "Item") %>
            </ItemTemplate>
</asp:DataList>

      <input type="hidden" id="PageSize" value="10" runat="server">
      <input type="hidden" id="CurrentPage" value="1" runat="server">
      <input type="hidden" id="TotalSize" runat="server">
      <br>
      <asp:LinkButton id="Prev" Text="<< Previous" OnClick="Page_DataList" runat="server" />
      &nbsp;
      <asp:LinkButton id="Next" Text="Next >>" OnClick="Page_DataList" runat="server" />
</form>
</body>
</html>
0
davidlars99Commented:
just makes sure you use correct directory path here

arr=Directory.GetDirectories("C:\\Windows");
0
davidlars99Commented:
I didn't pay attention at "Page_Datalist" sub you have to change it to this:



public void Page_DataList(object sender, EventArgs e){
    if(((LinkButton)sender).ID=="Prev"){
                  CurrentPage.Value=(int.Parse(CurrentPage.Value)-1).ToString();
                  if(((int)Session["i"]-(int.Parse(PageSize.Value)))<=0)
                        Session["i"]=0;
                  else
                        Session["i"]=(int)Session["i"]-(int.Parse(PageSize.Value));                               
    }
    else if(((LinkButton)sender).ID=="Next"){
                  CurrentPage.Value=(int.Parse(CurrentPage.Value)+1).ToString();
                  if(((int)Session["i"]+(int.Parse(PageSize.Value)))>=int.Parse(PageSize.Value))
                        Session["i"]=arr.Length-int.Parse(PageSize.Value);
                  else
                        Session["i"]=(int)Session["i"]+(int.Parse(PageSize.Value));
    }
    BuildGrid();
}
0
vinny45Author Commented:
this is what i ended up doing other solutions are very similar and might be better than mine.  there is a PagedDataSource (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolspageddatasourceclasstopic.asp)  object that u fill with a datasource and you can set its propeties and bind it to a datalist: well heres the code

private void bind()
            {
                   //my custom object
                       PhotoGallery gallery = new PhotoGallery();
                       
                        //instantiate the pagedatasource obj
                  PagedDataSource objPds = new PagedDataSource();

                        //set the PhotoGallery  parameters to retrieve the info
                  gallery.Level = this.level;
                  gallery.PicturePath = this.picturePath;
                  gallery.Folder = this.folder;

                        //get the directory listing
                  objPds.DataSource = gallery.Fill(); //returns collection that inherits from the arraylist

                        //set the pagedatasource properites
                  objPds.AllowPaging  = true;
                  objPds.PageSize = 9;
                  objPds.CurrentPageIndex = CurrentPage;

                  lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() + " of " 
                        + objPds.PageCount.ToString();

                  // Disable Prev or Next buttons if necessary
                  cmdPrev.Enabled = !objPds.IsFirstPage;
                  cmdNext.Enabled = !objPds.IsLastPage;
 
                  //bind the pagedatasource to the datalist
                  PortfolioDatalist.DataSource = objPds;
                  PortfolioDatalist.DataBind();

            }


            private void cmdPrev_Click(object sender, System.EventArgs e)
            {
                  // Set viewstate variable to the previous page
                  CurrentPage -= 1;

                  // Reload control
                  bind();
            }

            private void cmdNext_Click(object sender, System.EventArgs e)
            {
                  // Set viewstate variable to the next page
                  CurrentPage += 1;

                  // Reload control
                  bind();
            }

public int CurrentPage
            {
                  get
                  {
                        // look for current page in ViewState
                        object o = this.ViewState["_CurrentPage"];
                        if (o == null)
                              return 0;   // default to showing the first page
                        else
                              return (int) o;
                  }

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

0
davidlars99Commented:
glad you've found something, but that seems a bit confusing, here is really cool one:



<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>
<%@ Page Language="C#" Debug="True" %>
<html>
<script language="C#" runat="server">

void Page_Load( Object sender , EventArgs e){      
      if(!IsPostBack){
            string [] arr=Directory.GetDirectories("C:\\");
            Session["folders"]=arr;
            Session["i"]=0;
            Session["j"]=(int.Parse(PageSize.Value));
            BuildGrid();
            
      }
}
public void BuildGrid(){      
      DataTable dt=new DataTable("Windows");
      DataColumn dc=new DataColumn("Item", Type.GetType("System.String"));
      dt.Columns.Add(dc);
      DataRow dr;
      int i;
            
      for (i=((int)Session["i"]);i<((int)Session["j"]);i++){
            dr=dt.NewRow();
            dr["Item"]=((string[])Session["folders"])[i];
            dt.Rows.Add(dr);
      }
    MyDataList.DataSource = dt;
    MyDataList.DataBind();
    TotalSize.Value=(((string[])Session["folders"]).Length).ToString();
    lblmsg.Text="(Total Count: "+(((string[])Session["folders"]).Length).ToString()+")";
}
public void Page_DataList(object sender, EventArgs e){
    if(((Button)sender).ID=="Prev"){
                  CurrentPage.Value=(int.Parse(CurrentPage.Value)-1).ToString();
                  if(((int)Session["i"]-(int.Parse(PageSize.Value)))<0){
                        Session["i"]=0;
                        Session["j"]=(int.Parse(PageSize.Value));
                        Prev.Enabled=false;
                        Next.Enabled=true;
                  }else{
                        Session["i"]=(int)Session["i"]-(int.Parse(PageSize.Value));
                        Session["j"]=(int)Session["i"]+(int.Parse(PageSize.Value));
                        Prev.Enabled=true;
                        Next.Enabled=true;
                  }                              
    }
    else if(((Button)sender).ID=="Next"){
                  CurrentPage.Value=(int.Parse(CurrentPage.Value)+1).ToString();
                  if(((int)Session["j"]+(int.Parse(PageSize.Value))) > (((string[])Session["folders"]).Length)){
                        Session["i"]=(int)Session["j"];
                        Session["j"]=(((string[])Session["folders"]).Length);
                        Next.Enabled=false;
                  }else{
                        Session["i"]=(int)Session["j"];
                        Session["j"]=(int)Session["i"]+(int.Parse(PageSize.Value));
                        Next.Enabled=true;
                        Prev.Enabled=true;
                  }
    }
    BuildGrid();
}
</script>
<body>
<h3>Directory Listing <asp:label id="lblmsg" runat="server" /></h3>
<form runat="server">
      <asp:DataList id="MyDataList" RepeatColumns="1" RepeatDirection="Horizontal" runat="server">
            <ItemTemplate>
                  <%# DataBinder.Eval(Container.DataItem, "Item") %>
            </ItemTemplate>
</asp:DataList>
      <input type="hidden" id="PageSize" value="5" runat="server">
      <input type="hidden" id="CurrentPage" value="1" runat="server">
      <input type="hidden" id="TotalSize" runat="server">
      <br>
      <asp:Button id="Prev" Text="<< Prev" OnClick="Page_DataList" runat="server" />
      &nbsp;
      <asp:Button id="Next" Text="Next >>" OnClick="Page_DataList" runat="server" />
</form>
</body>
</html>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.