asp.net repeater控制使用示例详解

作者:袖梨 2022-06-25


我们将在例子中使用下面的 XML 文件("cdcatalog.xml"):

 代码如下 复制代码



  Empire Burlesque
  Bob Dylan
  USA
  Columbia
  10.90
  1985


  Hide your heart
  Bonnie Tyler
  UK
  CBS Records
  9.90
  1988


  Greatest Hits
  Dolly Parton
  USA
  RCA
  9.90
  1982


  Still got the blues
  Gary Moore
  UK
  Virgin records
  10.20
  1990


  Eros
  Eros Ramazzotti
  EU
  BMG
  9.90
  1997

请查看该 XML 文件:cdcatalog.xml

首先,导入 "System.Data" 命名空间。我们需要此命名空间与 DataSet 对象一同工作。在 .aspx 页面的顶部包含下面这条指令:

<%@ Import Namespace="System.Data" %>接下来,为这个 XML 文件创建一个 DataSet,并把此 XML 文件在页面首次加载时载入 DataSet:

 代码如下 复制代码























Title Artist Country Company Price Year
<%#Container.DataItem("title")%> <%#Container.DataItem("artist")%> <%#Container.DataItem("country")%> <%#Container.DataItem("company")%> <%#Container.DataItem("price")%> <%#Container.DataItem("year")%>



Repeater分页功能:
                                                                              
Repeater不像其他的数据控件有内置的分页功能,如果想在Repeater中实现分页功能,需要借助PagedDataSource来实现分页。

前台代码如下:
前台代码
       

 代码如下 复制代码


                       
               


                   
                       
                       
                       
                     
                       
                       
                   
               
          
         
           


               

               

 

               


               


               


           


       
       
id新闻名发布时间发布人发布状态

                   
               

                   
               

                   
               

                   
               

                   
               

       


      


          
      

           页首
           上一页
           下一页
           页尾

           第页/共
               页
          

后台代码:
后台代码
      

 代码如下 复制代码

  protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.labPage.Text = "1";
                this.fenye();
            }
        }

        void fenye()
        {

            //获取数据源
            PagedDataSource pds = new PagedDataSource();
            DataTable ds = getalldata();
            pds.DataSource = ds.DefaultView;
            //设置允许分页
            pds.AllowPaging = true;
            //设置在单页上显示的条数
            pds.PageSize = 5;
            //获取或设置当前页的索引
            pds.CurrentPageIndex = Int32.Parse(labPage.Text) - 1;
            //绑定控件的数据源
            Repeater1.DataSource = pds;
            Repeater1.DataBind();
            //设置页的总数
            LabCountPage.Text = pds.PageCount.ToString();
            //设置按钮的可操作性
            lbtnDownPage.Enabled = true;
            lbtnFirstPage.Enabled = true;
            lbtnNextPage.Enabled = true;
            lbtnpritPage.Enabled = true;
            if (pds.CurrentPageIndex < 1)
            {
                lbtnFirstPage.Enabled = false;
                lbtnpritPage.Enabled = false;
            }
            if (pds.CurrentPageIndex == pds.PageCount - 1)
            {
                lbtnDownPage.Enabled = false;
                lbtnNextPage.Enabled = false;
            }


   
        }

        DataTable getalldata()
        {

            using (SqlConnection cons = new SqlConnection("server = .; database = newsrelease; integrated security=SSPI;"))
            {
                string liketitle = string.Format("select * from T_News");
                SqlDataAdapter da = new SqlDataAdapter(liketitle, cons);
                DataTable ds = new DataTable();
                da.Fill(ds);
                if (ds != null)
                {
                    return ds;
                }
                else
                {
                    return null;
                }

            }
        }

        //设置首页按钮功能
        protected void lbtnFirstPage_Click(object sender, EventArgs e)
        {
            labPage.Text = "1";
            this.fenye();
        }
        //设置上一页按钮功能
        protected void lbtnpritPage_Click(object sender, EventArgs e)
        {
            labPage.Text = (Int32.Parse(labPage.Text) - 1).ToString();
            this.fenye();
        }
        //设置下一页按钮功能
        protected void lbtnNextPage_Click(object sender, EventArgs e)
        {
            labPage.Text = (Int32.Parse(labPage.Text) + 1).ToString();
            this.fenye();
        }
        //设置尾页按钮功能
        protected void lbtnDownPage_Click(object sender, EventArgs e)
        {
            labPage.Text = LabCountPage.Text;
            this.fenye();
        }

怎么使Repeater的数据横着显示:                                                                           

只需要设置Repeater的ItemTemplate绑定的数据以UL的形式显示,并且设置其CSS的样式向左边浮动即可,代码如下:
横着显示Repeater数据
                     

 代码如下 复制代码
  
                           
                                " class="home" >
                                   

                                           
  • <%#Eval("TypeName") %> 

  •                                    

                               

                           

                       

      
      
例。Repeater显示父子表数据,无闪烁

 

 代码如下 复制代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace RepeaterTest
{
public partial class Repeater : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.bind();
}
}

private DataSet dataSource
{
get
{
if (ViewState["ds"] != null)
{
return (DataSet)ViewState["ds"];
}
else
{
DataSet ds = DataSource.GetDataSet();
ViewState["ds"] = ds;
return ds;
}
}
}

private void bind()
{
DataSet ds = this.dataSource;

this.Repeater1.DataSource = ds.Tables["tbclass"];
this.Repeater1.DataBind();

this.Repeater2.DataSource = ds.Tables["tbclass"];
this.Repeater2.DataBind();
}

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "expand")
{
LinkButton btn = (LinkButton)e.CommandSource;
GridView grid =(GridView)btn.Parent.FindControl("grid1");
if (btn.Text == "+")
{
btn.Text = "-";
grid.Visible = true;
}
else
{
btn.Text = "+";
grid.Visible = false;
}
}
}

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataTable dt = this.dataSource.Tables["tbstu"];
LinkButton objButton = (LinkButton)e.Item.FindControl("btnExpand");
if (objButton != null && objButton.CommandArgument !="")
{
string classID = objButton.CommandArgument;
dt.DefaultView.RowFilter = "ClassID=" + classID;
DataTable dtStu = dt.DefaultView.ToTable();

GridView grid = (GridView)e.Item.FindControl("grid1");
grid.DataSource = dtStu;
grid.DataBind();
}
}

protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataTable dt = this.dataSource.Tables["tbstu"];
Label objlbl = (Label)e.Item.FindControl("lblclassid");
if (objlbl != null && objlbl.Text != "")
{
string classID = objlbl.Text;
dt.DefaultView.RowFilter = "ClassID=" + classID;
DataTable dtStu = dt.DefaultView.ToTable();

GridView grid = (GridView)e.Item.FindControl("grid2");
grid.DataSource = dtStu;
grid.DataBind();
}
}
}
}

相关文章

精彩推荐