我想适当加些代码,用以下功能代码写一个web分页控件。希望大虾们帮帮我啊!

凤凰涅檠 2007-07-10 04:54:35
这是一个简单的分页代码:

aspx:
<table width="100%">
<tr>
<td width="100%" style="text-align: right">
 <span style="font-size: 10pt">总共:</span><asp:Label ID="lblRecordCount" runat="server"
ForeColor="red"></asp:Label>条记录 当前:<asp:Label ID="lblCurrentPage" runat="server"
ForeColor="Red"></asp:Label>/<asp:Label ID="lblPageCount" runat="server" ForeColor="Red"></asp:Label>页
转到: <asp:DropDownList ID="Ddl_PageNumber" runat="server" AutoPostBack="true"
CssClass="lanyu">
</asp:DropDownList>页
<asp:LinkButton ID="BtnFirst" runat="server" CommandName="First" Font-Size="10pt"
OnCommand="Page_OnClick" Text="首页">首页</asp:LinkButton>
<asp:LinkButton ID="lbnPrevPage" runat="server" CommandName="prev" Font-Size="10pt"
OnCommand="Page_OnClick" Text="上一页"></asp:LinkButton> 
<asp:LinkButton ID="lbnNextPage" runat="server" CommandName="next" Font-Size="10pt"
OnCommand="Page_OnClick" Text="下一页"></asp:LinkButton>
<asp:LinkButton ID="BtnLast" runat="server" CommandName="Last" Font-Size="10pt" OnCommand="Page_OnClick"
Text="尾页">尾页</asp:LinkButton>
</td>
</tr>
</table>

cs:
public partial class data_updGoods : System.Web.UI.Page
{
string constr = (ConfigurationManager.ConnectionStrings["jxcConnectionString"]).ToString();
int PageSize, RecordCount, PageCount, CurrentPage, i;
ArrayList Al_PageNum;

public void Page_Load(Object src, EventArgs e)
{
readnum();
}
public void readnum()
{
PageSize = 6; //设定PageSize
SqlConnection MyConn = new SqlConnection(constr);
MyConn.Open();
if (!IsPostBack) //第一次请求执行
{
RecordCount = CalculateRecord(); //计算总共有多少记录/

PageCount = RecordCount / PageSize; //计算总共有多少页
if (RecordCount % PageSize > 0) //取整
{
PageCount = PageCount + 1;
}
lblPageCount.Text = PageCount.ToString();
lblRecordCount.Text = RecordCount.ToString();
ViewState["PageCount"] = PageCount;
CurrentPage = 0;
ViewState["PageIndex"] = 0;

Al_PageNum = new ArrayList();//绑定DROPDOWNLIST
for (i = 1; i <= PageCount; i++) //从1开始循环,为了不出现0页码
{
Al_PageNum.Add(i.ToString());
}
Ddl_PageNumber.DataSource = Al_PageNum;
Ddl_PageNumber.DataBind();
ListBind(); //绑定
}
MyConn.Close();
}
public int CalculateRecord() //计算总共有多少条记录
{
SqlConnection MyConn = new SqlConnection(constr);
int intCount;
string strCount = "select count(*) as co from Goods";
SqlCommand MyComm = new SqlCommand(strCount, MyConn);
MyConn.Open();
SqlDataReader dr = MyComm.ExecuteReader();
if (dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
MyConn.Close();
return intCount;
}

public void ListBind()
{
SqlConnection MyConn = new SqlConnection(constr);
int StartIndex; //设定导入的起终地址
StartIndex = CurrentPage * PageSize; //计算记录数的起始点
string strSel = "select * from Goods";
DataSet ds = new DataSet();
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, MyConn);
MyAdapter.Fill(ds, StartIndex, PageSize, "Goods");
GridView1.DataSource = ds.Tables["Goods"].DefaultView;
GridView1.DataBind();

lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
BtnFirst.Enabled = true;
BtnLast.Enabled = true;
if (PageCount == 0)
{
lblCurrentPage.Text = "0";
lbnNextPage.Enabled = false;
lbnPrevPage.Enabled = false;
BtnFirst.Enabled = false;
BtnLast.Enabled = false;
}
else
{
if (CurrentPage == (PageCount - 1))
{
lbnNextPage.Enabled = false;
BtnLast.Enabled = false;
}
if (CurrentPage == 0)
{
lbnPrevPage.Enabled = false;
BtnFirst.Enabled = false;
}
lblCurrentPage.Text = (CurrentPage + 1).ToString();
}
Ddl_PageNumber.Text = lblCurrentPage.Text;
}

public void Page_OnClick(Object sender, CommandEventArgs e)//上一页,下一页的单击代码
{
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];
string cmd = e.CommandName; //判断cmd,以判定翻页方向
switch (cmd)
{
case "next":
if (CurrentPage < (PageCount - 1)) CurrentPage++;
break;
case "prev":
if (CurrentPage > 0) CurrentPage--;
break;
case "Last":
CurrentPage = (PageCount - 1);
break;
default:
CurrentPage = 0;
break;
}

ViewState["PageIndex"] = CurrentPage;
ListBind();

}
public void PageNum_SelectIndexChanged(object sender, System.EventArgs e)
{
ViewState["PageIndex"] = int.Parse(Ddl_PageNumber.SelectedItem.Value) - 1;//保持不出现0页码
//PageSize = 3;
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];
ListBind();
}

override protected void OnInit(EventArgs e)
{
this.Load += new System.EventHandler(this.Page_Load);
this.Ddl_PageNumber.SelectedIndexChanged += new System.EventHandler(this.PageNum_SelectIndexChanged);
base.OnInit(e);
}
}

我没写过控件,希望借此学习如何自己编写自定义的控件。我会认真研究大侠们给的代码,希望大侠们多多帮助!
...全文
162 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
milo4210 2007-07-18
  • 打赏
  • 举报
回复
JF
weiang_1983 2007-07-18
  • 打赏
  • 举报
回复
接分。
楼主还是结贴吧,版主是不会帮你删掉的。
凤凰涅檠 2007-07-18
  • 打赏
  • 举报
回复
版主,这个问题我可以不问吗?我已经用不到了。。。请删贴。
  • 打赏
  • 举报
回复
路过先UP下。再看
凤凰涅檠 2007-07-10
  • 打赏
  • 举报
回复
帮顶者有分!
凤凰涅檠 2007-07-10
  • 打赏
  • 举报
回复
我自己人先顶下

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧