62,254
社区成员
发帖
与我相关
我的任务
分享<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<style type="text/css">
.style1{
width:784px;
}
.style2
{
width:126px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<asp:DataList ID="DataList1" runat="server" ForeColor="#333333"
Width="784px">
<FooterStyle BackColor="#990000" Font-Bold="true" ForeColor="White" />
<AlternatingItemStyle BackColor="White" />
<ItemStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SeparatorStyle BorderStyle="Dashed" />
<SelectedItemStyle BackColor="#FFCC66" Font-Bold="true" ForeColor="Navy" />
<HeaderTemplate>
<center>学生信息</center><br />
<table class="style1" style="background-color:#808000">
<tr class="style1">
<td class="style2">编号</td>
<td class="style2">姓名</td>
<td class="style2">年龄</td>
<td class="style2">地址</td>
</tr>
</table>
</HeaderTemplate>
<HeaderStyle BackColor="#990000" Font-Bold="true" ForeColor="White" />
<ItemTemplate><table class="style1" border="0">
<tr>
<td class="style2">
<asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
</td>
<td class="style2">
<asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
</td>
<td class="style2"><center>
<asp:Label ID="ageLabel" runat="server" Text='<%# Eval("age") %>' /></center>
</td>
<td class="style2">
<asp:Label ID="matterLabel" runat="server" Text='<%# Eval("matter") %>' />
</td></tr></table>
</ItemTemplate>
</asp:DataList>
</div>
<div align="center">
<asp:LinkButton ID="FirstLB" runat="server" onclick="FirstLB_Click" >第一页</asp:LinkButton>
<asp:LinkButton ID="PreviousLB" runat="server" onclick="PreviousLB_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="NextLB" runat="server" onclick="NextLB_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="EndLB" runat="server" onclick="EndLB_Click">最后一页</asp:LinkButton>
总<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>页
当前第<asp:Label ID="Label1" runat="server" Text="1"></asp:Label>页
</form>
</body>
</html>
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
{
int CurrentIndex;//当前页数
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataBinds();
}
}
protected void DataBinds() {
SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True");//连接并实例化数据库
string sql = "select * from Student";//定义查询语句
SqlDataAdapter da = new SqlDataAdapter(sql,cn);//实例化对象Adapter
DataSet ds = new DataSet();//实例化DataSet
da.Fill(ds,"Student");//填充
PagedDataSource pds = new PagedDataSource();//初始化分页事例
pds.DataSource = ds.Tables["Student"].DefaultView;
pds.AllowPaging = true;//启动分页
pds.PageSize = 5;//每页显示的个数
CurrentIndex = int.Parse(this.Label1.Text) - 1;//获取当前页数索引
pds.CurrentPageIndex = CurrentIndex;
if (CurrentIndex == 0)
{//如果是第一页,上一页和第一页的控件不可点击
this.PreviousLB.Enabled = false;
this.FirstLB.Enabled = false;
this.NextLB.Enabled = true;
this.EndLB.Enabled = true;
}
else if (CurrentIndex == pds.PageCount - 1)
{
//如果是最后一页,下一页和最后一页空间不可点击
this.PreviousLB.Enabled = true;
this.FirstLB.Enabled = true;
this.NextLB.Enabled = false;
this.EndLB.Enabled = false;
}
else {
this.PreviousLB.Enabled = true;
this.FirstLB.Enabled = true;
this.NextLB.Enabled = true;
this.EndLB.Enabled = true;
}
this.Label2.Text = pds.PageCount.ToString();//获取总页数
DataList1.DataSource = pds;//绑定DataList数据
DataList1.DataBind();
}
protected void FirstLB_Click(object sender, EventArgs e)//首页
{
this.Label1.Text = "1";//页数为1
DataBinds();
}
protected void PreviousLB_Click(object sender, EventArgs e)
{
this.Label1.Text = (int.Parse(Label1.Text) -1).ToString();//页数减1
DataBinds();
}
protected void NextLB_Click(object sender, EventArgs e)//下一页
{
this.Label1.Text = (int.Parse(this.Label1.Text) + 1).ToString();//页数加1
DataBinds();
}
protected void EndLB_Click(object sender, EventArgs e)//末页
{
this.Label1.Text = Label2.Text;//页数为最后一页
DataBinds();
}
}23132213protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
void bind()
{
int curpage = Convert.ToInt32(this.Label1.Text);
PagedDataSource ps = new PagedDataSource();
pagebind(ps);
ps.AllowPaging = true;
ps.PageSize = 5;
ps.CurrentPageIndex = curpage -1;
this.LinkButton1.Enabled = true;
this.LinkButton2.Enabled = true;
this.LinkButton3.Enabled = true;
this.LinkButton4.Enabled = true;
if (curpage == 1)
{
this.LinkButton1.Enabled = false;
this.LinkButton2.Enabled = false;
}
if (curpage == ps.PageCount)
{
this.LinkButton3.Enabled = false;
this.LinkButton4.Enabled = false;
}
this.Label2.Text = ps.PageCount.ToString();
this.DataList1.DataSource = ps;
this.DataList1.DataBind();
}
void pagebind(PagedDataSource pd)
{
string strcon="server=.;database=pubs;uid=sa;pwd=sa";
SqlConnection cn = new SqlConnection(strcon);
SqlDataAdapter da = new SqlDataAdapter("select title_id,title,price from titles", cn);
DataSet ds = new DataSet();
da.Fill(ds, "titles");
pd.DataSource = ds.Tables["titles"].DefaultView;
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
this.Label1.Text = "1";
bind();
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
this.Label1.Text = (Convert.ToInt32(this.Label1.Text) - 1).ToString();
bind();
}
protected void LinkButton3_Click(object sender, EventArgs e)
{
this.Label1.Text=(Convert.ToInt32(this.Label1.Text)+1).ToString();
bind();
}
protected void LinkButton4_Click(object sender, EventArgs e)
{
this.Label1.Text = Label2.Text;
bind();
}