zoukankan
html css js c++ java
在DataGrid中实现分页
用过ASP的朋友也许对起分页感到头大,但在ASP。NET中其自动分页功能便让您能得心应手。这是前台代码:
后台代码如下:
using
System;
using
System.Collections;
using
System.ComponentModel;
using
System.Data;
using
System.Data.SqlClient;
using
System.Drawing;
using
System.Web;
using
System.Web.SessionState;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.HtmlControls;
using
System.Configuration;
namespace
mynew
{
/**/
///
<summary>
///
WebForm1 的摘要说明。
///
</summary>
public
class
WebForm1 : System.Web.UI.Page
{
protected
System.Web.UI.WebControls.LinkButton LinkButton1;
protected
System.Web.UI.WebControls.LinkButton LinkButton2;
protected
System.Web.UI.WebControls.LinkButton LinkButton3;
protected
System.Web.UI.WebControls.LinkButton LinkButton4;
protected
System.Web.UI.WebControls.Label lblCurrentIndex;
protected
System.Web.UI.WebControls.Label lblPageCount;
protected
System.Web.UI.WebControls.DataGrid MyDataGrid;
private
void
Page_Load(
object
sender, System.EventArgs e)
{
SqlConnection conn
=
new
SqlConnection(ConfigurationSettings.AppSettings[
"
dsn
"
]);
conn.Open();
SqlDataAdapter myAdapter
=
new
SqlDataAdapter();
myAdapter.SelectCommand
=
new
SqlCommand(
"
select CategoryID,CategoryName from Categories
"
,conn);
//
myAdapter.SelectCommand.CommandType=CommandType.StoredProcedure;
DataSet ds
=
new
DataSet();
myAdapter.Fill(ds,
"
Categories
"
);
MyDataGrid.DataSource
=
ds;
MyDataGrid.DataBind();
lblCurrentIndex.Text
=
"
第
"
+
((Int32)MyDataGrid.CurrentPageIndex
+
1
)
+
"
页
"
;
lblPageCount.Text
=
"
/共
"
+
MyDataGrid.PageCount
+
"
页
"
;
conn.Close();
}
Web 窗体设计器生成的代码
#region
Web 窗体设计器生成的代码
override
protected
void
OnInit(EventArgs e)
{
//
//
CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base
.OnInit(e);
}
/**/
///
<summary>
///
设计器支持所需的方法 - 不要使用代码编辑器修改
///
此方法的内容。
///
</summary>
private
void
InitializeComponent()
{
this
.LinkButton1.Click
+=
new
System.EventHandler(
this
.PagerButton_Click);
this
.LinkButton2.Click
+=
new
System.EventHandler(
this
.PagerButton_Click);
this
.LinkButton3.Click
+=
new
System.EventHandler(
this
.PagerButton_Click);
this
.LinkButton4.Click
+=
new
System.EventHandler(
this
.PagerButton_Click);
this
.Load
+=
new
System.EventHandler(
this
.Page_Load);
}
#endregion
private
void
PagerButton_Click(
object
sender, System.EventArgs e)
{
string
arg
=
((LinkButton)sender).CommandArgument;
//
获取LinkButton的参数值
switch
(arg)
{
case
(
"
next
"
):
if
(MyDataGrid.CurrentPageIndex
<
(MyDataGrid.PageCount
-
1
))
MyDataGrid.CurrentPageIndex
++
;
break
;
case
(
"
prev
"
):
if
(MyDataGrid.CurrentPageIndex
>
0
)
MyDataGrid.CurrentPageIndex
--
;
break
;
case
(
"
first
"
):
MyDataGrid.CurrentPageIndex
=
0
;
break
;
case
(
"
last
"
):
MyDataGrid.CurrentPageIndex
=
MyDataGrid.PageCount
-
1
;
break
;
}
MyDataGrid.DataBind();
}
}
}
就怎么如次 easy 一个简单的分页便实现了。
查看全文
相关阅读:
[idea]2014.1.13
[idea]2014.1.12
纪念我2014.1.4 中国银行广东分行面试
[essay]2014.1.2
纪念我12月29日南方电网笔试
[essay]12.26
[idea]
纪念我12月24日终于用妖姬拿首胜了
Android自动化测试环境部署
Monitor工具使用详解
原文地址:https://www.cnblogs.com/long/p/79190.html
最新文章
HDU1159-Common Subsequence
oracle 查询 过去时间的记录
冒泡排序
插入排序
面试中常见的排序
快速排序
oralce instr与 like 比较
struts2 upload 的时候出错
一些开源项目的地址
关于面试
热门文章
failed to lazily initialize a collection of role
转:移动开发中一些bug及解决方案
web app 页面旋转整体样式问题
android部分手机onclick事件触发2次
内容在某div中滚动
android4.0以上部分手机绘图时会出现重影
App中嵌入网页浏览器
存储过程分割字符串查询
AJAX异步调用
怎么克服假期拖延症?
Copyright © 2011-2022 走看看