GridView+存储过程实现自定义分页!其实很简单,主要是怎么保存当前页面的页码PageIndex问题,不过把这个解决了什么都好办了.因为在分页过程中:PageSize是一定的,我们可以用一个属性[GridView1.PageSize]来表示即可.保存PageIndex好多中方法,而且数据不是很庞大,基本不会好太多的资源.在这里使用ViewState来保存当前的PageIndex和PageCount[总页数].还是一句老话,话再多都没有例子直观.
在这里我还有一个疑惑:第一次读入的时候是PageIndex为0,但是点击首页的时候,为什么非得是1呢?
存储过程里的判断条件是if(PageIndex=1)啊!
这个问题搞不明白.但是放心,这种分页是绝对能出来的.我以例子保证.*_*
存储过程:
CREATE
PROCEDURE
sp_PagerAD
(
@adAddressId
varchar
(
50
),
--
广告位编号
@PageSize
int
,
--
一页显示记录数
@PageIndex
int
--
当前页码(从1开始)
)
as
declare
@strSQL
nvarchar
(
1000
)
--
主语句
--
如果广告位编号为空,则显示全部信息
if
(
@adAddressId
=
'
NO
'
)
begin
--
如果只有一页
if
(
@PageIndex
=
1
)
begin
set
@strSQL
=
"
select
top
"
+
str
(
@PageSize
)
+
"
*
from
ad
order
by
id"
--
一定要order by,否则数据读取可能出错
end
--
如果不只一页
else
if
(
@PageIndex
>
1
)
begin
set
@strSQL
=
"
select
top
"
+
str
(
@Pagesize
)
+
"
*
from
ad
where
id
not
in
(
select
top
"
+
str
(
@PageSize
*
(
@PageIndex
-
1
))
+
"
id
from
ad
order
by
id)
order
by
id"
end
end

--
如果广告位编号不为空,则显示对应信息
else
begin
--
和上面差不多的做法
end
exec
(
@strSQL
)
GO
CS文件:
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Data.SqlClient;

public
partial
class
Admin_List_AD : System.Web.UI.Page

...
{
SqlConnection conn;
SqlCommand mycmd;
GetData GD = new GetData();
DataSet ds = new DataSet();
CheckBox cb;
string sql;

protected void Page_Load(object sender, EventArgs e)

...{
//判断Cookies["admin"]["adminName"];Cookies["admin"]["adminId"]是否存在
if (Request.Cookies["admin"] == null)

...{
Response.Redirect("AdminLogin.aspx");
}
//判断Cookies["admin"]是否存在
else if (Request.Cookies["admin"]["adminName"] == null && Request.Cookies["admin"]["adminId"] == null)

...{
Response.Redirect("AdminLogin.aspx");
}
else

...{//保存用户名和用户编号
ViewState["adminName"] = Request.Cookies["admin"]["adminName"].ToString();
ViewState["adminId"] = Request.Cookies["admin"]["adminId"].ToString();
}

//启动智能导航
Page.SmartNavigation = true;
if (!IsPostBack)

...{
ViewState["adAddressId"] = "";
if (Request.QueryString["adAddressId"] != null)

...{
ViewState["adAddressId"] = Request.QueryString["adAddressId"].ToString();
}//获取当前页,总页数
ListADInfo(ViewState["adAddressId"].ToString());
SearchProduct(ViewState["adAddressId"].ToString(),GridView1.PageSize,0);
}
}

public void ListADInfo(string adAddressId)

...{
if (adAddressId == "")

...{
sql = "select * from ad";
}
else

...{
sql = "select * from ad where id = " + adAddressId;
}
ds = GD.DataBind(sql);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();

//总页数
LblPageCount.Text = GridView1.PageCount.ToString();
ViewState["PageCount"] = GridView1.PageCount.ToString();
//当前页
LblCurrentIndex.Text = Convert.ToString(GridView1.PageIndex + 1);
ViewState["pageIndex"] = Convert.ToString(GridView1.PageIndex + 1);
//判断首页,上一页,下一页,尾页是否可用;
CheckPageCount();
}

/**//// <summary>
/// 查找所有区域信息
/// </summary>
/// <param name="adAddressId">广告位编号</param>
/// <param name="PageSize">每页数量</param>
/// <param name="CurrentPage">当前页码</param>
public void SearchProduct(string adAddressId, int PageSize, int CurrentPage)

...{
conn = GD.Conn();
conn.Open();
if (adAddressId == "")

...{
adAddressId = "NO";
}
//调用存储过程
mycmd = new SqlCommand("sp_PagerAD", conn);
mycmd.CommandType = CommandType.StoredProcedure;
//每页数量
mycmd.Parameters.Add(new SqlParameter("@PageSize", SqlDbType.Int));
mycmd.Parameters["@PageSize"].Value = PageSize;

//当前页码
mycmd.Parameters.Add(new SqlParameter("@PageIndex", SqlDbType.Int));
mycmd.Parameters["@PageIndex"].Value = CurrentPage;

//广告位编号
mycmd.Parameters.Add(new SqlParameter("@adAddressId", SqlDbType.VarChar, 50));
mycmd.Parameters["@adAddressId"].Value = adAddressId;

//在这里只能用DataSet,不可以用ExecuteReader,因为他是按顺序读取,不支持分页
SqlDataAdapter sda = new SqlDataAdapter(mycmd);
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();


/**/////总页数
LblPageCount.Text = ViewState["PageCount"].ToString();


/**/////当前页
LblCurrentIndex.Text = (Convert.ToInt32(ViewState["pageIndex"].ToString())).ToString();

/**/////判断首页,上一页,下一页,尾页是否可用;
CheckPageCount();
}

//如果当前只有一页,则四者皆不可用,如果当前页为1,则首页不可用,如果当前页为未页,则尾页不能用.
public void CheckPageCount()

...{
first.Enabled = true;
prev.Enabled = true;
next.Enabled = true;
Last.Enabled = true;
int i = Convert.ToInt32(ViewState["PageCount"].ToString());
//如果只有一页
if (i == 1)

...{
first.Enabled = false;
prev.Enabled = false;
next.Enabled = false;
Last.Enabled = false;
}
else if (Convert.ToInt32(ViewState["pageIndex"].ToString()) == 1)

...{
first.Enabled = false;
prev.Enabled = false;
}
if (Convert.ToInt32(ViewState["pageIndex"].ToString()) == Convert.ToInt32(ViewState["PageCount"].ToString()))

...{
Last.Enabled = false;
next.Enabled = false;
}
}

//首页
protected void first_Click(object sender, EventArgs e)

...{
ViewState["pageIndex"] = 1;
SearchProduct(ViewState["adAddressId"].ToString(), GridView1.PageSize, 1);
}
//上一页
protected void prev_Click(object sender, EventArgs e)

...{
if (Convert.ToInt32(ViewState["pageIndex"].ToString()) > 0)

...{
ViewState["pageIndex"] = Convert.ToInt32(ViewState["pageIndex"].ToString()) - 1;
SearchProduct(ViewState["adAddressId"].ToString(), GridView1.PageSize, Convert.ToInt32(ViewState["pageIndex"].ToString()));
}
}
//下一页
protected void next_Click(object sender, EventArgs e)

...{
if (Convert.ToInt32(ViewState["pageIndex"].ToString()) < Convert.ToInt32(ViewState["PageCount"].ToString()))

...{
ViewState["pageIndex"] = Convert.ToInt32(ViewState["pageIndex"].ToString()) + 1;
SearchProduct(ViewState["adAddressId"].ToString(), GridView1.PageSize, Convert.ToInt32(ViewState["pageIndex"].ToString()));
}
}
//尾页
protected void Last_Click(object sender, EventArgs e)

...{
ViewState["pageIndex"] = Convert.ToInt32(ViewState["PageCount"].ToString());
SearchProduct(ViewState["adAddressId"].ToString(), GridView1.PageSize, Convert.ToInt32(ViewState["pageIndex"].ToString()));
}


/**//// <summary>
/// 批量删除所选商品
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
int ADId;
protected void Button3_Click(object sender, EventArgs e)

...{
for (int i = 0; i < GridView1.Rows.Count; i++)

...{
IsCheckBox(i);
if (cb.Checked)

...{
GD.OperateAD(ADId, "DelAD");
}
}
//重新绑定
SearchProduct(ViewState["adAddressId"].ToString(), GridView1.PageSize, Convert.ToInt32(ViewState["pageIndex"].ToString()));
}
private void IsCheckBox(int i)

...{
cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
ADId = Convert.ToInt32(GridView1.Rows[i].Cells[1].Text.ToString());
}
}

注明:原帖地址:http://blog.youkuaiyun.com/Love1221/archive/2007/06/14/1651551.aspx