Live Chat Icon For mobile
Live Chat Icon

How to do Paging in DataList

Platform: ASP.NET| Category: DataList

<asp:datalist id='DataList1' runat='server'>
	<ItemTemplate>
		<%#DataBinder.Eval(Container.DataItem, 'ProductID').ToString()%>
		<%#DataBinder.Eval(Container.DataItem, 'ProductName').ToString()%>
	</ItemTemplate>
</asp:datalist>
<table width='50%' border='0'>
	<tr>
		<td><asp:LinkButton id='lnkPrevious' runat='server'><<</asp:LinkButton>
		<td><asp:LinkButton id='lnkNext' runat='server'>>></asp:LinkButton>
	</tr>
</table>

VB.NET


Dim intStart As Integer
Dim intpageSize As Integer
Dim cn As SqlConnection
Dim da As SqlDataAdapter
Dim ds As DataSet
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        	’Put user code to initialize the page here 
        	If Not Page.IsPostBack Then
            		ViewState('Start') = 0
            		bindList()
        	End If
End Sub

Sub bindList()
        	cn = New SqlConnection('server=localhost;uid=sa;pwd=;database=northwind')
        	da = New SqlDataAdapter('Select * from Products ', cn)
        	ds = New DataSet
        	intStart = ViewState('Start')
        	ViewState('pageSize') = 14
        	da.Fill(ds, intStart, ViewState('pageSize'), 'Table')
        	DataList1.DataSource = ds
        	DataList1.DataBind()
End Sub

Private Sub lnkPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkPrevious.Click
              intStart =  ViewState('Start')- ViewState('pageSize')
              ViewState('Start') = intStart
              If intStart <= 0 Then
            		ViewState('Start') = 0
              End If
              bindList()
End Sub

Private Sub lnkNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkNext.Click
        	Dim dlistcount As Integer = DataList1.Items.Count
        	intStart = ViewState('Start') + ViewState('pageSize')
        	ViewState('Start') = intStart
        	If dlistcount < ViewState('pageSize') Then
            		ViewState('Start') = ViewState('Start') - ViewState('pageSize')
        	End If
        	bindList()
End Sub

Private Sub lnkFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  	If intStart <= 0 Then
            		ViewState('Start') = 0
        	End If
        	bindList()
End Sub

C#


int intStart ;

SqlConnection cn ; 
SqlDataAdapter da ; 
DataSet ds ; 
private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
	if(!Page.IsPostBack )
	{
		ViewState['Start'] = 0;
		bindList();
	}
}

void bindList()
{
	cn = new SqlConnection('server=localhost;uid=sa;pwd=;database=northwind');
	da = new SqlDataAdapter('Select * from Products ', cn);
	ds = new DataSet();
	intStart = (int)ViewState['Start'];
	ViewState['pageSize'] = 14;
	da.Fill(ds, intStart,(int) ViewState['pageSize'], 'Table');
	DataList1.DataSource = ds;
	DataList1.DataBind();
}

private void lnkPrevious_Click(object sender, System.EventArgs e)
{
	intStart = (int) ViewState['Start'] -(int) ViewState['pageSize'];
	ViewState['Start'] = intStart;
	if (intStart <= 0 )
	{
		ViewState['Start'] = 0;
	}
	bindList();
}

private void lnkNext_Click(object sender, System.EventArgs e)
{
	int dlistcount = DataList1.Items.Count;
	intStart = (int) ViewState['Start']+(int) ViewState['pageSize'];
	ViewState['Start'] = intStart;
	if ( dlistcount < (int)ViewState['pageSize'] )
	{
		ViewState['Start'] = (int)ViewState['Start'] - (int)ViewState['pageSize'];
	}
	bindList();
}

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.