ARTICLE

Custom Paging in Datalist Control using VB.NET

Posted by Satyapriya Nayak Articles | ASP.NET using VB.NET August 17, 2011
In this article we will know how to do paging in datalist control.
 
Reader Level:

In this article we will know how to do paging in datalist control.

Program

Create a table employee

Create table employee (empid int primary key, empname varchar(50), empsal int, empadd varchar (50), empdes varchar (50) ).

table1.gif

Datalistpaging.aspx code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="datalistpaging.aspx.vb" Inherits="datalistpaging" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Datalist Paging</title>

    <style type="text/css">

    .wdt

    {

      width:10%;

    }

    </style>

</head>

<body>

    <form id="form1" runat="server">

    <div>

<a name="this"></a>

<asp:DataList ID="datalist_paging" Runat="server">

 <HeaderTemplate>

<table>

<tr>

<th><font color="red">Employee ID</font></th>

<th><font color="red">Employee Name</font></th>

<th><font color="red">Employee Salary</font></th>

<th><font color="red">Employee Address</font></th>

<th><font color="red">Employee Degisnation</font></th>

</tr>

</HeaderTemplate>

<ItemTemplate>

<tr>

<td><%#Eval("empid")%></td>

<td><%#Eval("empname")%></td>

<td><%#Eval("empsal")%></td>

<td><%#Eval("empadd")%></td>

<td><%#Eval("empdes")%></td>

</tr>

</ItemTemplate>

 <HeaderStyle BackColor="#FFCC66" />

<FooterTemplate>

</table>

</FooterTemplate>

</asp:DataList>

<table>

<tr>

<td>

<asp:label ID="lblshow" Runat="server" Font-Bold="False" />

</td>

<td class="wdt">

<a href="#this"

ID="Firstrecord"

onserverclick="First_record"

runat="server"><b><<</b></a>

</td>

<td class="wdt">

<a href="#this"

ID="Previousrecord"

onserverclick="Previous_record"

runat="server"><b><</b></a>

</td>

<td class="wdt">

<a href="#this"

ID="Nextrecord"

onserverclick="Next_record"

runat="server"><b>></b></a>

</td>

<td class="wdt">

<a href="#this"

ID="Lastrecord"

onserverclick="Last_record"

runat="server"><b>>></b></a>

</td>

</tr>

</table>

<asp:label ID="start_page" Visible="False" Runat="server" />

<asp:label ID="page_length" Visible="False" Runat="server" />

<asp:label ID="count_data" Visible="False" Runat="server" />

    </div>

    </form>

</body>

</html>

Datalistpaging.aspx.vb code

Imports System.Data

Imports System.Data.SqlClient

Partial Class datalistpaging

    Inherits System.Web.UI.Page

    Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()

    Dim con As New SqlConnection(strConnString)

    Dim str As String

    Dim com As SqlCommand

    Dim sqlda As SqlDataAdapter

    Dim ds As DataSet

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack() Then

            page_length.Text = "5"

            start_page.Text = "0"

            bindlist()

        End If

    End Sub

    Public Sub First_record(ByVal s As Object, ByVal e As EventArgs)

        start_page.Text = "0"

        bindlist()

    End Sub

    Public Sub Previous_record(ByVal s As Object, ByVal e As EventArgs)

        start_page.Text = CStr(CInt(start_page.Text) - CInt(page_length.Text))

        If CInt(start_page.Text) < 0 Then

            start_page.Text = "0"

        End If

        bindlist()

    End Sub

 

    Public Sub Next_record(ByVal s As Object, ByVal e As EventArgs)

        If CInt(start_page.Text) + 1 < CInt(count_data.Text) Then

            start_page.Text = CStr(CInt(start_page.Text) + CInt(page_length.Text))

        End If

        bindlist()

    End Sub

 

    Public Sub Last_record(ByVal s As Object, ByVal e As EventArgs)

        Dim i As Integer

 

        i = CInt(count_data.Text) Mod CInt(page_length.Text)

        If i > 0 Then

            start_page.Text = CStr(CInt(count_data.Text) - i)

        Else

            start_page.Text = CStr(CInt(count_data.Text) - CInt(page_length.Text))

        End If

        bindlist()

    End Sub

 

    Sub bindlist()

        con.Open()

        str = "select * from employee"

        sqlda = New SqlDataAdapter(str, con)

        ds = New DataSet()

 

        If Not Page.IsPostBack() Then

            sqlda.Fill(ds)

            count_data.Text = CStr(ds.Tables(0).Rows.Count)

        End If

        sqlda.Fill(ds, CInt(start_page.Text), CInt(page_length.Text), "employee")

        datalist_paging.DataSource = ds

        datalist_paging.DataMember = "employee"

        datalist_paging.DataBind()

        con.Close()

        navigate()

    End Sub

    Private Sub navigate()

        lblshow.ForeColor = Drawing.Color.Tomato

        lblshow.BackColor = Drawing.Color.Yellow

        lblshow.Text = "Total Records:<b>" & count_data.Text

        lblshow.Text += "</b> - Showing Page:<b> "

        lblshow.Text += CStr(CInt(CInt(start_page.Text) / CInt(page_length.Text) + 1))

        lblshow.Text += "</b> of <b>"

 

        If (CInt(count_data.Text) Mod CInt(page_length.Text)) > 0 Then

            lblshow.Text += CStr(CInt(CInt(count_data.Text) / CInt(page_length.Text) + 1))

        Else

            lblshow.Text += CStr(CInt(count_data.Text) / CInt(page_length.Text))

        End If

        lblshow.Text += "</b>"

    End Sub

End Class

Output

Thanks for reading

table2.gif

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor