1、Repeater控件的嵌套使用自定义分页及加链接样式制定源代码Repeater控件的嵌套使用、自定义分页及加链接,样式制定源代码Repeater控件自定义分页及加链接,样式制定源代码。.cs文件代码:using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using
2、System.Web.UI.HtmlControls;using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page. protected void Page_Load(object sender, EventArgs e) . if (!IsPostBack) . myRepeater.DataSource = pds(); /为Repeater1控件指定数据源 myRepeater.DataBind(); /这个当然是必须的,绑定上去 private PagedDataSource pds() .
3、 SqlConnection myconn = new SqlConnection(Data Source=192.168.201.108;Initial CataLog=陈乐斌;User ID=sa;Password=yttvmedia); myconn.Open(); SqlDataAdapter da = new SqlDataAdapter(select id,username,password,email,tel,age from user order by id desc, myconn); DataSet ds = new DataSet(); da.Fill(ds, user)
4、; SqlDataAdapter da2 = new SqlDataAdapter(select classid,classname,teachername,id,time,score,slevel from class, myconn); da2.Fill(ds, class); ds.Relations.Add(myrela, ds.Tablesuser.Columnsid, ds.Tablesclass.Columnsid); /为上面建立的两个表创建一个关系,指明父列和子列的名称并为他们的关系命名 PagedDataSource pds = new PagedDataSource();
5、 /初始化一个PagedDataSource,允许控件分页 pds.DataSource = ds.Tablesuser.DefaultView; /将上面的ds转换成标准数据视图 pds.AllowPaging = true; /允许分页 pds.PageSize = 5; /每页大小为5 pds.CurrentPageIndex = Convert.ToInt32(Request.QueryStringpage); /设置当前页 return pds; /将处理完毕的pds对象发出去 protected void myRepeater_ItemDataBound(object sender
6、, RepeaterItemEventArgs e) . if (e.Item.ItemType = ListItemType.Footer) ./判断当前项是页脚模板 int n = pds().PageCount;/将分页总数赋给变量n int i = pds().CurrentPageIndex;/将当前分页码赋给i Label lblpc = (Label)e.Item.FindControl(lblpc); lblpc.Text = n.ToString(); /找到lblpc这个Label,将总页码赋给他 Label lblp = (Label)e.Item.FindControl
7、(lblp); lblp.Text = Convert.ToString(pds().CurrentPageIndex+1); /找到lblp这个Label,将当前页码赋给他,但是注意,因为页码从0开始,这里要直观的话就得加1 HyperLink hlfir = (HyperLink)e.Item.FindControl(hlfir); hlfir.NavigateUrl = ?page=0; HyperLink hlla = (HyperLink)e.Item.FindControl(hlla); hlla.NavigateUrl = ?page=+Convert.ToInt32(n-1);
8、 /找到表示最前页和末页的Label,为他们的NavigateUrl属性赋为第0页和最大页码减1 HyperLink hlp = (HyperLink)e.Item.FindControl(hlp); HyperLink hln = (HyperLink)e.Item.FindControl(hln); /找到表示上页和下页这两个控件 if (i n - 2) ./如果当前项已经是最末页 hln.Enabled = false; hlla.Enabled = false; hlp.Enabled = true; else . hln.NavigateUrl = ?page= + Convert
9、.ToInt32(i+1); DropDownList ddlpage = (DropDownList)e.Item.FindControl(ddlpage); for (int p = 0; p n; p+) . ddlpage.Items.Add(Convert.ToString(p + 1); if (p = i) . ddlpage.SelectedIndex = p; protected void ddl_SelectedChanged(object sender, EventArgs e) . DropDownList ddl = (DropDownList)sender; Page.Response.Redirect(?page= +(Convert.ToInt32(ddl.SelectedItem.Value)-1); aspx文件: 无标题页 编号 姓名 密码 年龄 电话 邮箱 a href=mailto: asp:Repeater ID=myRepeater2 runat=server DataSource= a href=mailto: asp:Repeater ID=myRepeater2 runat=server DataSource= 共 页 当前为第 页 跳转到
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1