datagrid自动分页

aspx页面:
<form runat="server">

   'Previous' Link Text:
   <ASP:TextBox id="PrevText" runat="server" /> &nbsp;
   'Next' Link Text:
   <ASP:TextBox id="NextText" runat="server" /><br />

   Paging Style:
   <ASP:RadioButton id="PageNumeric" GroupName="Style"
                    runat="server" /> Numeric &nbsp;
   <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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值