我初学.NET,现有一问题按论坛中许多朋友的方法都无法顺利解决,请教一下:
    HTML代码:
    <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" DataSourceID="ObjectDataSource4">
                    <HeaderTemplate>
                    <table width="100%" border=0>
              
            <tr><td>
          </HeaderTemplate>
 
          <ItemTemplate>
               
              <tr>
              <td width="100%">
              
              <table width="100%" style="border-right: lightgrey 2px solid; border-top: lightgrey 2px solid;
            border-left: lightgrey 2px solid; border-bottom: lightgrey 2px solid">
            <th align="center" valign="middle" scope="row">
                 <asp:Label ID="Label2" runat="server" Text='<%#Eval("ArticleNumber")%>' Visible="false"></asp:Label>
                </th>
              </tr>
              <tr>
               <th align="center" valign="middle" scope="row">
                <asp:Label ID="Label1" runat="server" Text='<%#Eval("Title")%>'  Visible="true"></asp:Label>
                <asp:ImageButton ID="ImageButton1" runat="server" CommandArgument='<%#Eval("ArticleNumber") %>' OnCommand="ImageButton1_Click1" ImageUrl='<%# Eval("status").ToString() == "1" ? "images/blog/2.gif" : "images/blog/1.gif"%>'/>
               <asp:ImageButton ID="ImageButton3" runat="server" CommandArgument='<%#Eval("ArticleNumber") %>' OnCommand="ImageButton3_Click1" ImageUrl="images\blog\3.gif" />
              </th>
              </tr>
             <tr>
                <th  align="center" valign="middle" scope="row"><%#Eval("Type")%></th>
              </tr>
              <tr>
                <th align="center" valign="middle" scope="row"><%#Eval("Content")+"......"%></th>
              </tr>
              <tr>
                <th align="center" valign="middle" scope="row">
                <a href= '<%# "whole_ar.aspx?article_number="+Eval("ArticleNumber")%>'><img  border=0 src="images/blog/menu1.gif"/></a>
                <a href= '<%# "scan_ar.aspx?article_number=" + Eval("ArticleNumber") %>'><img border=0 src="images/blog/menu2.gif"/></a>
                <asp:LinkButton ID="LinkButton3" runat="server" CommandArgument='<%#Eval("ArticleNumber")%>' OnCommand="LinkButton3_Click"><img border=0 src="images/blog/menu3.gif"/></asp:LinkButton>
                <a href= '<%# "relating_ar.aspx?article_number="+ Eval("ArticleNumber")%>'><img border=0 src="images/blog/menu4.gif"/></a>
              </th>
              </tr>              
              </table>
            </td>            
         </ItemTemplate>                             
           <FooterTemplate>
           <tr></td>
          </table>
          </FooterTemplate>
                    
                    </asp:Repeater>
                    
 <asp:ObjectDataSource ID="ObjectDataSource4" runat="server" SelectMethod="ar_repeater" TypeName="myclass"></asp:ObjectDataSource>*----------------------------------------------------------------------------------------------------*
App_code下文件myclass.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 System.Web.UI.HtmlControls;
using System.Data.SqlClient;/// <summary>
/// myclass 的摘要说明
/// </summary>
public class myclass
{
    private SqlConnection connection;
    private SqlCommand command;
    public myclass()
     {
//
// TODO: 在此处添加构造函数逻辑
//
        string connectionString="Data Source=(local);Initial Catalog=log;Persist Security Info=True;User ID=admin;Password=admin" ;
        connection = new SqlConnection(connectionString);
        command = new SqlCommand();
        command.Connection = connection;
}
       public DataSet ar_repeater()
     {
        DataSet ds = new DataSet();
        command.CommandText = "select ArticleNumber,Title,SUBSTRING(Content,0,20) as Content, Type,Status from Blog_Article where(Grade<>'2')";
        //command.CommandText = "select ArticleNumber,Title,SUBSTRING(Content,0,200) as Content, Type from Blog_Article where(ReaderID='reader_id')";
        SqlDataAdapter da = new SqlDataAdapter(command);
        da.Fill(ds);
        return ds;
     }
}现在想实现repeater的分页功能,采用网友提供的诸多方法都无法解决,请问应该怎么做?

解决方案 »

  1.   

    改写它(repeater)代码如下
    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web.UI;
    using System.Web.UI.WebControls;namespace PetShop.Web {    public class CustomGrid : Repeater {
            //Static constants
            protected const string HTML1 = "<table cellpadding=0 cellspacing=0><tr><td colspan=2>";
            protected const string HTML2 = "</td></tr><tr><td class=paging align=left>";
            protected const string HTML3 = "</td><td align=right class=paging>";
            protected const string HTML4 = "</td></tr></table>";
            private static readonly Regex RX = new Regex(@"^&page=\d+", RegexOptions.Compiled);
            private const string LINK_PREV = "<a href=?page={0}>&#060;&nbsp;Previous</a>";
            private const string LINK_MORE = "<a href=?page={0}>More&nbsp;&#062;</a>";
            private const string KEY_PAGE = "page";
            private const string COMMA = "?";
            private const string AMP = "&";        protected string emptyText;
            private IList dataSource;
            private int pageSize = 10;
            private int currentPageIndex;
            private int itemCount;        override public object DataSource {
                set {
                    //This try catch block is to avoid issues with the VS.NET designer
                    //The designer will try and bind a datasource which does not derive from ILIST
                    try {
                        dataSource = (IList)value;
                        ItemCount = dataSource.Count;
                    }
                    catch {
                        dataSource = null;
                        ItemCount = 0;
                    }
                }
            }        public int PageSize {
                get { return pageSize; }
                set { pageSize = value; }
            }        protected int PageCount {
                get { return (ItemCount - 1) / pageSize; }
            }        virtual protected int ItemCount {
                get { return itemCount; }
                set { itemCount = value; }
            }        virtual public int CurrentPageIndex {
                get { return currentPageIndex; }
                set { currentPageIndex = value; }
            }        public string EmptyText {
                set { emptyText = value; }
            }        public void SetPage(int index) {
                OnPageIndexChanged(new DataGridPageChangedEventArgs(null, index));
            }        override protected void OnLoad(EventArgs e) {
                if (Visible) {
                    string page = Context.Request[KEY_PAGE];
                    int index = (page != null) ? int.Parse(page) : 0;
                    SetPage(index);
                }
            }        /// <summary>
            /// Overriden method to control how the page is rendered
            /// </summary>
            /// <param name="writer"></param>
            override protected void Render(HtmlTextWriter writer) {            //Check there is some data attached
                if (ItemCount == 0) {
                    writer.Write(emptyText);
                    return;
                }            //Mask the query
                string query = Context.Request.Url.Query.Replace(COMMA, AMP);
                query = RX.Replace(query, string.Empty);            // Write out the first part of the control, the table header
                writer.Write(HTML1);            // Call the inherited method
                base.Render(writer);            // Write out a table row closure
                writer.Write(HTML2);            //Determin whether next and previous buttons are required
                //Previous button?
                if (currentPageIndex > 0)
                    writer.Write(string.Format(LINK_PREV, (currentPageIndex - 1) + query));            //Close the table data tag
                writer.Write(HTML3);            //Next button?
                if (currentPageIndex < PageCount)
                    writer.Write(string.Format(LINK_MORE, (currentPageIndex + 1) + query));            //Close the table
                writer.Write(HTML4);
            }        override protected void OnDataBinding(EventArgs e) {            //Work out which items we want to render to the page
                int start = CurrentPageIndex * pageSize;
                int size = Math.Min(pageSize, ItemCount - start);            IList page = new ArrayList();            //Add the relevant items from the datasource
                for (int i = 0; i < size; i++)
                    page.Add(dataSource[start + i]);            //set the base objects datasource
                base.DataSource = page;
                base.OnDataBinding(e);        }        public event DataGridPageChangedEventHandler PageIndexChanged;        virtual protected void OnPageIndexChanged(DataGridPageChangedEventArgs e) {
                if (PageIndexChanged != null)
                    PageIndexChanged(this, e);
            }
        }
    }
      

  2.   

    一般都用分页控件
    如Asppager
      

  3.   

    最新Aspnetpager7.0控件不错
    支持生成分页存储过程并支持Datalist和Repeater
    代码里就简单调用下就行
      

  4.   

    1.用pageDataSource实现分页 MSDN上有相关内容
    http://www.bksite.com/42.aspx
    2.每次只查你想要的部分,需要在程序中计算一下。然后再绑定到repeater上来。我现在家里,电脑不能使用VS,没有办法给你贴代码了。
     
      

  5.   

    //对用于分页的类的引用
    PagedDataSource pds=new PagedDataSource();
    pds.DataSource=dt.DefaultView;//设置数据源(DataTable类型)
    pds.AllowPaging=true;采用以上的代码中:pds.DataSource=dt.DefaultView 程序会提示已经采用了DataSourceID="ObjectDataSource4 不能重复定义。这个问题该如何解决?
      

  6.   

    Repeater自定义分页+排序
    http://blog.csdn.net/amandag/archive/2008/07/19/2677316.aspx