Repeater读取数据并分页

来源:互联网 发布:淘宝店铺免费推广软件 编辑:程序博客网 时间:2024/06/06 05:45
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
据说这个没有什么用,但是把这个搞懂了对datagri和datalist和有帮助,事业就笔记一下了。
----------控件清单---------
Panel Panel1;
Button Button1;
Button Button2;
Label Label1;
Label Label2;
Label Label3;
Label Label4;
Label Label5;
Repeater Repeater1;

----------------------------------.cs页------------------------------------------
private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
this.Label1.Text="1";
this.fill();
}
}
private void fill()
{
int pag=Convert.ToInt32(this.Label1.Text);//设置当前页
SqlConnection con=new SqlConnection("server=.;database=Northwind;uid=sa;pwd=980123;");//实例化连接
SqlDataAdapter sda=new SqlDataAdapter();//建立一个数据适配器对象
sda.SelectCommand=new SqlCommand("select * from Employees",con);//实例化SelectCommand,并用他从数据库读出全部数据
DataSet ds=new DataSet();//定义一个数据集填充
sda.Fill(ds,"name");//使用适配器填充数据集到本地表“name”
PagedDataSource ps=new PagedDataSource();//实例化一个PagedDataSource,这个本来是封装是DATAGRID里面的
ps.DataSource=ds.Tables["name"].DefaultView;//设置他的数据源为ds.Tables["name"].DefaultView数据视图
ps.AllowPaging=true;//允许分页
ps.PageSize=3;//每页显示数量
ps.CurrentPageIndex=pag-1;//当前页码,因为页是从0开始的,所以要减1
this.Button1.Enabled=true;//按钮当前状态
this.Button2.Enabled=true;
this.Label5.Text=ps.PageCount.ToString();
if(pag==1)
{
this.Button1.Enabled=false;//如果当前页是 1 ,上一页按钮不可用
}
if(pag==ps.PageCount)
{
this.Button2.Enabled=false;//如果当前页是最后一页 ,下一页按钮不可用
}
this.Repeater1.DataSource=ps;
this.Repeater1.DataBind();
}
窗体代码
private void Button2_Click(object sender, System.EventArgs e)
{
this.Label1.Text=((Convert.ToInt32(this.Label1.Text)) 1).ToString();
this.fill();
}

private void Button1_Click(object sender, System.EventArgs e)
{
this.Label1.Text=((Convert.ToInt32(this.Label1.Text))-1).ToString();
this.fill();
}

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击