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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Jan 15, 2012, 8:35 PM EST
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
Abhimanyu K VatsaRank #14 · 51.7K reputationPosted Jan 15, 2012, 8:45 PM EST
Use this in your select query:-
---------------------------------
SelectCommand="SELECT TOP 10 field1, field2 FROM tablename
Abhimanyu K VatsaRank #14 · 51.7K reputationPosted Jan 15, 2012, 8:46 PM EST
Use this in your select query:-
---------------------------------
SelectCommand="SELECT TOP 10 field1, field2 FROM tablename
Datta KharadRank #2,135 · 668 reputationPosted Jan 15, 2012, 8:49 PM EST
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.
Alok PandeyRank #271 · 9.3K reputationPosted Jan 15, 2012, 9:15 PM EST
Alok PandeyRank #271 · 9.3K reputationPosted Jan 15, 2012, 9:16 PM EST