2014-04-07 19 views
10
<asp:Repeater ID="RepCourse" runat="server"> 

    <ItemTemplate> 

    <div style="width:400px"></div> 

    <div class="course" style="float: left; margin-left: 100px; margin-top: 100px"> 

     <div class="image"> 
     <asp:Image ID="imgteacher" runat="server" Height="150" Width="248" ImageUrl='<%# "ShowImage.ashx?id="+ DataBinder.Eval(Container.DataItem, "CourseID") %>'/> 
     </div> 

     <div style="margin-left: 3px; width: 250px"> 
     <div class="name"> 
      <a href="#"><asp:Label runat="server" ID="lblname" Text='<%#Eval("CourseName") %>'></asp:Label></a> 
     </div> 
     <div style="height: 13px"></div> 
     <div id="teacher"> 
      <a href="#"><%#Eval("UserName") %> </a> 
     </div> 
     </div> 

     <div style="height: 4px"></div> 

     <div class="date"> 
     <div id="datebegin"> 
      <asp:Label ID="lbldatebegin" runat="server" Text='<%#Eval("BeginDate") %>'></asp:Label> 
     </div> 
     <div id="dateend"> 
      <asp:Label ID="lbldateend" runat="server" Text='<%#Eval("ClosingDate") %>'></asp:Label> 
     </div> 
     </div> 

    </div> 

    </ItemTemplate> 

</asp:Repeater> 

W moim projekcie Repeater Control działa dobrze. A teraz potrzebuję paginacji do zastąpienia tych danych. Ale nie mam żadnych informacji na ten temat. Może być ktoś, kto doradzi mi w tej sprawie.Jak korzystać z stronicowania z kontrolką Repeater w ASP.NET?

Jak pokazano poniżej.

enter image description here

+0

może to pomoże: http://www.aspsnippets.com/Articles/Implement-Paging-in-Repeater-control-in-ASPNet.aspx –

Odpowiedz

17

Nie ma wbudowany paginacji w kontroli Repeater, ale na podstawie this artykule, można osiągnąć podział na strony w kontroli Repeater tworząc kolejną kontrolę Repeater dla stron i używać PagedDataSource jak to źródło.

pierwsze, dodać do swojej znaczników:

<div style="overflow: hidden;"> 

<asp:Repeater ID="rptPaging" runat="server" OnItemCommand="rptPaging_ItemCommand"> 
<ItemTemplate> 
    <asp:LinkButton ID="btnPage" 
    style="padding:8px;margin:2px;background:#ffa100;border:solid 1px #666;font:8pt tahoma;" 
    CommandName="Page" CommandArgument="<%# Container.DataItem %>" 
    runat="server" ForeColor="White" Font-Bold="True"> 
    <%# Container.DataItem %> 
    </asp:LinkButton> 
</ItemTemplate> 
</asp:Repeater> 

</div> 

Następnie dodaj następującą właściwość w kodzie za:

//This property will contain the current page number 
public int PageNumber 
{ 
    get 
    { 
     if (ViewState["PageNumber"] != null) 
     { 
      return Convert.ToInt32(ViewState["PageNumber"]); 
     } 
     else 
     { 
      return 0; 
     } 
    } 
    set { ViewState["PageNumber"] = value; } 
} 

koniec dodać następujące metody:

protected void Page_Load(object sender, EventArgs e) 
{ 
    BindRepeater(); 
} 

private void BindRepeater() 
{ 
    //Do your database connection stuff and get your data 
    SqlConnection cn = new SqlConnection(yourConnectionString); 
    SqlCommand cmd = new SqlCommand(); 
    cmd.Connection = cn; 
    SqlDataAdapter ad = new SqlDataAdapter(cmd); 
    cmd.CommandText = "Select * from YourTable"; 

    //save the result in data table 
    DataTable dt = new DataTable(); 
    ad.SelectCommand = cmd; 
    ad.Fill(dt); 

    //Create the PagedDataSource that will be used in paging 
    PagedDataSource pgitems = new PagedDataSource(); 
    pgitems.DataSource = dt.DefaultView; 
    pgitems.AllowPaging = true; 

    //Control page size from here 
    pgitems.PageSize = 4; 
    pgitems.CurrentPageIndex = PageNumber; 
    if (pgitems.PageCount > 1) 
    { 
     rptPaging.Visible = true; 
     ArrayList pages = new ArrayList(); 
     for (int i = 0; i <= pgitems.PageCount - 1; i++) 
     { 
      pages.Add((i + 1).ToString()); 
     } 
     rptPaging.DataSource = pages; 
     rptPaging.DataBind(); 
    } 
    else 
    { 
     rptPaging.Visible = false; 
    } 

    //Finally, set the datasource of the repeater 
    RepCourse.DataSource = pgitems; 
    RepCourse.DataBind(); 
} 

//This method will fire when clicking on the page no link from the pager repeater 
protected void rptPaging_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e) 
{ 
    PageNumber = Convert.ToInt32(e.CommandArgument) - 1; 
    BindRepeater(); 
} 

Spróbuj, a jeśli napotkasz problem, po prostu mnie poinformuj.

Edit: Alternatywne rozwiązanie

Innym doskonałym rozwiązaniem można znaleźć Here to rozwiązanie obejmuje przycisków nawigacyjnych stron. Będziesz musiał pobrać pliki z tego linku, aby zobaczyć funkcjonalną paginację i po prostu zamienić kontrolkę DataList na kontrolkę Repeater.

Mam nadzieję, że to pomoże.

Powiązane problemy