c#如何分页
用vs2005中windows应用程序中如何用c#写分页,存储过程怎么写?刚学.net新手,望大家多多照顾,谢谢!视图中有第一页、上一页、下一页、最后一页,其中pagecount.Value =?才能实现点下一页能翻页呢?pagecount是页码的参数 --------------------编程问答-------------------- 在线等哦 --------------------编程问答-------------------- 控件就有啊 --------------------编程问答-------------------- 初学多查查MSDN. --------------------编程问答-------------------- GridView 帮定数据就能实现分页呀 --------------------编程问答-------------------- 存储过程分页,要传入当前页数,每页个数二个参数,利用数据库自动加一或ORACLE中的ROWID来提取某一页的数据。同时,还要传出符合条件的总数据个数,来让前台的控件来显示上一页,下一页,首页、尾页 --------------------编程问答-------------------- 这个问题,从2000年,一直问到了2008年。
中国的程序员就这么不长劲? --------------------编程问答-------------------- 楼上过于偏激。任何阶段都有初学者。出现问题是肯定的。楼主可以搜索一下论坛或者baidu。 --------------------编程问答-------------------- 分页有2种方式,第一你用存储过程分页,也就是写个存储过程,出入参数需要第几页,就返回给你第几页的数据
第二就是代码这边分页,你先将所有的数据都获取下来,然后用代码分页,根据客户的动作来响应不同的页面
具体选择你需要按实际情况来决定 --------------------编程问答-------------------- 方式1 用控件绑定数据,使用控件的分页功能。
方式2 自己写代码,视数据量一次性取数据再分页,或者直接分页取数据。以前自己写过一个,因为数据量太大,性能不太好。
方式3 用存储过程。这个要注意存储过程的sql优化了,不然一个分页存储过程执行几分钟,这程序使用感受就很差劲了。存储过程的具体写发是数据库的内容,可以找些数据库方面的书看看。
Oracle的存储过程的格式是:
CREATE OR REPLACE PROCEDURE proname (parameter OUT/IN TYPE)
AS
--变量定义
t_num number;
BEGIN
--过程主体
EXCEPTION
WHEN NO_DATA_FOUND
THEN
--异常处理
WHEN OTHERS
THEN
--异常处理
END; --------------------编程问答-------------------- 很有难度
不是很清楚 --------------------编程问答-------------------- 我贡献一个源代码分页控件
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PublicPageWebSite.ascx.cs" Inherits="UserControl_PublicPageWebSite" %>
<style type="text/css">
.style1 {
margin-bottom: 0px;
}
</style>
<table border="0" cellspacing="0" cellpadding="0" style="width: 93%; margin-left:15px; height: 3px;" class="style1">
<tr>
<td style="width: 22%" > <asp:Label ID="cpnum" runat="server" Text="Label"></asp:Label>
/
<asp:Label ID="total" runat="server" Text="Label"></asp:Label>
<asp:LinkButton ID="First" runat="server" CommandName="first" OnCommand="link_Command">首页</asp:LinkButton>
<asp:LinkButton ID="Prev" runat="server" CommandName="prev" OnCommand="link_Command">
上一页</asp:LinkButton></td>
<td style="text-align: center; width: 33%;">
<asp:Panel ID="Panel1" runat="server" Height="18px" Width="88%">
</asp:Panel>
</td>
<td style="width: 13%"><asp:LinkButton ID="Next" runat="server" CommandName="next" OnCommand="link_Command">
下一页</asp:LinkButton>
<asp:LinkButton ID="Last" runat="server" CommandName="last" OnCommand="link_Command">末页</asp:LinkButton></td>
<td style="text-align: right; vertical-align: inherit; width: 11%;" valign="baseline">跳转到:</td>
<td style="text-align: left; width: 20%; vertical-align: inherit;" valign="baseline"><asp:TextBox ID="pageno" runat="server" Width="37px" ValidationGroup="pagelist"></asp:TextBox><asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="pageno"
Display="Dynamic" ErrorMessage="RegularExpressionValidator" ValidationExpression="[0-9]+"
ValidationGroup="pagelist">*</asp:RegularExpressionValidator><asp:RequiredFieldValidator
ID="RequiredFieldValidator1" runat="server" ControlToValidate="pageno" Display="Dynamic"
ErrorMessage="RequiredFieldValidator" ValidationGroup="pagelist">
*</asp:RequiredFieldValidator>
<asp:Button ID="go" runat="server" Text="GO" OnClick="go_Click" ValidationGroup="pagelist" /></td>
</tr>
</table>
--------------------编程问答--------------------
--------------------编程问答--------------------
/// <summary>
/// 选择的控件类型
/// </summary>
public enum SelControl
{
/// <summary>
/// GridView控件类型
/// </summary>
GridView,
/// <summary>
/// DataList控件类型
/// </summary>
DataList
}
/// <summary>
/// //分页底部页码用户控件(只能用在页面层(不能用到ajax里面))
/// </summary>
public partial class UserControl_PublicPageWebSite : System.Web.UI.UserControl
{
//属性部分
int _RecordCount;
SelControl _CountrolType;
string _CountrolName;
/// <summary>
/// 设置总记录数
/// </summary>
public int RecordCount
{
set
{
if (ViewState["RecordCount"] == null || Convert.ToInt32(ViewState["RecordCount"]) != value)
_RecordCount = value;
}
}
/// <summary>
/// 设置控件类型
/// </summary>
public SelControl CountrolType
{
set
{
if (ViewState["CountrolType"] == null)
_CountrolType = value;
}
}
/// <summary>
/// 设置控件(Gridview或者Datalist的id)
/// </summary>
public string CountrolName
{
set
{
//if (ViewState["CountrolName"] == null || ViewState["CountrolName"].ToString() != value)
_CountrolName = value;
}
}
//其他属性
string _TBName, _KeyField, _KeyAscDesc, _Fields, _Condition, _Order;
int _PageSize, _CurPage;
//属性部分
/// <summary>
/// 表名或者视图(必填)
/// </summary>
public string TBName
{
set { _TBName = value; }
get { return _TBName; }
}
/// <summary>
/// 关键字段(选择填写,默认是ID,如果不是ID,请填写)
/// </summary>
public string KeyField
{
set
{
if (value != "")
_KeyField = value; }
get { return _KeyField; }
}
/// <summary>
/// 关键字排列方式(Asc或者Desc,默认Asc)(选填)
/// </summary>
public string KeyAscDesc
{
get { return _KeyAscDesc; }
set
{
if (value != "")
_KeyAscDesc = value; }
}
/// <summary>
/// 所选择的列名,默认为全选(选填)(例如:"id,city")
/// </summary>
public string Fields
{
get { return _Fields; }
set {
if (value != "")
_Fields = value; }
}
/// <summary>
/// where 条件,默认为空(选择)(例如:"id=0 and a=5")
/// </summary>
public string Condition
{
get { return _Condition; }
set { _Condition = value; }
}
/// <summary>
/// 排序条件,默认为空(选填)( order by time desc)
/// </summary>
public string Order
{
set { _Order = value; }
get { return _Order; }
}
/// <summary>
/// 每页的记录数,默认为 10(选填)
/// </summary>
public int PageSize
{
set
{
if (value > 0)
_PageSize = value; }
get { return _PageSize; }
}
/// <summary>
/// 表示当前页 默认1(选填)
/// </summary>
public int CurPage
{
get { return _CurPage; }
set
{
if (value > 0)
_CurPage = value; }
}
/// <summary>
/// 初始化分页函数
/// </summary>
private void PublicTurnPageWebSite()
{
if(_KeyField==null)
_KeyField = "ID";
if (_KeyAscDesc == null)
_KeyAscDesc = "Asc";
if (_Fields == null)
_Fields = "*";
if (_Condition == null)
_Condition = "";
if (_Order == null)
_Order = "";
if (_PageSize == null || _PageSize==0)
_PageSize = 10;
if (_CurPage == null || _CurPage == 0)
_CurPage = 1;
}
/// <summary>
/// 设置好类的属性才调用的方法
/// </summary>
/// <returns></returns>
private DataTable Public_Page()
{
if (ChkAtt())
{
return Public_Page(ProcSql());
}
return null;
}
/// <summary>
/// 执行拼接的sql存储过程
/// </summary>
/// <param name="sql">拼接的sql存储过程的字符串</param>
/// <returns></returns>
private DataTable Public_Page(string sql)
{
//执行存储过程
using (pweb_SQL pwebsql = new pweb_SQL())
{
return pwebsql.ReTable(sql);
}
}
private string ProcSql()
{
return "up_PublicTurnPageWebSite '" + _TBName + "'," + _PageSize + "," + _CurPage + ",'" + _KeyField + "','" + _KeyAscDesc + "','" + _Fields + "','" + _Condition + "','" + _Order + "'";
}
/// <summary>
/// 检测属性是否合法
/// </summary>
/// <returns></returns>
private bool ChkAtt()
{
if(_TBName=="" || _KeyField=="" || _KeyAscDesc=="" || _Fields=="")
{
return false;
}
if (_PageSize < 1 || _CurPage < 1)
{
return false;
}
return true;
}
protected void Page_Load(object sender, EventArgs e)
{
//if (!IsPostBack)
//{
// //ControlBind();
//}
CreateNum();
}
}
/// <summary>--------------------编程问答-------------------- 哇噻,楼上的是在贴分页大全百科还是什么啊!
/// 根据设置了的属性重新绑定
/// </summary>
public void ControlBind()
{
PublicTurnPageWebSite();
SetVievState();
//DataBingControl(1);
//ViewState["CurPage"] = 1;
//CreateNum();
ControlBind(1);
}
/// <summary>
/// 绑定控件
/// </summary>
/// <param name="i">页数</param>
private void ControlBind( int i)
{
GetViewState();
DataBingControl(i);
ViewState["CurPage"] = i;
CreateNum();
}
/// <summary>
/// 设置保全的视图(属性)
/// </summary>
private void SetVievState()
{
ViewState["RecordCount"] = _RecordCount;//总记录数
ViewState["CountrolType"]=_CountrolType;//是Gridview还是datalist
ViewState["CountrolName"] = _CountrolName;//控件的id
ViewState["TBName"] = _TBName;//表名称或者视图名称
ViewState["KeyField"] = _KeyField;//关键字段
ViewState["KeyAscDesc"] = _KeyAscDesc;//关键字段排序
ViewState["Fields"] = _Fields;//要查找的字段
ViewState["Condition"] = _Condition;//where条件
ViewState["Order"] = _Order;//order by 条件
ViewState["PageSize"] = _PageSize;//每页多少记录
ViewState["CurPage"] = _CurPage; //当前页码
int _pagecount = _RecordCount / _PageSize;
if (_RecordCount % _PageSize > 0)
_pagecount++;
ViewState["PageCount"] = _pagecount;
}
/// <summary>
/// 获取视图的属性
/// </summary>
private void GetViewState()
{
//_RecordCount = Convert.ToInt32( ViewState["RecordCount"]);//总记录数
_TBName = ViewState["TBName"].ToString() ;//表名称或者视图名称
_KeyField = ViewState["KeyField"].ToString();//关键字段
_KeyAscDesc = ViewState["KeyAscDesc"].ToString();//关键字段排序
_Fields = ViewState["Fields"].ToString();//要查找的字段
_Condition = ViewState["Condition"].ToString();//where条件
_Order = ViewState["Order"].ToString();//order by 条件
_PageSize =Convert.ToInt32(ViewState["PageSize"]);//每页多少记录
_CurPage =Convert.ToInt32( ViewState["CurPage"]); //当前页码
}
/// <summary>
/// 绑定显示控件
/// </summary>
/// <param name="i">当前页码</param>
private void DataBingControl(int i)
{
_CurPage = i;
_CountrolType =(SelControl) ViewState["CountrolType"];//是Gridview还是datalist
_CountrolName = ViewState["CountrolName"].ToString();//控件的id
Public_Page();
switch (_CountrolType)
{
case SelControl.GridView:
GridView gv = (GridView)Page.FindControl(_CountrolName);
gv.DataSource=Public_Page();
gv.DataBind();
break;
case SelControl.DataList:
DataList dl = (DataList)Page.FindControl(_CountrolName);
dl.DataSource = Public_Page();
dl.DataBind();
break;
}
ViewState["CurPage"] = i;
}
/// <summary>
/// 上一页和下一页和数字页的处理事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void link_Command(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "first":
ControlBind(1);
break;
case "prev":
int i = Convert.ToInt32(ViewState["CurPage"]);
if (i > 1)
i--;
ControlBind(i);
break;
case "next":
i = Convert.ToInt32(ViewState["CurPage"]);
if (i < Convert.ToInt32(ViewState["PageCount"]))
i++;
ControlBind(i);
break;
case "last":
ControlBind(Convert.ToInt32(ViewState["PageCount"]));
break;
default:
ControlBind(Convert.ToInt32(e.CommandName));
break;
}
}
/// <summary>
/// 生成数字页
/// </summary>
private void CreateNum()
{
Panel1.Controls.Clear();
int pagecount = Convert.ToInt32(ViewState["PageCount"]);
int CurPage = Convert.ToInt32(ViewState["CurPage"]);
if (pagecount <= 9)
{
makenum(1, pagecount);
}
else
{
if (CurPage == 1)
makenum(1, 9);
if (CurPage == pagecount)
{
makenum(pagecount - 8, pagecount);
}
if (CurPage > 1 && CurPage < pagecount)
{
if (CurPage < 5)
makenum(1, 9);
else if (pagecount - CurPage < 5)
{
makenum(pagecount - 8, pagecount);
}
else
{
makenum(CurPage - 4, CurPage + 4);
}
}
}
cpnum.Text = CurPage.ToString();
total.Text = pagecount.ToString();
}
private void makenum(int start, int end)
{
for (int i = start; i <= end; i++)
{
LinkButton li = new LinkButton();
li.Text = i.ToString();
li.CommandName = i.ToString();
li.ID = "linkb" + i.ToString();
li.Command += link_Command;
Panel1.Controls.Add(li);
Panel1.Controls.Add(new LiteralControl(" "));
}
}
protected void go_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
ControlBind(int.Parse(pageno.Text));
}
}
补充:.NET技术 , C#