ASP.NET 分页如何调用?
create proc getMemberList
@viewTable varchar(50),--要显示的表或视图
@sqlString varchar(300), --查询语句
@viewField varchar(200),--字段(多个用正中逗号隔开)
@pagesize int, --每页大小
@showPage int --当前页
As
declare @jilu bigint
set @showPage=@showPage-1
set @jilu=@pagesize*@showPage
if (@viewField= ' ')
begin
set @viewField= '* '
end
declare @sql nvarchar(200)
set @sql=N 'select top '+convert(varchar(10),@pagesize)+ ' '+convert(varchar(200),@viewField)+ ' from '+convert(varchar(50),@viewTable)
set @sql=@sql+ ' where id not in (select top ' +convert(varchar(10),@jilu)+ ' id from '+convert(varchar(50),@viewTable)+ ' where 1=1 '+@sqlString+ ' order by id) ' +@sqlString+ ' order by id '
exec sp_executesql @sql
return
.net 函数
public DataTable GetMemberList(string viewTable,string sqlString,string viewField,int pageSize,int showPage)
{
DataTable ds = new DataTable();
SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataAdapter da = null;
try
{
conn = new _DB().SqlConDb();
cmd = new SqlCommand( "getMemberList ", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add( "@viewTable ", SqlDbType.VarChar).Value = viewTable;
cmd.Parameters.Add( "@sqlString ", SqlDbType.VarChar).Value = sqlString.ToString();
cmd.Parameters.Add( "@viewField ", SqlDbType.VarChar).Value = viewField;
cmd.Parameters.Add( "@pagesize ", SqlDbType.Int).Value = pageSize;
cmd.Parameters.Add( "@showPage ", SqlDbType.Int).Value = showPage;
da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
cmd.Dispose();
conn.Dispose();
conn.Close();
}
return ds;
}
当这样调用用时就正常
GetMemberList(“表”, " and u_name like '% " + u_name + "% ' ", “*“, 20, 1);
我现在问题是:在.net 中如何调用
比如 上一页 下一页 首页 页面 ,如何调用?
我感觉比较迷茫?/ 谢谢 各位能提供思路…………
[解决办法]
上一页 下一页 首页
@showPage int --当前页 传的参数变一下就可以了
首页就是1
上一页,下一页,如果用的是gridview的话就是e.NewPageIndex
[解决办法]
using System;
using System.Data;
using System.Configuration;
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;
/// <summary>
/// GridViewPager 为gridview提供分页的基本方法
/// </summary>
///
public class GridViewPager
{
private GridView _gv=null;
public GridView gv
{
get { return _gv; }
set { _gv = value; }
}
private DataTable _dt=null;
public DataTable dt
{
get { return _dt; }
set { _dt = value; }
}
private int _index = 0;
public int index
{
get { return _index; }
set { _index = value; }
}
/// <summary>
/// 确定已经指定了dt和要分页的gridview
/// </summary>
public void BindGridView()
{
_gv.DataSource = _dt;
_gv.DataBind();
GridView theGrid = _gv;
GridViewRow pagerRow = theGrid.BottomPagerRow;
if (pagerRow != null)
{
LinkButton btnFirst = pagerRow.FindControl( "btnFirst ") as LinkButton;
LinkButton btnPrev = pagerRow.FindControl( "btnPrev ") as LinkButton;
LinkButton btnNext = pagerRow.FindControl( "btnNext ") as LinkButton;
LinkButton btnLast = pagerRow.FindControl( "btnLast ") as LinkButton;
if (btnFirst != null && btnPrev != null && btnNext != null && btnLast != null)
{
if (_index < 0 || _index == 0)
{
btnFirst.Enabled = false;
btnPrev.Enabled = false;
}
else
{
btnFirst.Enabled = true;
btnPrev.Enabled = true;
}
if (_index > = _gv.PageCount - 1)
{
btnNext.Enabled = false;
btnLast.Enabled = false;
}
else
{
btnNext.Enabled = true;
btnLast.Enabled = true;
}
}
}
}
/// <summary>
/// 确定已经指定了gridview,dt,和index
/// </summary>
public void pager()
{
GridView theGrid = _gv;
int newPageIndex = 0;
GridViewRow pagerRow = theGrid.BottomPagerRow;
if (_index == -2)
{ // when click the "GO " Button
TextBox txtNewPageIndex = null;
if (null != pagerRow)
{
txtNewPageIndex = pagerRow.FindControl( "txtNewPageIndex ") as TextBox;
}
if (null != txtNewPageIndex)
{
if (int.TryParse(txtNewPageIndex.Text.Trim(), out newPageIndex))
{
_index=newPageIndex = newPageIndex - 1;
}
//newPageIndex = _index=int.Parse(txtNewPageIndex.Text) - 1;
}
}
else
{ // when click the first, last, previous and next Button,绑定自定义dt必备项
newPageIndex = _index;
}
newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
newPageIndex = newPageIndex > = theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
theGrid.PageIndex = newPageIndex;
BindGridView();
}
}
调用
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 DB;
public partial class Default3 : System.Web.UI.Page
{
data sqldata = new data();
DataTable dt = new DataTable();
GridViewPager gvp = new GridViewPager();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
public DataTable GetTable()
{
string strTxt = "SELECT * FROM FunctionModule ";
dt = sqldata.GetTable(strTxt);
return dt;
}
public void BindGridView()
{
gvp.gv = this.GridView1;
gvp.dt = GetTable();
gvp.BindGridView();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvp.index = e.NewPageIndex;
gvp.gv = sender as GridView;
gvp.dt = GetTable();
gvp.pager();
}
}
[解决办法]
数据不多用自动分页就行了,改变下INDEX