ASP.NET C# 分页显示的问题,请教.

tonyvicky1 2003-08-31 12:28:24
<% @ Page Language="C#" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.OleDb" %>
<Script Language="C#" Runat="Server">
public void Page_Load(Object src,EventArgs e)
{
//各种String的设置
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".\\data\\db1.mdb")+";";
string strSel1 = "select * from Score";
DataSet MyDataSet = new DataSet();
OleDbConnection MyConnection = new OleDbConnection(MyConnString);
//打开DataAdapter
OleDbDataAdapter MyAdapter1 = new OleDbDataAdapter(strSel1,MyConnection);
//将各表的数据,存入DataSet
MyAdapter1.Fill(MyDataSet,"Score");
//交给DataGrid去显示
DataGrid1.DataSource = MyDataSet.Tables["Score"].DefaultView;
DataGrid1.DataBind();
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form id=ServerForm runat=server>
<ASP:DataGrid id="DataGrid1" runat="server"
AllowPaging="True"
PageSize="1"
PagerStyle-Mode="NumericPages"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="#eeeeee"
/>
</form>
</body>
</html>


总感觉没有错误呀,不知道是什么原因,就是不能到下一页了.
...全文
27 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tonyvicky1 2003-08-31
  • 打赏
  • 举报
回复
help me
qiuji 2003-08-31
  • 打赏
  • 举报
回复
DataGrid分页:

1.pirvate void getData()
{
//你的获取数据并绑定到DataGrid的代码
//各种String的设置
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".\\data\\db1.mdb")+";";
string strSel1 = "select * from Score";
DataSet MyDataSet = new DataSet();
OleDbConnection MyConnection = new OleDbConnection(MyConnString);
//打开DataAdapter
OleDbDataAdapter MyAdapter1 = new OleDbDataAdapter(strSel1,MyConnection);
//将各表的数据,存入DataSet
MyAdapter1.Fill(MyDataSet,"Score");
//交给DataGrid去显示
DataGrid1.DataSource = MyDataSet.Tables["Score"].DefaultView;
DataGrid1.DataBind();
}

2.在Page_Load事件中添加:
if(!IsPostBack)
{
getData();
}

3.分页事件:
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
getData();
}
duneyang 2003-08-31
  • 打赏
  • 举报
回复
你根本没在属性里包含事件驱动呀。加一个
'翻页事件
Sub datagrid_pageindexchanged(ByVal s As Object, ByVal e As DataGridPageChangedEventArgs)
'if (e.NewPageIndex >= 0 And e.NewPageIndex < DataGrid1.PageCount) Then
DataGrid1.CurrentPageIndex = e.NewPageIndex
binddatagrid1()
' End If
End Sub

'选择事件
Sub datagrid_itemcommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)


If DataGrid1.SelectedIndex <> -1 Then'这句话一定要有
       ‘你的操作
End If

End Sub

然后在datagrid的属性里加上OnItemCommand 和 OnPageIndexChanged事件
<asp:datagrid ......OnPageIndexChanged="datagrid_pageindexchanged" OnItemCommand="datagrid_itemcommand" ..........>.......</asp:datagrid>
tonyvicky1 2003-08-31
  • 打赏
  • 举报
回复
试过了,还是不行
;(
不过还是要谢谢你了.
看来不能相信这些控件呀.
BenZ004 2003-08-31
  • 打赏
  • 举报
回复
把page_load里面的那些代码放到if(!IsPostBack){}里面,即:
public void Page_Load(Object src,EventArgs e)
{
if(!IsPostBack)
{
//各种String的设置
string MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+Server.MapPath(".\\data\\db1.mdb")+";";
string strSel1 = "select * from Score";
DataSet MyDataSet = new DataSet();
OleDbConnection MyConnection = new OleDbConnection(MyConnString);
//打开DataAdapter
OleDbDataAdapter MyAdapter1 = new OleDbDataAdapter(strSel1,MyConnection);
//将各表的数据,存入DataSet
MyAdapter1.Fill(MyDataSet,"Score");
//交给DataGrid去显示
DataGrid1.DataSource = MyDataSet.Tables["Score"].DefaultView;
DataGrid1.DataBind();
}
}

试试

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧