想找一个分页的源码学习一下。请大家帮忙。数据库是SQL2000的,表的内容随意。要求:分页显示为: 第一页 1 2 3 4 5....最后页 共XX页 运行环境:VS2005
请将源码发送到邮箱 [email protected]
请将源码发送到邮箱 [email protected]
解决方案 »
- 网站运行中的奇怪问题----望高手指教。
- radiobuttonlist如何在鼠表划过显示相应的备注信息
- 在框架中用哪个控件或方法来更简单的实现Tab页切换的功能
- 100分敬上请大家帮忙解决以下~谢谢
- vs2005中label的单击事件怎样添加?
- asp.net(c#)有什么好论坛下载吗??
- 不是高手请不要进来
- webgrid如何按某列倒排序
- JS+ASP.NET+COOKIE,帮忙看看啊!
- ASP.Net C#2.0全能数据库组件 (开源含实例源码)
- Vs2005 asp.net development server未能开始侦听端口
- 想用oledb连接sql server,为什么在传递参数时会出现错误“必须声明变量 '@userID'”
<td align="center" valign="top">
<asp:GridView ID="gv_File" runat="server" AllowPaging="True" AutoGenerateColumns="False"
BorderColor="#AAC1ED" BorderStyle="Solid" OnPageIndexChanging="gv_File_PageIndexChanging"
PageSize="5" Width="98%">
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Font-Bold="False" />
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="filename" HeaderText="文件名">
<HeaderStyle Font-Bold="False" />
</asp:BoundField>
<asp:BoundField DataField="checked" HeaderText="是否检查">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Font-Bold="False" />
</asp:BoundField>
<asp:BoundField DataField="format" HeaderText="是否检查通过">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Font-Bold="False" />
</asp:BoundField>
<asp:BoundField DataField="converted" HeaderText="是否导入">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Font-Bold="False" />
</asp:BoundField>
</Columns>
<PagerTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument="First" CommandName="Page">第一页</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandArgument="Prev" CommandName="Page">上一页</asp:LinkButton><span
style="color: #ff0000"> </span>
<asp:LinkButton ID="LinkButton3" runat="server" CommandArgument="Next" CommandName="Page">下一页</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" CommandArgument="Last" CommandName="Page">最后一页</asp:LinkButton>
<font color="black">[当前是第 <font color="red">
<%= gv_File.PageIndex * gv_File.PageSize + 1%>
-
<%=gv_File.PageIndex * gv_File.PageSize + gv_File.PageSize%>
</font>]条</font> <font color="black">[共 <font color="red">
<%= iRecordCount %>
</font>条记录]</font>
</PagerTemplate>
<PagerStyle HorizontalAlign="Right" />
<HeaderStyle HorizontalAlign="Center" CssClass="head" />
</asp:GridView>
</td>
/// 分页函数
/// </summary>
/// <param name="pageSize">每页记录记录数</param>
/// <param name="navNum">数字页码显示个数</param>
/// <param name="firstPage">首页</param>
/// <param name="prevPage">上页</param>
/// <param name="nextPage">下页</param>
/// <param name="lastPage">末页</param>
/// <param name="leftHtml">页码左边内容</param>
/// <param name="rightHtml">页码右边内容</param>
/// <param name="outerSpace">首上下末间空格</param>
/// <param name="innerSpace">数字页码间空格</param>
/// <param name="urlParam">要转向的URL地址,须加?号</param>
/// <param name="tableKey">表的主键</param>
/// <param name="tableName">表的名称</param>
/// <param name="theField">表的字段</param>
/// <param name="theWhere">以where开头,不能为空</param>
/// <param name="theOrder">排序语句,可为空</param>
/// <param name="currentPage">当前页,从1开始</param>
/// <param name="cmdParam">含参数的Command对象</param>
/// <param name="objs">参数列表</param>
public static Hashtable ExecutePage(int pageSize, int navNum, string firstPage, string prevPage, string nextPage, string lastPage, string leftHtml, string rightHtml, string outerSpace, string innerSpace, string urlParam, string tableKey, string tableName, string theField, string theWhere, string theOrder, int currentPage, SqlCommand cmdParam, params object[] objs)
{
int num = 0;
Hashtable ha = new Hashtable(); if (navNum % 2 == 0) navNum++;
if (firstPage == "0")
{
firstPage = "<font face=webdings>9</font>";
prevPage = "<font face=webdings>7</font>";
nextPage = "<font face=webdings>8</font>";
lastPage = "<font face=webdings>:</font>";
}
if (prevPage == "0")
{
firstPage = "";
prevPage = "<font face=webdings>7</font>";
nextPage = "<font face=webdings>8</font>";
lastPage = "";
} SqlCommand cmd = cmdParam;
if (cmd == null) cmd = new SqlCommand(); try
{
cmd.Connection = new SqlConnection(ConnectionString);
cmd.CommandText = "select count(*) from " + tableName + " " + theWhere;
for (int i = 0; i < objs.Length; i += 2) cmd.Parameters.Add(objs[i].ToString(), objs[i + 1]);
cmd.Connection.Open();
num = Convert.ToInt32(cmd.ExecuteScalar());
}
catch (SqlException ex)
{
ErrorMessage = ex.Message;
}
finally
{
cmd.Connection.Close();
} int total = (num - 1) / pageSize + 1;
if (currentPage < 1 || currentPage > num || num < 1) return null;
string sql = "select top " + pageSize + " " + theField + " from " + tableName + " " + theWhere + " " + theOrder;
if (currentPage != 1) sql = "select top " + pageSize + " " + theField + " from " + tableName + " " + theWhere + " and " + tableKey + " not in (select top " + pageSize * (currentPage - 1) + " " + tableKey + " from " + tableName + " " + theWhere + " " + theOrder + ") " + theOrder;
cmd.CommandText = sql;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds); string span = "<span>";
if (currentPage != 1)
{
span += "<a title='首页' href='" + urlParam + "&page=1'>" + firstPage + "</a>" + outerSpace;
span += "<a title='上一页' href='" + urlParam + "&page=" + (currentPage - 1) + "'>" + prevPage + "</a>" + outerSpace;
}
else
{
span += "<a disabled=disabled>" + firstPage + "</a>" + outerSpace;
span += "<a disabled=disabled>" + prevPage + "</a>" + outerSpace;
}
if (navNum > 0)
{
int start = 1, end = total, len = navNum / 2;
if (navNum < total)
{
start = currentPage - len;
end = currentPage + len;
if (start < 1)
{
start = 1;
end = navNum;
}
if (end > total)
{
start = total - navNum + 1;
end = total;
}
}
for (int i = start; i <= end; i++)
{
if (i == currentPage)
{
//span += "<font color=red>" + leftHtml + i + rightHtml + "</font>" + innerSpace;
span += i + innerSpace;
}
else
{
if (i != end)
{
span += "<a title='第" + i + "页' href='" + urlParam + "&page=" + i + "'>" + leftHtml + i + rightHtml + "</a>" + innerSpace;
}
else
{
span += "<a title='第" + i + "页' href='" + urlParam + "&page=" + i + "'>" + leftHtml + i + rightHtml + "</a>";
}
}
}
span = span.Trim(' ');
span += outerSpace;
}
if (currentPage != total)
{
span += "<a title='下一页' href='" + urlParam + "&page=" + (currentPage + 1) + "'>" + nextPage + "</a>" + outerSpace;
span += "<a title='尾页' href='" + urlParam + "&page=" + ((num - 1) / pageSize + 1) + "'>" + lastPage + "</a>";
}
else
{
span += "<a disabled=disabled>" + nextPage + "</a>" + outerSpace;
span += "<a disabled=disabled>" + lastPage + "</a>";
}
ha.Add("MainPage", span);
span = "<table cellpadding=0 cellspacing=0 width=100% style='margin-top:10px'><tr><td style='padding-left:10px;padding-top:2px'>第<font color=red>" + currentPage + "</font>页 共<font color=red>" + total + "</font>页 每页<font color=red>" + pageSize + "</font>条记录 共<font color=red>" + num + "</font>条记录</td><td align=right>" + span + "</td></tr></table>";
ha.Add("AutoPage", span);
span = "\n<input id=goto size=1 value=" + currentPage + " onchange=\"location.href='" + urlParam + "&page='+document.getElementById('goto').value\">\n<input type=button size=1 value=GO onclick=\"location.href='" + urlParam + "&page='+document.getElementById('goto').value\">";
ha.Add("GotoPage", span);
span = "\n<select onchange=\"location.href='" + urlParam + "&page='+this.value\">\n";
for (int i = 1; i <= total; i++) span += "\t<option value=" + i + (i == currentPage ? " selected" : "") + ">第" + i + "页</option>\n";
span += "</select>\n";
ha.Add("SelectPage", span);
ha.Add("CurrentPage", currentPage);
ha.Add("TotalPage", total);
ha.Add("PageSize", pageSize);
ha.Add("TotalRecords", num);
ha.Add("DataSet", ds);
return ha;
}
网址http://www.webdiyer.com/AspNetPager/default.aspx
<div class="content" style="width: 776px; text-align: left">
<div style="overflow-x: scroll; overflow-y: hidden;width: 776px; ">
<asp:GridView ID="grid_List" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="C_BatchSale.BatchSaleNumber"
CellPadding="4" OnDataBound="grid_List_DataBound" PageSize="20" Width="1400px" OnRowDeleting="grid_List_RowDeleting" OnRowDataBound="grid_List_RowDataBound" BorderColor="#93BEE2" BorderStyle="Solid">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="C_BatchSale.CustomerID" DataNavigateUrlFormatString="CustomerInfo.aspx?id={0}"
DataTextField="C_BatchSale.CustomerName" HeaderText="客户名称">
<ItemStyle Width="150px" />
</asp:HyperLinkField>
<asp:BoundField DataField="DName" HeaderText="所属区域">
<ItemStyle Width="140px" />
</asp:BoundField>
<asp:BoundField DataField="C_BatchSale.BatchSaleNumber" HeaderText="批销单号">
<ItemStyle Width="90px" />
</asp:BoundField>
<asp:BoundField DataField="C_BatchSale.SaleDate" DataFormatString="{0:d}" HeaderText="批销日期">
<ItemStyle Width="70px" />
</asp:BoundField>
<asp:BoundField DataField="C_BatchSale.BSTypesCode" HeaderText="批销类型">
<ItemStyle Width="53px" />
</asp:BoundField>
<asp:BoundField DataField="C_BatchSale.CarryModeCode" HeaderText="发货方式">
<ItemStyle Width="53px" />
</asp:BoundField>
<asp:BoundField DataField="C_BatchSale.ReceiveStateCode" HeaderText="收款情况">
<ItemStyle Width="53px" />
</asp:BoundField>
<asp:BoundField DataField="C_BatchSale.Operator" HeaderText="操作员">
<ItemStyle Width="53px" />
</asp:BoundField>
<asp:BoundField DataField="C_BatchSale.TotalFixedPrice" HeaderText="总码洋">
<ItemStyle Width="60px" />
</asp:BoundField>
<asp:BoundField DataField="C_BatchSale.TotalDiscountedPrice" HeaderText="总实洋">
<ItemStyle Width="60px" />
</asp:BoundField>
<asp:BoundField DataField="C_BatchSale.AverageRebate" HeaderText="平均折扣">
<ItemStyle Width="53px" />
</asp:BoundField>
<asp:BoundField DataField="C_BatchSale.AllowSendBackCode" HeaderText="允许退货">
<ItemStyle Width="53px" />
</asp:BoundField>
<asp:BoundField DataField="C_BatchSale.StateCode" HeaderText="单据状态">
<ItemStyle Width="53px" />
</asp:BoundField>
<asp:BoundField DataField="C_BatchSale.TotalCount" HeaderText="本单数量">
<ItemStyle Width="53px" />
</asp:BoundField>
<asp:BoundField DataField="C_BatchSale.AddDate" HeaderText="添加日期">
<ItemStyle Width="120px" />
</asp:BoundField>
<asp:BoundField DataField="C_BatchSale.Re" Visible="False" HeaderText="备注"/>
<asp:HyperLinkField DataNavigateUrlFields="C_BatchSale.BatchSaleNumber" DataNavigateUrlFormatString="AddBatchSale.aspx?bid={0}&type=upd"
HeaderText="修改" Text="修改">
<ItemStyle Width="30px" />
</asp:HyperLinkField>
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
OnClientClick='return confirm("您确定要删除这条记录吗?")' Text="删除"></asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="30px" />
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#859FD4" Font-Bold="False" ForeColor="White" />
</asp:GridView>
</div>
<div style="margin-top: 0px; font-size: 12px; letter-spacing: 2px; text-align: right;
text-decoration: none">
<webdiyer:aspnetpager id="Pager" runat="server" alwaysshow="True" cssclass="" custominfosectionwidth="10%"
custominfotext="" custominfotextalign="Justify" imagepath="/" inputboxclass="txtnav" NumericButtonTextFormatString="[{0}]"
inputboxstyle="txtnav" onpagechanged="Pager_PageChanged" pagesize="20" pagingbuttonspacing="80px"
showcustominfosection="Left" showinputbox="Always" submitbuttonclass="btn1nav"
submitbuttonstyle="btn1nav" textafterinputbox="页 " textbeforeinputbox="第">
</webdiyer:aspnetpager>
</div>
</div>
/// 分页绑定
/// </summary>
/// <param name="strTCondition"></param>
private void GridTotal(string strTCondition)
{ SqlDataReader drStorage = member.getBatchSale(Convert.ToInt32(Session["user_id"]), 1, 1, true, strTCondition);
while (drStorage.Read())
{
Pager.RecordCount = Convert.ToInt32(drStorage[0].ToString());
}
drStorage.Close();
} /// <summary>
/// gridview绑定
/// </summary>
/// <param name="strGCondition"></param>
private void BindGrid(string strGCondition)
{ SqlDataReader drStorage = member.getBatchSale(Convert.ToInt32(Session["user_id"]), Pager.PageSize, Pager.CurrentPageIndex, false, strGCondition); DataTable dtStorage = tools.ConvertDataReaderToDataTable(drStorage);
dtStorage.TableName = "BatchSale";
grid_List.DataSource = dtStorage.DefaultView;
grid_List.DataBind();
lblNum.Text = Pager.RecordCount.ToString();
}
/// <summary>
/// 分页控件分页事件
/// </summary>
/// <param name="src"></param>
/// <param name="e"></param>
protected void Pager_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
{
Pager.CurrentPageIndex = e.NewPageIndex;
BindGrid(strCondition);
}
public SqlDataReader getBatchSale(Int32 id, int iSize, int iIndex, bool bDocount, string strCondition)
{
SqlDataReader dataReader = null;
database data = new database();
SqlParameter[] prams = {
data.MakeInParam("@id",SqlDbType.BigInt,8,id), //用作条件,用户id(可选)
data.MakeInParam("@pagesize",SqlDbType.Int,4,iSize),//每页显示条数
data.MakeInParam("@pageindex",SqlDbType.Int,4,iIndex),//第几页
data.MakeInParam("@docount",SqlDbType.Bit,1,bDocount),//是否统计数据
data.MakeInParam("@condition",SqlDbType.VarChar,800,strCondition) //其他条件(可选)
};
try
{
data.RunProc("sp_member_BatchSale", prams, out dataReader);
return (dataReader);
}
catch (Exception ex)
{
Error.Log(ex.ToString());
return null;
}
finally
{
data = null;
}
}
============存储过程CREATE PROCEDURE sp_member_BatchSale @id bigint,
@pagesize int,
@pageindex int,
@docount bit,
@condition varchar(800) AS
set nocount on if(@docount=1)
exec('select count(*) FROM web_C_BatchSale C_BatchSale inner join web_C_Customer c on c.CustomerID = C_BatchSale.CustomerID
left join Common_DCode on Common_DCode.DCode=c.AreaCode where C_BatchSale.UpUserID='+@id+@condition)
else
begin
declare @PageLowerBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
if(@PageLowerBound=0)
exec('SELECT top 20 C_BatchSale.CustomerID as "C_BatchSale.CustomerID",
c.CustomerName as "C_BatchSale.CustomerName",
Common_DCode.DName,
C_BatchSale.BatchSaleNumber as "C_BatchSale.BatchSaleNumber",
Convert(varchar(10),C_BatchSale.SaleDate,120) as "C_BatchSale.SaleDate",
C_BatchSale.BSTypesCode as "C_BatchSale.BSTypesCode",
C_BatchSale.CarryModeCode as "C_BatchSale.CarryModeCode",
C_BatchSale.ReceiveModeCode as "C_BatchSale.ReceiveModeCode",
C_BatchSale.ReceiveStateCode as "C_BatchSale.ReceiveStateCode",
C_BatchSale.Operator as "C_BatchSale.Operator",
C_BatchSale.AddDate as "C_BatchSale.AddDate",
convert(numeric(8,2),round(C_BatchSale.TotalFixedPrice,2)) as "C_BatchSale.TotalFixedPrice",
convert(numeric(8,2),round(C_BatchSale.TotalDiscountedPrice,2)) as "C_BatchSale.TotalDiscountedPrice",
convert(numeric(8,2),round(C_BatchSale.AverageRebate,2)) as "C_BatchSale.AverageRebate",
C_BatchSale.AllowSendBackCode as "C_BatchSale.AllowSendBackCode",
C_BatchSale.StateCode as "C_BatchSale.StateCode",
C_BatchSale.TotalCount as "C_BatchSale.TotalCount",
C_BatchSale.Re as "C_BatchSale.Re"
FROM web_C_BatchSale C_BatchSale inner join web_C_Customer c on c.CustomerID = C_BatchSale.CustomerID
left join Common_DCode on Common_DCode.DCode=c.AreaCode
where C_BatchSale.UpUserID='+@id+@condition+'
ORDER BY C_BatchSale.OrderID')
else
begin
exec('SELECT top 20 C_BatchSale.CustomerID as "C_BatchSale.CustomerID",
c.CustomerName as "C_BatchSale.CustomerName",
Common_DCode.DName,
C_BatchSale.BatchSaleNumber as "C_BatchSale.BatchSaleNumber",
Convert(varchar(10),C_BatchSale.SaleDate,120) as "C_BatchSale.SaleDate",
C_BatchSale.BSTypesCode as "C_BatchSale.BSTypesCode",
C_BatchSale.CarryModeCode as "C_BatchSale.CarryModeCode",
C_BatchSale.ReceiveModeCode as "C_BatchSale.ReceiveModeCode",
C_BatchSale.ReceiveStateCode as "C_BatchSale.ReceiveStateCode",
C_BatchSale.Operator as "C_BatchSale.Operator",
C_BatchSale.AddDate as "C_BatchSale.AddDate",
convert(numeric(8,2),round(C_BatchSale.TotalFixedPrice,2)) as "C_BatchSale.TotalFixedPrice",
convert(numeric(8,2),round(C_BatchSale.TotalDiscountedPrice,2)) as "C_BatchSale.TotalDiscountedPrice",
convert(numeric(8,2),round(C_BatchSale.AverageRebate,2)) as "C_BatchSale.AverageRebate",
C_BatchSale.AllowSendBackCode as "C_BatchSale.AllowSendBackCode",
C_BatchSale.StateCode as "C_BatchSale.StateCode",
C_BatchSale.TotalCount as "C_BatchSale.TotalCount",
C_BatchSale.Re as "C_BatchSale.Re"
FROM web_C_BatchSale C_BatchSale inner join web_C_Customer c on c.CustomerID = C_BatchSale.CustomerID
left join Common_DCode on Common_DCode.DCode=c.AreaCode
where C_BatchSale.UpUserID='+@id+@condition+'
and C_BatchSale.orderID>(select max(OrderID) From
(Select top '+@PageLowerBound+' C_BatchSale.OrderID FROM web_C_BatchSale C_BatchSale inner join web_C_Customer c
on c.CustomerID = C_BatchSale.CustomerID left join Common_DCode on Common_DCode.DCode=c.AreaCode
where C_BatchSale.UpUserID='+@id+@condition+'
ORDER BY C_BatchSale.orderID
) As ENGINE_CHILDQUERY_TEMPTABLE)
ORDER BY C_BatchSale.orderID')
end
end
set nocount off
GO============================上面那是未优化过的分页函数,下面是优化过的,你自己对着修改,结贴给分CREATE PROCEDURE sp_memberCenter_BookStock
@id int,
@pagesize int,
@pageindex int,
@docount bit,
@condition varchar(800)
AS
set nocount on if(@docount=1)
exec('select count(*) from web_publish_Books_Storage a inner join web_publish_books b
on a.booknumber=b.booknumber where b.upuserid='+@id+@condition) else
begin --select * from common_code where codeid=7
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound
set @PageLowerBound=@PageLowerBound+1
exec('declare @indextable table(id int identity(1,1),nid int)
insert into @indextable(nid) select a.BookNumber from web_publish_Books_Storage a inner join web_publish_books b
on a.booknumber=b.booknumber where b.upuserid='+@id+@condition+' order by a.adddate desc SELECT a.booknumber,storagevalues,instoragetotal,a.adddate,bookname,
(SELECT typesname FROM web_B_BookTypes z where b.BookTypeCode=z.typesid)as booktypecode,author,
(select codename from common_code c where c.codeid=7 and a.SaleState=c.code) as SaleState,
Convert(numeric(10,2),price) as price
FROM web_publish_Books_Storage a inner join web_publish_books b
on a.booknumber=b.booknumber,@indextable t
where b.upuserid='+@id+@condition+' and a.BookNumber=t.nid
and t.id between '+@PageLowerBound+' and '+@PageUpperBound+'
order by t.id ')
end
set nocount off
GO
{
StringBuilder builder = new StringBuilder();
StringBuilder builder2 = new StringBuilder();
string str = Myurlstr + Myurlpage;
int num = Myallcount / Mypagesize;
if (num == 0)
{
num = 1;
}
else if ((Myallcount % Mypagesize) > 0)
{
num++;
}
int num3 = 0;
if (Myallcount > 0)
{
int num2;
int num4;
int num5;
builder.Append("<td class=pagefalse><a href=#>共有" + Myallcount + "条记录</a></td>\r\n");
builder.Append("<td class=pagefalse><a href=" + Mypostbackurl + "?" + str + "=1 title=首页>首页</a></td>\r\n");
if (!(((HttpContext.Current.Request.QueryString[Myurlpage] != null) && (HttpContext.Current.Request.QueryString[Myurlpage].ToString() != null)) && int.TryParse(HttpContext.Current.Request.QueryString[Myurlpage], out num4)))
{
num2 = 1;
}
else
{
num2 = Convert.ToInt32(HttpContext.Current.Request.QueryString[Myurlpage]);
}
if (num < Mypageshowsize)
{
for (num5 = (num2 / Mypageshowsize) + 1; num5 <= num; num5++)
{
if (num2 == num5)
{
builder.Append("<td class=pagetrue><a href=" + Mypostbackurl + "?" + str + "=" + num5.ToString() + " title=第" + num5.ToString() + "页>" + num5.ToString() + "</a></td>\r\n");
}
else
{
builder.Append("<td class=pagefalse><a href=" + Mypostbackurl + "?" + str + "=" + num5.ToString() + " title=第" + num5.ToString() + "页>" + num5.ToString() + "</a></td>\r\n");
}
}
}
else if ((num2 / Mypageshowsize) > 0)
{
builder.Append(string.Concat(new object[] { "<td class=pagefalse><a href=", Mypostbackurl, "?", str, "=", Convert.ToInt32((int) (((num2 / Mypageshowsize) * Mypageshowsize) - 1)), " title=上一组>.....</a></td>\r\n" }));
if ((num2 / Mypageshowsize) < (num / Mypageshowsize))
{
for (num5 = (num2 / Mypageshowsize) * Mypageshowsize; num5 <= (((num2 / Mypageshowsize) + 1) * Mypageshowsize); num5++)
{
if (num2 == num5)
{
builder.Append("<td class=pagetrue><a href=" + Mypostbackurl + "?" + str + "=" + num5.ToString() + " title=第" + num5.ToString() + "页>" + num5.ToString() + "</a></td>\r\n");
}
else
{
builder.Append("<td class=pagefalse><a href=" + Mypostbackurl + "?" + str + "=" + num5.ToString() + " title=第" + num5.ToString() + "页>" + num5.ToString() + "</a></td>\r\n");
}
}
num3 = ((num2 / Mypageshowsize) + 1) * Mypageshowsize;
builder.Append("<td class=pagefalse><a href=" + Mypostbackurl + "?" + str + "=" + num3.ToString() + " title=下一组>.....</a></td>\r\n");
}
else
{
for (num5 = (num2 / Mypageshowsize) * Mypageshowsize; num5 <= num; num5++)
{
if (num2 == num5)
{
builder.Append("<td class=pagetrue><a href=" + Mypostbackurl + "?" + str + "=" + num5.ToString() + " title=第" + num5.ToString() + "页>" + num5.ToString() + "</a></td>\r\n");
}
else
{
builder.Append("<td class=pagefalse><a href=" + Mypostbackurl + "?" + str + "=" + num5.ToString() + " title=第" + num5.ToString() + "页>" + num5.ToString() + "</a></td>\r\n");
}
}
num3 = num;
}
}
else
{
for (num5 = (num2 / Mypageshowsize) + 1; num5 <= Mypageshowsize; num5++)
{
if (num2 == num5)
{
builder.Append("<td class=pagetrue><a href=" + Mypostbackurl + "?" + str + "=" + num5.ToString() + " title=第" + num5.ToString() + "页>" + num5.ToString() + "</a></td>\r\n");
}
else
{
builder.Append("<td class=pagefalse><a href=" + Mypostbackurl + "?" + str + "=" + num5.ToString() + " title=第" + num5.ToString() + "页>" + num5.ToString() + "</a></td>\r\n");
}
}
num3 = ((num2 / Mypageshowsize) + 1) * Mypageshowsize;
builder.Append("<td class=pagefalse><a href=" + Mypostbackurl + "?" + str + "=" + num3.ToString() + " title=下一组>.....</a></td>\r\n");
}
builder.Append("<td><select id=\"MySelectPage\" onchange=\"window.location='" + Mypostbackurl + "?" + str + "='+this.options[this.selectedIndex].value\">\r\n");
for (int i = 1; i < (num + 1); i++)
{
if ((HttpContext.Current.Request.QueryString[Myurlpage] != null) && (HttpContext.Current.Request.QueryString[Myurlpage].ToString() == i.ToString()))
{
builder.Append(string.Concat(new object[] { "<option value=", i, " selected>", i, "页</option>\r\n" }));
}
else
{
builder.Append(string.Concat(new object[] { "<option value=", i, ">", i, "页</option>\r\n" }));
}
}
builder.Append("</select></td>\r\n");
builder.Append(string.Concat(new object[] { "<td class=pagefalse><a href=", Mypostbackurl, "?", str, "=", num, " title=最后一页>最后一页</a></td>\r\n" }));
}
else
{
builder.Append("<td>抱歉没有数据</td>");
}
builder2.Append("<style>\r\n");
builder2.Append(".pageborder A:active,.pageborder A:hover,.pageborder A:visited,.pageborder A:link {\r\n");
builder2.Append("DISPLAY: block;COLOR: #686868;TEXT-DECORATION: none;font-size:14px;text-align:center;\r\n");
builder2.Append("border: 1px solid " + Mybordercolor + ";}\r\n");
builder2.Append(".pagefalse A:link,.pagefalse A:visited,.pagefalse A:active,.pagefalse A:hover {padding:3px;}\r\n");
builder2.Append(".pagetrue A:link,.pagetrue A:visited,.pagetrue A:active,.pagetrue A:hover {\r\n");
builder2.Append("padding:5px;background-color:" + Mycolor + ";color:" + Myfontcolor + ";}\r\n");
builder2.Append("</style>\r\n");
builder2.Append("<table cellspacing=0 class=pageborder>\r\n");
builder2.Append("<tr>\r\n");
builder2.Append(builder.ToString() + "\r\n");
builder2.Append("</tr>\r\n");
builder2.Append("</table>\r\n");
return builder2.ToString();
}
{
int num;
int num2;
StringBuilder builder = new StringBuilder();
string str = Meurlstr + Meurlpage;
if ((HttpContext.Current.Request.QueryString[Meurlpage] != null) && int.TryParse(HttpContext.Current.Request.QueryString[Meurlpage], out num))
{
num2 = Convert.ToInt32(HttpContext.Current.Request.QueryString[Meurlpage]);
}
else
{
num2 = 1;
}
int num3 = Mcount / pagesize;
if (num3 == 0)
{
num3 = 1;
}
else if ((Mcount % pagesize) > 0)
{
num3++;
}
builder.Append("<table><tr>");
builder.Append("<td>");
builder.Append("共有条" + Mcount.ToString() + "记录 ");
builder.Append(string.Concat(new object[] { "当前是第", num2, "/", num3, "页 " }));
if (num3 == 1)
{
builder.Append("首页 ");
builder.Append("上一页 ");
builder.Append("下一页 ");
builder.Append("最后一页");
}
else if (num2 == 1)
{
builder.Append("首页 ");
builder.Append("上一页 ");
builder.Append(string.Concat(new object[] { "<a href=", Mepostbackurl, "?", str, "=", num2 + 1, ">下一页</a> " }));
builder.Append(string.Concat(new object[] { "<a href=", Mepostbackurl, "?", str, "=", num3, ">最后一页</a>" }));
}
else if (num2 >= num3)
{
builder.Append("<a href=" + Mepostbackurl + "?" + str + "=1>首页</a> ");
builder.Append(string.Concat(new object[] { "<a href=", Mepostbackurl, "?", str, "=", num2 - 1, ">上一页</a> " }));
builder.Append("下一页 ");
builder.Append("最后一页");
}
else
{
builder.Append("<a href=" + Mepostbackurl + "?" + str + "=1>首页</a> ");
builder.Append(string.Concat(new object[] { "<a href=", Mepostbackurl, "?", str, "=", num2 - 1, ">上一页</a> " }));
builder.Append(string.Concat(new object[] { "<a href=", Mepostbackurl, "?", str, "=", num2 + 1, ">下一页</a> " }));
builder.Append(string.Concat(new object[] { "<a href=", Mepostbackurl, "?", str, "=", num3, ">最后一页</a>" }));
}
builder.Append("</td>");
builder.Append("<td><select id=\"MeSelectPage\" onchange=\"window.location='" + Mepostbackurl + "?" + str + "='+this.options[this.selectedIndex].value\">\r\n");
for (int i = 1; i < (num3 + 1); i++)
{
if ((HttpContext.Current.Request.QueryString[Meurlpage] != null) && (HttpContext.Current.Request.QueryString[Meurlpage].ToString() == i.ToString()))
{
builder.Append(string.Concat(new object[] { "<option value=", i, " selected>", i, "页</option>\r\n" }));
}
else
{
builder.Append(string.Concat(new object[] { "<option value=", i, ">", i, "页</option>\r\n" }));
}
}
builder.Append("</select></td>\r\n");
builder.Append("</tr></table>");
return builder.ToString();
}
(
@metabname varchar(200), --表名
@mepagesize int, --每页显示记录数
@mepageint int, --分页条显示个数
@thispage int, --当前页编号 @msrsall int output, --总记录数
@mspagecount int output --总页数
)
AS
declare @tabsql nvarchar(200)
declare @pagecount int --分页总数
declare @rsall int --总记录数
declare @top int --提当前段数据
declare @sql varchar(200)
------------------------------------------------------------------------------------
set @tabsql='select @rsall =count(*) from '+ @metabname
exec sp_executesql @tabsql,N'@rsall int output',@rsall output
------------------------------------------------------------------------------------
if @rsall >0 --有数据
begin
set @top=@mepagesize*(@thispage-1)
set @sql='select top ' + str(@mepagesize) + ' * from '+@metabname+' where id not in(select top '+ str(@top) +' id from '+@metabname+') order by id asc'
exec(@sql)
if @rsall % @mepagesize=0
begin
set @pagecount=@rsall / @mepagesize
end
else
begin
set @pagecount=@rsall / @mepagesize+1
end
end
else --没数据
begin
set @msrsall=0
set @mspagecount=0
end
set @msrsall=@rsall --总信息数
set @mspagecount=@pagecount --分页总数
GO
/// <summary>
/// 存储过程
/// </summary>
/// <param name="webcons">WebControl绑定控件</param>
/// <param name="dbtable">表名</param>
/// <param name="pagesize">每页显示记录数</param>
/// <param name="showsize">每页显示分页数</param>
/// <param name="thispage">当前页索引</param>
/// <returns>分页字串</returns>
public string GetPorc(WebControl webcons,string dbtable,int pagesize,int showsize,int thispage)
{
MAYI.Mpage Mypage = new MAYI.Mpage();
Open();
SqlCommand cmd = new SqlCommand("Mypage", Connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@metabname", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@mepagesize", SqlDbType.Int);
cmd.Parameters.Add("@mepageint", SqlDbType.Int);
cmd.Parameters.Add("@thispage", SqlDbType.Int); cmd.Parameters["@metabname"].Value = "news";
cmd.Parameters["@mepagesize"].Value = pagesize;
cmd.Parameters["@mepageint"].Value = showsize;
cmd.Parameters["@thispage"].Value = thispage;
cmd.Parameters.Add("@msrsall", SqlDbType.Int);
cmd.Parameters.Add("@mspagecount", SqlDbType.Int); cmd.Parameters["@msrsall"].Direction = ParameterDirection.Output;
cmd.Parameters["@mspagecount"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
if (webcons is GridView)
{
(webcons as GridView).DataSource= ds.Tables[0].DefaultView;
(webcons as GridView).DataBind();
} if (webcons is DataList)
{
(webcons as DataList).DataSource = ds.Tables[0].DefaultView;
(webcons as DataList).DataBind();
} string pagestr = Mypage.MyPage(Convert.ToInt32(cmd.Parameters["@msrsall"].Value), pagesize, showsize, null, null, "pp", null, null, null);
return pagestr;
Close();
}
http://topic.csdn.net/u/20081130/11/f52eebe9-e6f9-46de-b63a-f637b83aa944.html
很不错的学习地方
可以 参考
http://blog.csdn.net/hertcloud/category/281167.aspx
//定义全局变量用来保存每页的起始项索引
int startIndex=0;
private void Page_Load(object sender, System.EventArgs e)
{
//页面初试化时进行数据绑定
if(!IsPostBack)
DataGridDataBind();
}
private void DataGridDataBind()
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
//创建数据适配器对象
SqlDataAdapter da = new SqlDataAdapter("select LastName,FirstName,BirthDate,City from Employees",conn);
//创建DataSet对象
DataSet ds = new DataSet();
try
{
//从指定的索引开始取PageSize条记录
da.Fill(ds,startIndex,dgCustomPage.PageSize,"CurDataTable");
//填充数据集
da.Fill(ds,"AllDataTable");
//设置DataGrid控件实际要显示的项数
dgCustomPage.VirtualItemCount = ds.Tables["AllDataTable"].Rows.Count;
//进行数据绑定
dgCustomPage.DataSource = ds.Tables["CurDataTable"];
dgCustomPage.DataBind();
}
catch(Exception error)
{
Response.Write(error.ToString());
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgCustomPage.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgCustomPage_PageIndexChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void dgCustomPage_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
//设置DataGrid当前页的索引值为用户选择的页的索引
dgCustomPage.CurrentPageIndex = e.NewPageIndex;
//取得当前页为止总共有多少条记录,以便在下一页就从该记录开始读取
startIndex = dgCustomPage.PageSize * dgCustomPage.CurrentPageIndex;
//重新绑定数据
DataGridDataBind();
}
string connString = ConfigurationManager.AppSettings["conn"];
private void pgupdown()
{
pgsize = pagesize();
string strsql = "";
string page = ""; try
{
page = Request.QueryString["page"];
string mysql;
int nextcol = 1;
if (page != "")
nextcol = int.Parse(page) * pgsize - (pgsize - 1);
else
page = "1";
mysql = "SELECT TOP " + pgsize + " * FROM guest WHERE (ID <= (SELECT MIN(id) FROM (SELECT TOP " + nextcol.ToString() + " id FROM guest ORDER BY id desc) AS T)) ORDER BY ID desc";
strsql = "select count(*) as number from guest";
dblist(mysql); if (!Page.IsPostBack)
{
firpg.NavigateUrl = "../guestbooklist.aspx?page=1";
if (conum(strsql) % pgsize == 0)
pgcont = conum(strsql) / pgsize;
else
pgcont = conum(strsql) / pgsize + 1;
}
////////////////////////////////////////////////////////
if (int.Parse(page) == pgcont) //判断下一页
{
nextpg.Enabled = false;
endpg.Enabled = false;
}
else
{
nextpg.NavigateUrl = "../guestbooklist.aspx?&page=" + (int.Parse(page) + 1);
endpg.NavigateUrl = "../guestbooklist.aspx?&page=" + pgcont;
}
/////////////////////////////////////////////////////////
if (int.Parse(page) == 1) //判断上一页
{
copg.Enabled = false;
firpg.Enabled = false;
}
else
copg.NavigateUrl = "../guestbooklist.aspx?&page=" + (int.Parse(page) - 1);
pgmsg.Text = "第" + page + "页/共" + pgcont + "页(每页" + pgsize + "条共" + conum(strsql) + "条)";
}
catch (Exception ex)
{
Response.Write( ex.Message);
} } void dblist(string sql)
{
SqlConnection conn = new SqlConnection(connString);
DataSet ds = new DataSet();
conn.Open();
SqlDataAdapter sqldap = new SqlDataAdapter(sql, conn);
sqldap.Fill(ds, "tables");
guestbook.DataSource = ds.Tables["tables"].DefaultView;
guestbook.DataBind();
conn.Close();
conn = null;
}
int conum(string sql)
{
SqlConnection conn = new SqlConnection(connString);
SqlCommand mycmd = new SqlCommand();
conn.Open();
mycmd.Connection = conn;
mycmd.CommandText = sql; //
int n = (int)mycmd.ExecuteScalar();
conn.Close();
conn = null;
return n;
}
楼主.不要听他们乱来.既然学了.就要学精.相信你..呵呵..喝多了..看到一些人的回复狠是不爽.都说中国人制造不出原创.这能制造出来么??全是用别人的.根本没有想过diy..