Datalist使用存储过程来分页。
存储过程;
create PROCEDURE [dbo].[sp_Mail_Select_by_Page_rowNumber]
@pageSize int, --每页记录数量
@pageCount int output, --总页数
@pageIndex int --当前页索引号
AS
BEGIN
declare @totalRecords int
select @totalRecords = count(PId) from Product
if(@totalRecords % @pageSize = 0)
set @pageCount = @totalRecords / @pageSize;
else
set @pageCount = @totalRecords / @pageSize +1;
with temp as (select row_number() over (order by PId) as id,* from Product)
select * from temp where id between (@pageIndex -1)*@pageSize +1 and @pageIndex * @pageSize
return @totalRecords
end
前台代码:
<div>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
商品名:<asp:Label ID="Label2" runat="server" Text='<%# Eval("ProdctName") %>'></asp:Label>
<br />
规格:<asp:Label ID="Label3" runat="server" Text='<%# Eval("ProdctStandard") %>'></asp:Label>
<br />
包装率:<asp:Label ID="Label4" runat="server" Text='<%# Eval("PackagingRatio") %>'></asp:Label>
<br />
商品条码:<asp:Label ID="Label5" runat="server" Text='<%# Eval("ArticleNum") %>'></asp:Label>
<br />
价格:<asp:Label ID="Label6" runat="server" Text='<%# Eval("Price") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>
<br />
<asp:Button ID="btnFirst" runat="server" Text="|<"
onclick="btnFirst_Click" />
<asp:Button ID="btnPrev" runat="server" Text="<" onclick="btnPrev_Click" />
<asp:Button ID="btnNext" runat="server" Text=">" onclick="btnNext_Click" />
<asp:Button ID="btnLast" runat="server" Text=">|" onclick="btnLast_Click" />
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Width="25px"></asp:TextBox>
<asp:Button ID="Button5" runat="server" Text="Go" onclick="Button5_Click" />
<br />
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:HiddenField ID="HiddenField2" runat="server" />
</div>
后台代码:
public partial class DataListPaging : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataListBind(1);
}
}
private void DataListBind(int index)
{
string conStr = ConfigurationManager.ConnectionStrings["mailConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_Mail_Select_by_Page_rowNumber";
cmd.Parameters.AddWithValue("@pageSize", 6);
cmd.Parameters.AddWithValue("@pageIndex", index);
cmd.Parameters.Add("@pageCount", System.Data.SqlDbType.Int).Direction = ParameterDirection.Output;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
DataList1.DataSource = dt;
DataList1.DataBind();
string count = cmd.Parameters["@pageCount"].Value.ToString();
Label1.Text = "当前第" + index.ToString() + "页,共" + count + "页";
HiddenField1.Value = index.ToString();
HiddenField2.Value = count;
conn.Close();
conn.Dispose();
}
protected void btnFirst_Click(object sender, EventArgs e)
{
DataListBind(1);
}
protected void btnPrev_Click(object sender, EventArgs e)
{
int index=int.Parse(HiddenField1.Value.Trim());
if (index > 1)
{
index--;
DataListBind(index);
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
int index=int.Parse(HiddenField1.Value.Trim());
int count=int.Parse(HiddenField2.Value.Trim());
if (index < count)
{
index++;
DataListBind(index);
}
}
protected void btnLast_Click(object sender, EventArgs e)
{
DataListBind(int.Parse(HiddenField2.Value.Trim()));
}
protected void Button5_Click(object sender, EventArgs e)
{
int num = int.Parse(TextBox1.Text.Trim());
int index=int.Parse(HiddenField1.Value.Trim());
int count=int.Parse(HiddenField2.Value.Trim());
if (num >= 1 && num <= count)
{
DataListBind(num);
}
else
{
Label1.Text="当前页不存在!!";
}
}