Answered by:
Help me to add paging to DataList

Question
-
User426001450 posted
I'm having a hard time finding a good online article that explain how to add paging to DataList with Visual Basic code. Can anybody provide for me a good code example or a link. Please, Visual Basic only. It may be one of your examples or an online article. I have to repeat myself, Visual Basic only. Thanks to all.
Wednesday, December 13, 2017 12:57 AM
Answers
-
User426001450 posted
After many trials and tribulations I couldn’t make it to work. I tried hard but it didn’t work for me. My advice to all of you is to use a control that comes with automatic paging build in it. Instead of killing myself with DataList, I changed everything to ListView. This control comes with an automatic paging feature. Why should I kill myself if there is another ways to do it?
The one to blame here for all of this is Microsoft. I think that the company should homogenize all controls. These controls all display data in different ways but their purpose is the same. All of them should have the same features. Even though they are different, should be a standard between all of them. Things like formatting, paging, sorting and so on should be automatic features in all of them. For now, just use ListView instead of DataList. Thanks to all. <o:p></o:p>
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, December 23, 2017 5:35 PM
All replies
-
User991499041 posted
Hi Vstorpedo,
I'm having a hard time finding a good online article that explain how to add paging to DataList with Visual Basic code. Can anybody provide for me a good code example or a link. Please, Visual Basic only. It may be one of your examples or an online article. I have to repeat myself, Visual Basic only. Thanks to all.DataList control by default does not have paging (pagination) enabled hence we need to develop a custom pager control in order to implement paging in DataList control in ASP.Net.
Implement Paging in DataList control in ASP.Net
https://www.aspsnippets.com/Articles/Implement-Paging-in-DataList-control-in-ASPNet.aspx
Regards,
zxj
Wednesday, December 13, 2017 1:36 AM -
User-335504541 posted
<g class="gr_ gr_12 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="12" data-gr-id="12">Hi</g> <g class="gr_ gr_11 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="11" data-gr-id="11">vstorpedo</g>,
vstorpedo
I'm having a hard time finding a good online article that explain how to add paging to DataList with Visual Basic code. Can anybody provide for me a good code example or a link. Please, Visual Basic only. It may be one of your examples or an online article.I think you need to use buttons to page your <g class="gr_ gr_13 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="13" data-gr-id="13">datalist</g>.
You could refer to the following link for more information:
Here is the code:
<p> <asp:DataList ID="ProductsDefaultPaging" runat="server" Width="100%" DataKeyField="ProductID" RepeatColumns="2" DataSourceID="ProductsDefaultPagingDataSource" EnableViewState="False"> <ItemTemplate> <h4><asp:Label ID="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>'> </asp:Label></h4> Category: <asp:Label ID="CategoryNameLabel" runat="server" Text='<%# Eval("CategoryName") %>'> </asp:Label><br /> Supplier: <asp:Label ID="SupplierNameLabel" runat="server" Text='<%# Eval("SupplierName") %>'> </asp:Label><br /> <br /> <br /> </ItemTemplate> <ItemStyle Width="50%" /> </asp:DataList> <asp:ObjectDataSource ID="ProductsDefaultPagingDataSource" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetProductsAsPagedDataSource" TypeName="ProductsBLL"> <SelectParameters> <asp:QueryStringParameter DefaultValue="0" Name="pageIndex" QueryStringField="pageIndex" Type="Int32" /> <asp:QueryStringParameter DefaultValue="4" Name="pageSize" QueryStringField="pageSize" Type="Int32" /> </SelectParameters> </asp:ObjectDataSource> </p> <p style="text-align:center;"> <asp:Button runat="server" ID="FirstPage" Text="<< First" /> <asp:Button runat="server" ID="PrevPage" Text="< Prev" /> <asp:Button runat="server" ID="NextPage" Text="Next >" /> <asp:Button runat="server" ID="LastPage" Text="Last >>" /> </p> <p style="text-align:center;"> <asp:Label runat="server" ID="CurrentPageNumber"></asp:Label> </p>
In code behind:
Public Function GetProductsAsPagedDataSource(ByVal pageIndex As Integer, _ ByVal pageSize As Integer) As PagedDataSource ' Get ALL of the products Dim products As Northwind.ProductsDataTable = GetProducts() ' Limit the results through a PagedDataSource Dim pagedData As New PagedDataSource() pagedData.DataSource = products.Rows pagedData.AllowPaging = True pagedData.CurrentPageIndex = pageIndex pagedData.PageSize = pageSize Return pagedData End Function Protected Sub FirstPage_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FirstPage.Click ' Send the user to the first page RedirectUser(0) End Sub Protected Sub PrevPage_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PrevPage.Click ' Send the user to the previous page RedirectUser(PageIndex - 1) End Sub Protected Sub NextPage_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles NextPage.Click ' Send the user to the next page RedirectUser(PageIndex + 1) End Sub Protected Sub LastPage_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LastPage.Click ' Send the user to the last page RedirectUser(PageCount - 1) End Sub Private Sub RedirectUser(ByVal sendUserToPageIndex As Integer) ' Send the user to the requested page Response.Redirect(String.Format("Paging.aspx?pageIndex={0}&pageSize={1}", sendUserToPageIndex, PageSize)) End Sub Protected Sub ProductsDefaultPagingDataSource_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles ProductsDefaultPagingDataSource.Selected ' Reference the PagedDataSource bound to the DataList Dim pagedData As PagedDataSource = CType(e.ReturnValue, PagedDataSource) ' Remember the total number of records being paged through across postbacks TotalRowCount = pagedData.DataSourceCount ' Configure the paging interface based on the data in the PagedDataSource FirstPage.Enabled = Not pagedData.IsFirstPage PrevPage.Enabled = Not pagedData.IsFirstPage NextPage.Enabled = Not pagedData.IsLastPage LastPage.Enabled = Not pagedData.IsLastPage ' Display the current page being viewed... CurrentPageNumber.Text = String.Format("You are viewing page {0} of {1}...", PageIndex + 1, PageCount) End Sub #Region "Page-Level, Paging-Related properties" Private Property TotalRowCount() As Integer Get Dim o As Object = ViewState("TotalRowCount") If (o Is Nothing) Then Return -1 Else Return Convert.ToInt32(o) End If End Get Set(ByVal Value As Integer) ViewState("TotalRowCount") = Value End Set End Property Private ReadOnly Property PageCount() As Integer Get If TotalRowCount <= 0 OrElse PageSize <= 0 Then Return 1 Else Return ((TotalRowCount + PageSize) - 1) / PageSize End If End Get End Property Private ReadOnly Property PageIndex() As Integer Get If (Not String.IsNullOrEmpty(Request.QueryString("pageIndex"))) Then Return Convert.ToInt32(Request.QueryString("pageIndex")) Else Return 0 End If End Get End Property Private ReadOnly Property PageSize() As Integer Get If (Not String.IsNullOrEmpty(Request.QueryString("pageSize"))) Then Return Convert.ToInt32(Request.QueryString("pageSize")) Else Return 4 End If End Get End Property #End Region
Best Regards,
Billy
Wednesday, December 13, 2017 4:48 AM -
User426001450 posted
I'm trying to implement the code from this link: https://www.codeproject.com/Tips/457957/Pagination-with-DataList , but the problem is that I already have a DataList and a sqlDataSource on my page. So, I do not need a code behind like the following to feed the sqlDataAdapter.
Dim da As SqlDataAdapter = New SqlDataAdapter("select * from emp", "Data Source=.\SQLDB;Database=Test1;Integrated Security=True")
But instead I need my page datasource to populate the sqlDataAdapter in the code behind. How to do that?
For example, my sqlDataSource is call: sqlAllRecordsForPaintersDataSource, so is possible to do this or something similar in the code behind?
Dim da As SqlDataAdapter = New SqlDataAdapter("sqlAllRecordsForPaintersDataSource ")
Thanks for helping
Wednesday, December 13, 2017 6:58 AM -
User-335504541 posted
<g class="gr_ gr_17 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="17" data-gr-id="17">Hi</g> vstorpedo,
Please try the following code:
In code behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not IsPostBack Then CurrentPageIndex = 0 showData() End If End Sub Private pg As Integer = 0 Private Sub showData() Dim pgd As PagedDataSource = New PagedDataSource() pgd.DataSource = sqlAllRecordsForPaintersDataSource.Select(DataSourceSelectArguments.Empty()) pgd.CurrentPageIndex = CurrentPageIndex pgd.AllowPaging = True pgd.PageSize = 3 LinkButton2.Enabled = Not (pgd.IsLastPage) LinkButton3.Enabled = Not (pgd.IsFirstPage) DataList1.DataSource = pgd DataList1.DataBind() End Sub Public Property CurrentPageIndex As Integer Get If ViewState("pg") Is Nothing Then Return 0 Else Return Convert.ToInt16(ViewState("pg")) End Get Set(ByVal value As Integer) ViewState("pg") = value End Set End Property Protected Sub LinkButton2_Click(ByVal sender As Object, ByVal e As EventArgs) CurrentPageIndex += 1 showData() End Sub Protected Sub LinkButton3_Click(ByVal sender As Object, ByVal e As EventArgs) CurrentPageIndex -= 1 showData() End Sub
In <g class="gr_ gr_55 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="55" data-gr-id="55">aspx</g>:
<form id="form1" runat="server"> <div> <asp:DataList ID="DataList1" runat="server"> <HeaderTemplate> <table border="1"> </HeaderTemplate> <ItemTemplate> <tr> <td><%#Eval("LastName")%></td> <td><%#Eval("FirstName")%></td> <td><%#Eval("Title")%></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:DataList> <table class="style1"> <tr> <td class="style2"> <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">Next</asp:LinkButton> </td> <td> <asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton3_Click">Previous</asp:LinkButton> </td> </tr> </table> </div> <asp:SqlDataSource ID="sqlAllRecordsForPaintersDataSource" runat="server" ConnectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Northwind.MDF;Integrated Security=True;Connect Timeout=30" SelectCommand="select * from Employees"> </asp:SqlDataSource> </form>
And the result is:
Best Regards,
Billy
Wednesday, December 13, 2017 8:48 AM -
User426001450 posted
Thanks for your help.
There is a problem on this line:
pgd.DataSource = sqlAllRecordsForPaintersDataSource.Select(DataSourceSelectArguments.Empty())
Select is not a member of DataList.Wednesday, December 13, 2017 5:12 PM -
User-335504541 posted
<g class="gr_ gr_23 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="23" data-gr-id="23">Hi</g> vstorpedo,
Could you show me your code?
Best Regards,
Billy
Thursday, December 14, 2017 1:32 AM -
User426001450 posted
My code is exactly the same as the one that you pasted before, but only that line is giving problem. This is the line that is giving problem in the code behind again, and the message is : Select is not a member of DataList.
Private Sub showData() Dim pgd As PagedDataSource = New PagedDataSource() pgd.DataSource = sqlAllRecordsForPaintersDataSource.Select(DataSourceSelectArguments.Empty()) pgd.CurrentPageIndex = CurrentPageIndex pgd.AllowPaging = True pgd.PageSize = 3 LinkButton2.Enabled = Not (pgd.IsLastPage) LinkButton3.Enabled = Not (pgd.IsFirstPage) sqlAllRecordsForPainterDataSource = pgd
sqlAllRecordsForPainterDataSource.DataBind()
End SubThursday, December 14, 2017 2:11 AM -
User-335504541 posted
<g class="gr_ gr_19 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="19" data-gr-id="19">Hi</g> <g class="gr_ gr_20 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="20" data-gr-id="20">vstorpedo</g>,
sqlAllRecordsForPainterDataSource = pgd
sqlAllRecordsForPainterDataSource.DataBind()What's the sqlAllRecordsForPainterDataSource? Does it the <g class="gr_ gr_21 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="21" data-gr-id="21">datalist</g>? if so, I think it should be:
sqlAllRecordsForPainterDataSource.DataSource = pgd sqlAllRecordsForPainterDataSource.DataBind()
And does your asp:SqlDataSource same as I provide?
Could you also show me your page?
Best Regards,
Billy
Thursday, December 14, 2017 4:55 AM -
User426001450 posted
I can't make it to work. Still having a hard time after trying everything. On your first code I'm only having problem with this line:
Public Function GetProductsAsPagedDataSource(ByVal pageIndex As Integer, ByVal pageSize As Integer) As PagedDataSource ' Get ALL of the products Dim products As Northwind.ProductsDataTable = GetProducts() ' Limit the results through a PagedDataSource Dim pagedData As New PagedDataSource() pagedData.DataSource = products.Rows pagedData.AllowPaging = True pagedData.CurrentPageIndex = pageIndex pagedData.PageSize = pageSize Return pagedData End Function
This is from this link on one of your post:
: https://docs.microsoft.com/en-us/aspnet/web-forms/overview/data-access/paging-and-sorting-with-the-datalist-and-repeater/paging-report-data-in-a-datalist-or-repeater-control-vb
Before you use all the code that you provided on your post, you must declared GetProduct(). This code need additional work.
May you help here?
Thanks
Friday, December 22, 2017 6:48 PM -
User426001450 posted
After many trials and tribulations I couldn’t make it to work. I tried hard but it didn’t work for me. My advice to all of you is to use a control that comes with automatic paging build in it. Instead of killing myself with DataList, I changed everything to ListView. This control comes with an automatic paging feature. Why should I kill myself if there is another ways to do it?
The one to blame here for all of this is Microsoft. I think that the company should homogenize all controls. These controls all display data in different ways but their purpose is the same. All of them should have the same features. Even though they are different, should be a standard between all of them. Things like formatting, paging, sorting and so on should be automatic features in all of them. For now, just use ListView instead of DataList. Thanks to all. <o:p></o:p>
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, December 23, 2017 5:35 PM