如何在ASP.NET中将页面调度与Repeater控件一起使用?

编程入门 行业动态 更新时间:2024-10-23 19:18:47
本文介绍了如何在ASP.NET中将页面调度与Repeater控件一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 <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>

在我的项目中,Repeater Control正常运行.现在,我需要分页来替换这些数据.但是我对此没有任何信息.可能有人给我有关此问题的建议.

In my project Repeater Control works fine. And now I need pagination for replacing those data. But I don't have any information about this. May be someone give me advice about this issue.

如下图所示.

推荐答案

Repeater控件中没有内置分页,而是基于此文章,您可以通过为页面创建另一个Repeater控件并使用PagedDataSource来在Repeater控件中实现分页作为来源.

There's no built-in pagination in the Repeater control, but based on this article, you can achieve pagination in the Repeater control by creating another Repeater control for pages and use PagedDataSource as it's source.

首先,将其添加到您的标记中:

First, add this to your markup:

<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>

接下来,在您的代码后面添加以下属性:

Next, add the following property in your code behind:

//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; } }

最后添加以下方法:

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(); }

请尝试一下,如果您遇到任何问题,请通知我.

Please give it a try and if you faced any issue just inform me.

替代解决方案

可以找到另一个出色的解决方案此处,此解决方案包括页面的导航按钮.您需要从该链接下载文件以查看功能的分页,只需将DataList控件替换为Repeater控件即可.

Another excellent solution can be found Here, this solution includes the Navigation buttons of pages. You'll need to download files from that link to see a functional pagination and just replace the DataList control with your Repeater control.

希望这会有所帮助.

更多推荐

如何在ASP.NET中将页面调度与Repeater控件一起使用?

本文发布于:2023-11-14 13:39:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587367.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   中将   页面   如何在   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!