aspx页面:
<form runat="server">
'Previous' Link Text:
<ASP:TextBox id="PrevText" runat="server" />
'Next' Link Text:
<ASP:TextBox id="NextText" runat="server" /><br />
Paging Style:
<ASP:RadioButton id="PageNumeric" GroupName="Style"
runat="server" /> Numeric
<ASP:RadioButton id="PageText" GroupName="Style"
runat="server" /> Text<br />
Number of rows per Page:
<ASP:TextBox id="PageRows" runat="server" /><p />
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="90%"
AllowPaging="True" //允许分页
PagerStyle-HorizontalAlign="Right"
OnPageIndexChanged="ChangeGridPage" />
//OnPageIndexChanged事件处理函数:ChangeGridPage
</form>
脚本:
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
if (!Page.IsPostBack)//首次加载
{
// set the default values in the controls on the page
PrevText.Text = "Prev";
NextText.Text = "Next";
PageNumeric.Checked = true;
PageRows.Text = "10";
// set the initial page in the DataGrid to zero
// (not actually required as this is the default)
MyDataGrid.CurrentPageIndex = 0;
// create the data set and bind it to the DataGrid control
BindDataGrid();
}
}
//分页处理函数:
void ChangeGridPage(Object objSender, DataGridPageChangedEventArgs objArgs )
{
// runs when one of the pager controls is clicked
// update the current page number from the parameter values
MyDataGrid.CurrentPageIndex = objArgs.NewPageIndex;
// recreate the data set and bind it to the DataGrid control
BindDataGrid();
}
void BindDataGrid()
{
// set the value of the number of rows per page
MyDataGrid.PageSize = Convert.ToInt32(PageRows.Text);
// set the type of pager to include in the DataGrid
if (PageNumeric.Checked == true)
MyDataGrid.PagerStyle.Mode = PagerMode.NumericPages;
else
MyDataGrid.PagerStyle.Mode = PagerMode.NextPrev;
// set the text for the pager to use when in NextPrev mode
MyDataGrid.PagerStyle.NextPageText = NextText.Text;
MyDataGrid.PagerStyle.PrevPageText = PrevText.Text;
// get connection string from ../global/connect-strings.ascx user control
string strConnect = ctlConnectStrings.OLEDBConnectionString;
//create a SQL statement to select some rows from the database
string strSelect = "SELECT * FROM BookList";
// create a variable to hold an instance of a DataView object
DataView objDataView;
// get DataView from get-dataview-control.ascx user control
objDataView = ctlDataView.GetDataView(strConnect, strSelect);
if (objDataView == null)
return;
// set the DataSource property of the DataList
MyDataGrid.DataSource = objDataView;
// and bind the control to the data
MyDataGrid.DataBind();
}
</script>
关键部分:
初始值:
MyDataGrid.CurrentPageIndex = 0;
点击分页链接时指向新页面:
MyDataGrid.CurrentPageIndex = objArgs.NewPageIndex;
在BindDataGrid()中进行分页设置:
// set the value of the number of rows per page
MyDataGrid.PageSize = Convert.ToInt32(PageRows.Text);
// set the type of pager to include in the DataGrid
if (PageNumeric.Checked == true)
MyDataGrid.PagerStyle.Mode = PagerMode.NumericPages;
else
MyDataGrid.PagerStyle.Mode = PagerMode.NextPrev;
// set the text for the pager to use when in NextPrev mode
MyDataGrid.PagerStyle.NextPageText = NextText.Text;
MyDataGrid.PagerStyle.PrevPageText = PrevText.Text;