求救。。。老大们进来帮帮小弟。。。
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "Default4.aspx.cs " Inherits= "Default4 " %>
<%@ Import Namespace= "System.Data " %>
<%@ Import Namespace= "System.Data.OleDb " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<script language= "C# " runat= "server ">
OleDbConnection MyConn;
public void Page_Load(Object src, EventArgs e)
{
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Server.MapPath( "db1.mdb ");
MyConn = new OleDbConnection(MyConnString);
MyConn.Open();
if (!Page.IsPostBack)
{
BindGrid();
}
}
ICollection CreateTable()
{
string strSel = "select * from Score ";
DataSet ds = new DataSet();
OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strSel,MyConn);
MyAdapter.Fill(ds, "Score ");
return ds.Tables[ "Score "].DefaultView;
}
public void BindGrid()
{
DataView dv = (DataView)CreateTable();
usrGrid.VirtualItemCount = dv.Count;
usrGrid.DataSource = dv;
usrGrid.DataBind();
}
public void DataGrid_PageChanged(Object sender, DataGridPageChangedEventArgs e)
{
usrGrid.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
</script>
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<center>
<b> 演示GataGrid的分页功能 </b>
<asp:DataGrid ID= "usrGrid " runat= "server " HeaderStyle-BackColor= "#aaaadd "
AlternatingItemStyle-BackColor= "#eeeeee " AutoGenerateColumns= "false "
AllowPaging= "true " OnPageIndexChanged= "DataGrid_PageChanged "
PageSize= "4 "
PagerStyle-PrevPageText= "前页 "
PagerStyle-NextPageText= "后页 "
PagerStyle-HorizontalAlign= "Right "
AllowCustomPaging= "true ">
<Columns>
<asp:BoundColumn HeaderText= "姓名 " DataField= "Name "> </asp:BoundColumn>
<asp:BoundColumn HeaderText= "语文 " DataField= "Chinese "> </asp:BoundColumn>
<asp:BoundColumn HeaderText= "数学 " DataField= "Math "> </asp:BoundColumn>
</Columns>
</asp:DataGrid>
</center>
</form>
</body>
</html>
-----------------------
代码我照书写的,主要是分页显示功能(自定义)
但是显示出来数据我点下一页数据没有更新,还是第一次显示的。
[解决办法]
代码记得写在.cs文件中啊,要养成好习惯
主要看下DataGrid_PageChanged这个事件是否跟你的datagrid关联起来了?如果你只是照抄的话,很可能就是因为这个原因
[解决办法]
1。
设置 AllowCustomPaging=falase 就会得到预期的分页效果
2。
当你使用自定义分页的时候(AllowCustomPaging=true),DataGrid 将呈现你提供的 DataSource 的前 PageSize 条数据,因此,比如你跳到第二页的时候,你只能返回实际上第二页的数据,而你这里却总是返回全部数据,自然永远显示的第一页的数据
3。
关于自定义分页示例:
http://msdn2.microsoft.com/zh-cn/library/system.web.ui.webcontrols.datagrid.virtualitemcount(VS.80).aspx
4。
MyConn.Open();
》》》》
Connection Open 了竟然没有 Close
5。
哪本书抄的?既然给了示例页没由将清楚?
趁早丢一边去吧