Asp.net Study(11)
DataGrid 控件
用到的属性:AllowPaging,PageSize,OnPageIndexChanged
5.用DataGrid控件进行分页
注意:不是所有的控件都可以进行DataBind(),在使用DataBind之前先用ICollection函数创建默认视图,然后再进行DataBind()
声明OleDbConnection变量位全局变量
今天的代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataGridPageIndex.aspx.cs" Inherits="DataGridPageIndex" %>
<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<script runat=server>
OleDbConnection Myconn;
public void Page_Load(Object src, EventArgs e)
{
string Myconnstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(".") + "..\\data\\score.mdb;";
Myconn = new OleDbConnection(Myconnstring);
Myconn.Open();
if (!Page.IsPostBack)
{
DataBind();
}
}
ICollection CreateTable()
{
string strsel = "select * from score";
OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strsel, Myconn);
DataSet ds = new DataSet();
MyAdapter.Fill(ds, "score");
return ds.Tables["score"].DefaultView;
}
public void DataBind()
{
score.DataSource = CreateTable();
score.DataBind();
}
public void DataGrid_PageChanged(Object sender, DataGridPageChangedEventArgs e)
{
score.CurrentPageIndex = e.NewPageIndex;
DataBind();
}
</script>
<title>DataGridPageIndex</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid
HeaderStyle-BackColor=Aqua
AlternatingItemStyle-BackColor=AliceBlue
ID="score"
AutoGenerateColumns=true
runat=server
AllowPaging=true PageSize="3"
OnPageIndexChanged="DataGrid_PageChanged">
</asp:DataGrid>
</div>
</form>
</body>
</html>
</html>