我写的DataGrid分页 却分不了页
我把代码拷给大家,大家看看哪里不对?
public partial class newslist : System.Web.UI.UserControl
{
private string newTypeID;
public string NewTypeID
{
set
{
this.newTypeID = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
SqlConnection con = DB.createCon();
SqlCommand cmd = new SqlCommand( "select newsTypeName from newsType where newsTypeID= ' " + this.newTypeID + " ' ", con);
con.Open();
string newTypeName = Convert.ToString(cmd.ExecuteScalar());
this.Label1.Text = newTypeName;
cmd.CommandText = "select * from newsMaster where newsTypeID= ' " + this.newTypeID + " ' ";
this.DataGrid1.DataSource = cmd.ExecuteReader();
this.DataGrid1.DataBind();
}
}
protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
this.DataGrid1.DataBind();
}
}
[解决办法]
看一下你的 PagerStyle 中设置的对吗?
[解决办法]
你allow paging了吗?
[解决办法]
DataGrid-> 右键-> 属性生成器-> 分页-> 允许分页
------解决方案--------------------
把 allow paging 这个属性设置为true
[解决办法]
LS几位大大说的很清楚了
[解决办法]
问题在分页事件处理程序的代码中:
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
this.DataGrid1.DataBind();
你没有重新给DataGrid绑定数据,而只调用DataBind方法等于什么用也没有,应该把Page_Load中绑定数据的代码独立出来,在Page_Load和分页事件处理程序中都要调用这段代码,而不能只DataBind().
[解决办法]
webdiyer正解,相信lz是新手吧?
[解决办法]
<% @ Page Language= "C# " %>
<% @ Import Namespace= "System.Data " %>
<% @ Import Namespace= "System.Data.OleDb " %>
<Script Language= "C# " Runat= "Server ">
/*
Create By 飞刀
http://www.aspcn.com
2001-7-25 01:44
Support .Net Framework Beta 2
*/
OleDbConnection MyConn;
int PageSize,RecordCount,PageCount,CurrentPage;
public void Page_Load(Object src,EventArgs e)
{
//设定PageSize
PageSize = 10;
//连接语句
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= "+Server.MapPath( ". ")+ "..\\DataBase\\db1.mdb; ";
MyConn = new OleDbConnection(MyConnString);
MyConn.Open();
//第一次请求执行
if(!Page.IsPostBack)
{
ListBind();
CurrentPage = 0;
ViewState[ "PageIndex "] = 0;
//计算总共有多少记录
RecordCount = CalculateRecord();
lblRecordCount.Text = RecordCount.ToString();
//计算总共有多少页
PageCount = RecordCount/PageSize;
lblPageCount.Text = PageCount.ToString();
ViewState[ "PageCount "] = PageCount;
}
}
//计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from Score ";
OleDbCommand MyComm = new OleDbCommand(strCount,MyConn);
OleDbDataReader dr = MyComm.ExecuteReader();
if(dr.Read())
{
intCount = Int32.Parse(dr[ "co "].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}
ICollection CreateSource()
{
int StartIndex;
//设定导入的起终地址
StartIndex = CurrentPage*PageSize;
string strSel = "select * from Score ";
DataSet ds = new DataSet();
OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds,StartIndex,PageSize, "Score ");
return ds.Tables[ "Score "].DefaultView;
}
public void ListBind()
{
score.DataSource = CreateSource();
score.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
if(CurrentPage==0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage+1).ToString();
}
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;
}
ViewState[ "PageIndex "] = CurrentPage;
ListBind();
}
</script>
<html>
<head>
<title> </title>
</head>
<body>
<form runat= "server ">
共有 <asp:Label id= "lblRecordCount " ForeColor= "red " runat= "server " /> 条记录
当前为 <asp:Label id= "lblCurrentPage " ForeColor= "red " runat= "server " /> / <asp:Label id= "lblPageCount " ForeColor= "red " runat= "server " /> 页
<asp:DataList id= "score " runat= "server "
HeaderStyle-BackColor= "#aaaadd "
AlternatingItemStyle-BackColor= "Gainsboro "
EditItemStyle-BackColor= "yellow "
>
<ItemTemplate>
姓名: <%# DataBinder.Eval(Container.DataItem, "Name ") %>
<asp:LinkButton id= "btnSelect " Text= "编辑 " CommandName= "edit " runat= "server " />
</ItemTemplate>
</asp:DataList>
<asp:LinkButton id= "lbnPrevPage " Text= "上一页 " CommandName= "prev " OnCommand= "Page_OnClick " runat= "server " />
<asp:LinkButton id= "lbnNextPage " Text= "下一页 " CommandName= "next " OnCommand= "Page_OnClick " runat= "server " />
</form>
</body>
</html>
[解决办法]
后台代码改成如下
public partial class newslist : System.Web.UI.UserControl
{
private string newTypeID;
public string NewTypeID
{
set
{
this.newTypeID = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
binddate();
}
}
protected void binddate()
{
SqlConnection con = DB.createCon();
SqlCommand cmd = new SqlCommand( "select newsTypeName from newsType where newsTypeID= ' " + this.newTypeID + " ' ", con);
con.Open();
string newTypeName = Convert.ToString(cmd.ExecuteScalar());
this.Label1.Text = newTypeName;
cmd.CommandText = "select * from newsMaster where newsTypeID= ' " + this.newTypeID + " ' ";
this.DataGrid1.DataSource = cmd.ExecuteReader();
this.DataGrid1.DataBind();
}
protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
this.binddate();
}
}
[解决办法]
建议把
SqlConnection con = DB.createCon();
SqlCommand cmd = new SqlCommand( "select newsTypeName from newsType where newsTypeID= ' " + this.newTypeID + " ' ", con);
con.Open();
string newTypeName = Convert.ToString(cmd.ExecuteScalar());
this.Label1.Text = newTypeName;
cmd.CommandText = "select * from newsMaster where newsTypeID= ' " + this.newTypeID + " ' ";
this.DataGrid1.DataSource = cmd.ExecuteReader();
this.DataGrid1.DataBind();
独立出来作为一个方法,比如bind(),这样在需要绑定的时候直接bind(),
[解决办法]
楼主 this.DataGrid1.DataBind(); 这句是固定写法
[解决办法]
我也用的用户控件 代码如下:
using System;
namespace MicMasterTest.Model
{
/// <summary>
/// Page の概要の明です。
/// </summary>
public class Page
{
private int CurrentPage = 1;
private int TotalRecords = 0;
private int LowerBound = 0;
private int UpperBound = 0;
private int TotalPages = 1;
private int PageSize = 10;
public int TOTALPAGES
{
set
{
this.TotalPages = value;
}
get
{
return this.TotalPages;
}
}
public int LOWERBOUND
{
set
{
this.LowerBound = value;
}
get
{
return this.LowerBound;
}
}
public int UPPERBOUND
{
set
{
this.UpperBound = value;
}
get
{
return this.UpperBound;
}
}
public int TOTALRECORDS
{
set
{
this.TotalRecords = value;
}
get
{
return this.TotalRecords;
}
}
public int CURRENTPAGE
{
set
{
this.CurrentPage = value;
}
get
{
return this.CurrentPage;
}
}
public int PAGESIZE
{
get
{
return PageSize;
}
set
{
PageSize = value;
}
}
public void Recalculate()
{
TotalPages = (int)Math.Ceiling((double)TotalRecords/(double)PageSize);
LowerBound = PageSize * (CurrentPage - 1);
UpperBound = PageSize * CurrentPage - 1;
if (UpperBound > = TotalRecords)
{
UpperBound = TotalRecords - 1;
}
}
}
}
private void Page_Load(object sender, System.EventArgs e)
{
// ペジを初期化するユザ コドをここに入します。
this.uPager.Visible = false;
}
#region Web フォム デザイナで生成されたコド
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: この呼び出しは、ASP.NET Web フォム デザイナで必要です。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// デザイナ サポトに必要なメソッドです。このメソッドの内容を
/// コド エディタで更しないでください。
/// </summary>
private void InitializeComponent()
{
this.btnView.Click += new System.EventHandler(this.btnView_Click);
this.btnNew.Click += new System.EventHandler(this.btnNew_Click);
this.uPager.Paging += new MIC.RETSS.Common.Controls.PagingEventHandler(this.uPager_Paging);
this.CustomValidator1.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.CustomValidator1_ServerValidate);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnView_Click(object sender, System.EventArgs e)
{
if(Page.IsValid == true)
this.BundleTable(1);
else
this.Panel1.Visible = false;
}
//Redirect BundleEdit.aspx
private void btnNew_Click(object sender, System.EventArgs e)
{
Response.Redirect( "BundleEdit.aspx?state=create ");
}
private void uPager_Paging(object sender, MIC.RETSS.Common.Controls.PagingEventArgs e)
{
this.uPager.CurrentPage = e.GotoPage;
this.BundleTable(e.GotoPage);
}
private void BundleTable(int currentPage)
{
M_BUNDLE_OPERATIONS mbo = new M_BUNDLE_OPERATIONS();
BundleSql bSql = new BundleSql();
DataTable dt = new DataTable();
mbo.Bundle_cd = this.txtCD.Text;
mbo.Bundle_name = this.txtNAME.Text;
mbo.Currentpage = currentPage;
dt = bSql.BundleSelect(mbo);
mbo.Totalrecords = dt.Rows.Count;
mbo.Recalculate();
if(dt.Rows.Count > 0)
{
this.uPager.Visible = true;
this.lblMessage .Visible = false;
ArrayList ar = new ArrayList();
for(int i = mbo.Lowerbound;i <=mbo.Upperbound;i ++)
{
M_BUNDLE_OPERATIONS m = new M_BUNDLE_OPERATIONS();
if(this.chClear.Checked == true)
m.Del_type = dt.Rows[i][0].ToString();
else
m.Del_type = " ";
m.Bundle_cd = dt.Rows[i][1].ToString();
m.Bundle_name = dt.Rows[i][2].ToString();
m.Apply_vol = Convert.ToInt32(dt.Rows[i][3].ToString());
m.Pt = Convert.ToDouble(dt.Rows[i][4].ToString());
ar.Add(m);
}
this.Repeater1.DataSource = ar;
this.Repeater1.DataBind();
this.uPager.CurrentPage = mbo.Currentpage;
this.uPager.TotalPages = mbo.Totalpages;
this.Panel1.Visible = true;
}
else
{
this.Panel1.Visible = false;
this.lblMessage.Visible = true;
this.lblMessage.Text = "索条件に当するデタはつかりませんでした。 ";
}
}