c# - How to use paging with Repeater control in ASP.NET? -


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

in project repeater control works fine. , need pagination replacing data. don't have information this. may give me advice issue.

as shown below picture.

enter image description here

there's no built-in pagination in repeater control, based on this article, can achieve pagination in repeater control creating repeater control pages , use pageddatasource it's source.

first, add 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 following property in code behind:

//this property contain current page number  public int pagenumber {         {         if (viewstate["pagenumber"] != null)         {             return convert.toint32(viewstate["pagenumber"]);         }         else         {             return 0;         }     }     set { viewstate["pagenumber"] = value; } } 

finally add following methods:

protected void page_load(object sender, eventargs e) {     bindrepeater(); }  private void bindrepeater() {     //do database connection stuff , data     sqlconnection cn = new sqlconnection(yourconnectionstring);     sqlcommand cmd = new sqlcommand();     cmd.connection = cn;     sqldataadapter ad = new sqldataadapter(cmd);     cmd.commandtext = "select * yourtable";      //save result in data table     datatable dt = new datatable();     ad.selectcommand = cmd;     ad.fill(dt);      //create pageddatasource used in paging     pageddatasource pgitems = new pageddatasource();     pgitems.datasource = dt.defaultview;     pgitems.allowpaging = true;      //control page size here      pgitems.pagesize = 4;     pgitems.currentpageindex = pagenumber;     if (pgitems.pagecount > 1)     {         rptpaging.visible = true;         arraylist pages = new arraylist();         (int = 0; <= pgitems.pagecount - 1; i++)         {             pages.add((i + 1).tostring());         }         rptpaging.datasource = pages;         rptpaging.databind();     }     else     {         rptpaging.visible = false;     }      //finally, set datasource of repeater     repcourse.datasource = pgitems;     repcourse.databind(); }  //this method fire when clicking on page no link pager repeater protected void rptpaging_itemcommand(object source, system.web.ui.webcontrols.repeatercommandeventargs e) {     pagenumber = convert.toint32(e.commandargument) - 1;     bindrepeater(); } 

please give try , if faced issue inform me.

edit: alternative solution

another excellent solution can found here, solution includes the navigation buttons of pages. you'll need download files link see functional pagination , replace datalist control repeater control.

hope helps.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -