Datalist 分页
.aspx.cs
protected SqlConnection conn; //添加数据库的操作对象
protected SqlDataAdapter da;
protected DataSet ds;
protected SqlCommand comm;
protected void Page_Load(object nder, EventArgs e)
{
wavingflag
if (!IsPostBack)
{
getArticle();
}
}
private void getArticle() //取得Article 数据
{
conn = new SqlConnection("Data Source=.;Initial Catalog=myrice干部学习平台;Integrated Security=True");//取连接字符串,建立连接
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("lect 留言编号,用户名, 留言题目, 留言内容, 留言时间, 留言回复, 回复管理员, 回复时间 from 留言信息表 ORDER BY 留言时间 DESC, 回复时间 DESC ", conn);
ds = new DataSet();
try
{
conn.Open();
da.Fill(ds, "Article");
conn.Clo();
}
catch (SqlException e1)
{
Respon.Write(e1.ToString());
}
int cup = Convert.ToInt32(this.lb_CurrentPage.Text); //当前页数love is blind,初始化为地1 页
PagedDataSource ps = new PagedDataSource();
ps.DataSource = ds.Tables["Article"].DefaultView;
ps.AllowPaging = true;
ps.PageSize =5; //每页显示的数据的行数
ps.CurrentPageIndex = cup - 1;
lb_count.Text = ps.DataSourceCount.ToString(); //获取记录总数
lb_page.Text = ps.PageCount.ToString(); //获取总页数
if (!IsPostBack)
{
verso for (int i = 1; i < ps.PageCount + 1; i++)
{
this.DropDownList1.Items.Add(i.ToString());
}
LinkUp.Enabled = true;
LinkDown.Enabled = true;
}
try
{
DropDownList1.SelectedItem.Text = cup.ToString();
DataList1.DataSource = ps;
DataList1.DataBind();
}
catch (Exception ex)
{
Respon.Write(ex.Message);
}
}
vina
protected void LinkDown_Click(object nder, EventArgs e) //下一页按钮代码
{
try
{
lb_CurrentPage.Text = Convert.ToString(Convert.ToInt32(lb_CurrentPage.Text) + 1);
DropDownList1.SelectedValue = lb_CurrentPage.Text;
getArticle();
}
catch
{
Respon.Write("<script language=javascript>" + "alert(\"已经是最后一页\")" + "</script>");
lb_CurrentPage.Text = "1";
getArticle();
}
}
protected void DropDownList1_SelectedIndexChanged(object nder, EventArgs e) //跳转到指定页代码
{
int page = Convert.ToInt16((DropDownList1.SelectedItem.Value));
lb_CurrentPage.Text = page.ToString();
getArticle();
}
protected void LinkUp_Click(object nder, EventArgs e) //上一页按钮代码
{
try
{
if (Convert.ToInt16(lb_CurrentPage.Text) > 1)
{
lb_CurrentPage.Text =
Convert.ToString(Convert.ToInt32(lb_CurrentPage.Text) - 1);
DropDownList1.SelectedValue = lb_CurrentPage.Text;
overlap getArticle();
}
el
{
Respon.Write("<script language=javascript>" + "alert(\"已经是第一页\")" + "</script>");
}
}
catch
{
Respon.Write("<script language=javascript>" + "alert(\" 已经是第一页\")" + "</script>");
}
}
protected void LinkFirst_Click(object nder, EventArgs e) //跳到第一页代码
mentor {节约水资源
if (lb_CurrentPage.Text != "1")
{
lb_CurrentPage.Text = "1";
}
el
{
Respon.Write("<script language=javascript>" + "alert(\" 已经是第一页\")" + "</script>");
}
getArticle();
}
protected void LinkLast_Click(object nder, EventArgs e) //跳到最后一页代码
{
if (lb_CurrentPage.Text.ToString() != lb_page.Text.ToString())
{
lb_CurrentPage.Text = lb_page.Text.ToString();
}
el
{
Respon.Write(美联储的秘密"<script language=javascript>" + "alert(\"已经是最后一页\")" + "</script>");
}
getArticle();
}
.aspx
<双是什么意思是asp:DataList ID="DataList1" runat="rver" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" Width="804px" DataKeyField="留言编号" OnItemCommand="DataList1_ItemCommand" >