Click here to Skip to main content
Licence 
First Posted 6 Nov 2007
Views 10,367
Bookmarked 9 times

Datalist paging

By | 19 Nov 2007 | Article
Article defines how we can implement paging in Datalist

Introduction

This article will give you knowledge how can implement Pagin in Datalist. Generally we are using pagin in Gridview and sometimes we need to use pagin in datalist. As in datalist we can define column horizontally or vertically as well. So its really amaging control. Now when we need to allow pagin we have to specify some code written by us, as Datalist does not have any event for pagin. So have a fun with code.

Using the code

Use below code in your codebehind file.

//
// Code for codebehind file
//
private void Page_Load(object sender, System.EventArgs e)
{
    if(!IsPostBack)
    {
    TextBox1.Text="0";
    TextBox2.Text="1";
    Build_List();
    }
}

private void Build_List()
{
    try
    {
        SqlConnection con;
        SqlDataAdapter adp;
        con = new SqlConnection("Connection String");
        con.Open();

        adp = new SqlDataAdapter("Command String", con); 
        DataSet ods = new DataSet();
        int index = int.Parse(TextBox1.Text);
        adp.Fill(ods, index, 1, "contact");
        DataList1.DataSource=ods.Tables["contact"].DefaultView;
        HiddenField1.Value = ods.Tables["contact"].Rows.Count.ToString();

        DataList1.DataBind();
        con.Close();
}

    catch(Exception ex)
    {
        Response.Write(ex.Message+" "+ex.StackTrace);
    }
}

private void LinkButton1_Click(object sender, System.EventArgs e)
{
    int index = int.Parse(TextBox1.Text);
    if(index >0)
    {
        index--;
        TextBox1.Text= index.ToString();
        Build_List();
    }

}

private void LinkButton2_Click(object sender, System.EventArgs e)
{
    int index = int.Parse(TextBox1.Text);
    if(Convert.ToInt32( HiddenField1.Value) >= index)
    {
        index++;
        TextBox1.Text= index.ToString();
        Build_List();
    }

} 
//  HTML code 
<HTML>

<HEAD>

<title>Paging in Datalist</title>

</HEAD>

<body>

<form id="Form1" method="post" runat="server">

<table border="1" style="width: 300px">

<TBODY>

<tr>

<td><asp:DataList id="DataList1" runat="server" Visible="True">

<HeaderTemplate>

</HeaderTemplate>

<ItemTemplate>

Message :

<%# DataBinder.Eval(Container.DataItem, "Mess")%>

<br>

From User :

<%# DataBinder.Eval(Container.DataItem, "FromUser")%>

<br>

User Name :

<%# DataBinder.Eval(Container.DataItem, "userName")%>

<br>

</ItemTemplate>

<FooterTemplate>

</FooterTemplate>

</asp:DataList></td>

</tr>

<tr>

<td><asp:LinkButton id="LinkButton1" runat="server">Prev</asp:LinkButton>

  

<asp:LinkButton id="LinkButton2" runat="server">Next</asp:LinkButton></td>

</tr>

<tr>

<td><asp:TextBox id="TextBox1" runat="server"></asp:TextBox></td>

</tr>

<tr>

<td><asp:TextBox id="TextBox2" runat="server"></asp:TextBox></td>

</tr>

<tr>

<td></td>

</tr>

</TBODY>

</table>

<asp:HiddenField ID="HiddenField1" runat="server" />

</form>

</body>

</HTML>

Points of Interest

Best of luck guys... :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Sun Rays

Team Leader

India India

Member

Working as a Team Lead in MNC located at India.
In IT carrier, I have worked with Microsoft Technologies and always ready for R&D.
Worked with Website Development and Windows based applications as well.
 

New errors are really good and being happy to resolve those.....

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 20 Nov 2007
Article Copyright 2007 by Sun Rays
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid