62,046
社区成员
发帖
与我相关
我的任务
分享
SqlConnection conn;
DataSet ds;
SqlDataAdapter da;
PagedDataSource pds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
public void Bind()
{
conn = new SqlConnection(str);
conn.Open();
string SQL = "select * from Froms";//froms是表名
da = new SqlDataAdapter(SQL, conn);//conn是连接字符串
ds = new DataSet();
da.Fill(ds, "Froms");
pds = new PagedDataSource();
pds.PageSize = 3;
pds.AllowPaging = true;
pds.DataSource=ds.Tables["Froms"].DefaultView;
int cont = pds.PageCount;
lblCount.Text = cont.ToString();
int current = Convert.ToInt32(lblCurrent.Text);
pds.CurrentPageIndex=current-1;
lkbFirst.Enabled = true;
lkbPrev.Enabled = true;
lkbNext.Enabled = true;
lkbLast.Enabled = true;
if (current == 1)
{
lkbFirst.Enabled = false;
lkbPrev.Enabled = false;
}
if (current ==pds.PageCount)
{
lkbNext.Enabled = false;
lkbLast.Enabled = false;
}
datalist1.DataSource = pds;
datalist1.DataKeyField = "ID";
datalist1.DataBind();
conn.Close();
}
protected void lkbFirst_Click(object sender, EventArgs e)
{
lblCurrent.Text = "1";
Bind();
}
protected void lkbPrev_Click(object sender, EventArgs e)
{
lblCurrent.Text = Convert.ToString(Convert.ToInt32(lblCurrent.Text) - 1);
Bind();
}
protected void lkbNext_Click(object sender, EventArgs e)
{
lblCurrent.Text = Convert.ToString(Convert.ToInt32(lblCurrent.Text) + 1);
Bind();
}
protected void lkbLast_Click(object sender, EventArgs e)
{
lblCurrent.Text = lblCount.Text;
Bind();
}
<asp:datalist ID="datalist1" runat="server" >
<ItemTemplate>
Title:
<asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
<br />
Body:
<asp:Label ID="BodyLabel" runat="server" Text='<%# Eval("Body") %>' />
<br />
Datetime:
<asp:Label ID="DatetimeLabel" runat="server" Text='<%# Eval("Datetime") %>' />
<br />
</ItemTemplate>
</asp:datalist>
当前<asp:Label ID="lblCurrent" runat="server" Text="1"></asp:Label>页/共
<asp:Label ID="lblCount" runat="server" >页</asp:Label>
<asp:Label ID="Label4" runat="server"></asp:Label>
<asp:LinkButton ID="lkbFirst" runat="server" CausesValidation="false"
onclick="lkbFirst_Click" >首页</asp:LinkButton>
<asp:LinkButton ID="lkbPrev" runat="server" CausesValidation="false"
onclick="lkbPrev_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="lkbNext" runat="server" CausesValidation="false"
onclick="lkbNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lkbLast" runat="server" CausesValidation="false"
onclick="lkbLast_Click">末页</asp:LinkButton>