Hi,
I want to display records from database using DataList control, in which 10 records should be display at a time. How to make such control?
Thanks.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Jan 16, 2012, 1:35 AM
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str ;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binddatalist();
}
}
private void binddatalist()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "employee");
PagedDataSource Pds1 = new PagedDataSource();
Pds1.DataSource = ds.Tables[0].DefaultView;
Pds1.AllowPaging = true;
Pds1.PageSize = 10;
Pds1.CurrentPageIndex = CurrentPage;
lbl1.Text = "Showing Page: " + (CurrentPage + 1).ToString() + " of " + Pds1.PageCount.ToString();
btnPrevious.Enabled = !Pds1.IsFirstPage;
btnNext.Enabled = !Pds1.IsLastPage;
dl1.DataSource = Pds1;
dl1.DataBind();
con.Close();
}
public int CurrentPage
{
get
{
object s1 = this.ViewState["CurrentPage"];
if (s1 == null)
{
return 0;
}
else
{
return Convert.ToInt32(s1);
}
}
set { this.ViewState["CurrentPage"] = value; }
}
protected void btnPrevious_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
binddatalist();
}
protected void btnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
binddatalist();
}
}
Thanks
If this post helps you mark it as answer
Alok PandeyPosted Jan 16, 2012, 2:16 AM
Alok PandeyPosted Jan 16, 2012, 2:15 AM
Datta KharadPosted Jan 16, 2012, 1:49 AM
You have to do this using paging but datalist doesn't have in-built paging, so you should be do custom paging. I have done this in my previous post, in that i am displaying name and image from the database, you can refer this example it can be useful for you. See in Attachment file.
Abhimanyu K VatsaPosted Jan 16, 2012, 1:46 AM
Use this in your select query:-
---------------------------------
SelectCommand="SELECT TOP 10 field1, field2 FROM tablename
Abhimanyu K VatsaPosted Jan 16, 2012, 1:45 AM
Use this in your select query:-
---------------------------------
SelectCommand="SELECT TOP 10 field1, field2 FROM tablename