http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=D5C6E29D-99F5-44E8-4FED-75AF892A53CB

解决方案 »

  1.   

    Repeater和DataList控件提供了一个快速、灵活的表现数据的方式,但是,它们没有内建的分页功能;DataGrid控件提供了内建的分页功能,但它的结构比较复杂。下面就用PagedDataSource类实现Repeater和DataList的分页。 PagedDataSource封装了DataGrid的分页属性,我们可以象DataGrid那样进行分页。代码如下:<%@ Page Language="C#" %>
    <%@ import namespace="System.Data" %>
    <%@ import namespace="System.Data.OleDb" %>
    <script language="C#" runat="server">
    public void Page_Load(Object src,EventArgs e) {
      OleDbConnection objConn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + 
       Server.MapPath("../aspxWeb.mdb"));
      OleDbDataAdapter objCommand=new OleDbDataAdapter("select * from Document",objConn);
      DataSet ds=new DataSet();
      objCommand.Fill(ds);  PagedDataSource objPds = new PagedDataSource();
      objPds.DataSource = ds.Tables[0].DefaultView;
      objPds.AllowPaging = true;
      objPds.PageSize = 5;
      int CurPage;
      if (Request.QueryString["Page"] != null)
        CurPage=Convert.ToInt32(Request.QueryString["Page"]);
      else
        CurPage=1;  objPds.CurrentPageIndex = CurPage-1;
      lblCurrentPage.Text = "当前页:" + CurPage.ToString();  if (!objPds.IsFirstPage)
        lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage-1);  if (!objPds.IsLastPage)
        lnkNext.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" + Convert.ToString(CurPage+1);  Repeater1.DataSource=objPds;
      Repeater1.DataBind();
    }
    </script>
    <html>
    <head>
    <title>Repeater控件分页的例子</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <style>
      P,TD,DIV,SPAN {font-size:9pt}
    </style>
    </head>
    <body>
    <form name="form1" method="POST" runat="server">
    <div style="padding:5px;background-color:#dedede">
    <asp:label ID="lblCurrentPage" runat="server"></asp:label></td>
      <td>&nbsp;<asp:HyperLink id="lnkPrev" runat="server">上一页</asp:HyperLink>
      <asp:HyperLink id="lnkNext" runat="server">下一页</asp:HyperLink>&nbsp;
    </div>
    <hr size="1" color="#000099"/>
    <asp:Repeater ID="Repeater1" runat="server">
    <Itemtemplate>
    <div style="padding:5px;background-color:#dedede">
    <%# DataBinder.Eval(Container.DataItem, "Title") %>
    </div>
    </Itemtemplate>
    </asp:Repeater>
    </form>
    </body>
    </html>
      

  2.   

    我自信我这个例子最经典!<%@Page Language="C#"%><%@Import Namespace="System.Data" %>
    <%@Import Namespace="System.Data.OleDb" %><%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html><head>
    <title>Selecting and Editing Data in a DataList Control</title>
    <style type="text/css">
    body, td {font-family:Tahoma,Arial,sans-serif; font-size:10pt}
    input {font-family:Tahoma,Arial,sans-serif; font-size:8pt}
    .heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}
    .subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px}
    .cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}.rHead {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial;
            font-size:14pt; font-weight:bold; padding:8px; color:green}
    .rItem {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial,sans-serif;
            font-size:10pt}
    .rFoot {font-family:Tahoma,Arial; font-size:8pt; padding:8px; color:darkgray}</style></head>
    <body bgcolor="#ffffff">
    <span class="heading">Selecting and Editing Data in a DataList Control</span><hr />
    <!---------------------------------------------------------------------------><%// -- insert connection string script --%>
    <wrox:connect id="ctlConnectStrings" runat="server" /><div id="outError" runat="server" /><ASP:Label id="lblSQL" runat="server" /><p /><form runat="server">  <ASP:DataList id="MyDataList" runat="server"
           CellSpacing = "2"
           SelectedItemStyle-BackColor="red"
           SelectedItemStyle-ForeColor="white"
           EditItemStyle-BackColor="yellow"
           EditItemStyle-ForeColor="black"
           DataKeyField="ISBN"
           OnItemCommand="DoItemSelect"
           OnEditCommand="DoItemEdit"
           OnUpdateCommand="DoItemUpdate"
           OnDeleteCommand="DoItemDelete"
           OnCancelCommand="DoItemCancel">    <HeaderTemplate>
          <b>Some Wrox Press Books:</b><br />
        </HeaderTemplate>    <ItemTemplate>
          <ASP:Button CommandName="Select" Text="Info" runat="server" />
          <%# DataBinder.Eval(Container.DataItem, "Title") %>
        </ItemTemplate>    <SelectedItemTemplate>
          Title: <b><%# DataBinder.Eval(Container.DataItem, "Title") %></b><br />
          <ASP:Button CommandName="Edit" Text="Edit" runat="server" />
          ISBN: <%# DataBinder.Eval(Container.DataItem, "ISBN") %> &nbsp;
          Published:
          <%# DataBinder.Eval(Container.DataItem, "PublicationDate", "{0:D}") %>
        </SelectedItemTemplate>    <EditItemTemplate>
          <b>ISBN: <%# DataBinder.Eval(Container.DataItem, "ISBN") %></b> &nbsp;
          <ASP:Button CommandName="Update" Text="Update" runat="server" />
          <ASP:Button CommandName="Delete" Text="Delete" runat="server" />
          <ASP:Button CommandName="Cancel" Text="Cancel" runat="server" /><br />
          Title:
          <ASP:TextBox id="txtTitle" Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>'
                       size="46" runat="server" /><br />
          PublicationDate:
          <ASP:TextBox id="txtPubDate" size="20" runat="server"
                       Text='<%# DataBinder.Eval(Container.DataItem, "PublicationDate") %>'/>
        </EditItemTemplate>  </ASP:DataList></form><!---------------------------------------------------------------------------><script language="C#" runat="server"> void Page_Load(Object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    BindDataGrid(); // create data set and bind to list control
    }
    void DoItemSelect(Object objSource, DataListCommandEventArgs objArgs )
    {
    lblSQL.Text = ""; // clear any content from SQL statement Label // see if it was the Select button that was clicked
    if (objArgs.CommandName == "Select")
    {
    // set the SelectedIndex property of the list to this item// s index
    MyDataList.SelectedIndex = objArgs.Item.ItemIndex;
    BindDataGrid(); // bind the data and display it
    }
    }
    void DoItemEdit(Object objSource, DataListCommandEventArgs objArgs)
    {
    // set the SelectedIndex property of the list to -1 to "unselect" it
    MyDataList.SelectedIndex = -1; // set the EditItemIndex property of the list to this item's index
    MyDataList.EditItemIndex = objArgs.Item.ItemIndex;
    BindDataGrid(); // bind the data and display it
    }
    void DoItemUpdate(Object objSource, DataListCommandEventArgs objArgs)
    {
    // get a reference to the title and publication date text boxes
    TextBox objTitleCtrl = (TextBox)objArgs.Item.FindControl("txtTitle");
    TextBox objPubDateCtrl = (TextBox)objArgs.Item.FindControl("txtPubDate");

    // create a suitable SQL statement and execute it
    string strSQL = "UPDATE Booklist SET Title='" + objTitleCtrl.Text + "', "
    + "PublicationDate='" + objPubDateCtrl.Text + "' "
    + "WHERE ISBN='" + MyDataList.DataKeys[objArgs.Item.ItemIndex] + "'";
    ExecuteSQLStatement(strSQL); // set EditItemIndex property of grid to -1 to switch out of Edit mode
    MyDataList.EditItemIndex = -1;
    BindDataGrid(); // bind the data and display it
    }
    void DoItemDelete(Object objSource, DataListCommandEventArgs objArgs)
    {
    // create a suitable SQL statement and execute it
    string strSQL = "DELETE FROM Booklist WHERE ISBN='"
    + MyDataList.DataKeys[objArgs.Item.ItemIndex] + "'";
    ExecuteSQLStatement(strSQL); // set EditItemIndex property of grid to -1 to switch out of Edit mode
    MyDataList.EditItemIndex = -1;
    BindDataGrid(); // bind the data and display it
    }
    void DoItemCancel(Object objSource, DataListCommandEventArgs objArgs)
    {
    // set EditItemIndex property of grid to -1 to switch out of Edit mode
    MyDataList.EditItemIndex = -1;
    BindDataGrid(); // bind the data and display it
    }
    void ExecuteSQLStatement(string strSQL)
    {
    // this is where the SQL statement would be executed against the
    // original data source. In this example, we're simply displaying
    // the statement in a Label on the page
    lblSQL.Text = "<b>The SQL statement that would be executed is:</b><br />" + strSQL;
    }
    void BindDataGrid()
    {
    // get connection string from ..\global\connect-strings.ascx user control
    string strConnect = ctlConnectStrings.OLEDBConnectionString; // create a SQL statement to select some rows from the database
    string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '%18610025%'"; // create a variable to hold an instance of a DataReader object
    OleDbDataReader objDataReader; try
    {
    // create a new Connection object using the connection string
    OleDbConnection objConnect = new OleDbConnection(strConnect); // open the connection to the database
    objConnect.Open(); // create a new Command using the connection object and select statement
    OleDbCommand objCommand = new OleDbCommand(strSelect, objConnect); // execute the SQL statement against the command to get the DataReader
    objDataReader = objCommand.ExecuteReader();
    }
    catch (Exception objError)
    {
    // display error details
    outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
    + objError.Message + "<br />" + objError.Source + "<p />";
    return; //  and stop execution
    } // set the DataSource property and bind the list
    MyDataList.DataSource = objDataReader;
    MyDataList.DataBind();
    }</script><!--------------------------------------------------------------------------->
    <hr /><span class="cite">[<a href="../global/viewsource.aspx">view source</a>] &nbsp; &nbsp; &nbsp; &nbsp;
    +copy;2001 <a class="cite" href="http://www.wrox.com/">Wrox Press</a> -
    <a class="cite" href="http://www.wrox.com/Books/Book_Details.asp?isbn=1861004885">Professional ASP.NET</a> (ISBN: 1-861004-88-5)</span>
    </body>
    </html>
      

  3.   

    再贴一个更经典的:
    <%@Page Language="C#"%><%@Import Namespace="System.Data" %>
    <%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>
    <%@ Register TagPrefix="wrox" TagName="getdataview" Src="..\global\get-dataview-control.ascx" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html><head>
    <title>Using Automatic Paging with a DataGrid Control</title>
    <style type="text/css">
    body, td {font-family:Tahoma,Arial,sans-serif; font-size:10pt}
    input {font-family:Tahoma,Arial,sans-serif; font-size:8pt}
    .heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}
    .subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px}
    .cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}.rHead {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial;
            font-size:14pt; font-weight:bold; padding:8px; color:green}
    .rItem {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial,sans-serif;
            font-size:10pt}
    .rFoot {font-family:Tahoma,Arial; font-size:8pt; padding:8px; color:darkgray}</style></head>
    <body bgcolor="#ffffff">
    <span class="heading">Using Automatic Paging with a DataGrid Control</span><hr />
    <!---------------------------------------------------------------------------><%-- insert connection string script --%>
    <wrox:connect id="ctlConnectStrings" runat="server" /><%-- insert the control that creates the DataSet --%>
    <wrox:getdataview id="ctlDataView" runat="server" /><form runat="server">   'Previous' Link Text:
       <ASP:TextBox id="PrevText" runat="server" /> &nbsp;
       'Next' Link Text:
       <ASP:TextBox id="NextText" runat="server" /><br />   Paging Style:
       <ASP:RadioButton id="PageNumeric" GroupName="Style"
                        runat="server" /> Numeric &nbsp;
       <ASP:RadioButton id="PageText" GroupName="Style"
                        runat="server" /> Text<br />   Number of rows per Page:
       <ASP:TextBox id="PageRows" runat="server" /><p />   <ASP:DataGrid id="MyDataGrid" runat="server"
            Width="90%"
            AllowPaging="True"
            PagerStyle-HorizontalAlign="Right"
            OnPageIndexChanged="ChangeGridPage" /></form><!---------------------------------------------------------------------------><script language="C#" runat="server"> void Page_Load(Object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    // set the default values in the controls on the page
    PrevText.Text = "Prev";
    NextText.Text = "Next";
    PageNumeric.Checked = true;
    PageRows.Text = "10"; // set the initial page in the DataGrid to zero
    // (not actually required as this is the default)
    MyDataGrid.CurrentPageIndex = 0; // create the data set and bind it to the DataGrid control
    BindDataGrid();
    }
    }
    void ChangeGridPage(Object objSender, DataGridPageChangedEventArgs objArgs )
    {
    // runs when one of the pager controls is clicked // update the current page number from the parameter values
    MyDataGrid.CurrentPageIndex = objArgs.NewPageIndex; // recreate the data set and bind it to the DataGrid control
    BindDataGrid();
    }
    void BindDataGrid()
    {
    // set the value of the number of rows per page
    MyDataGrid.PageSize = Convert.ToInt32(PageRows.Text); // set the type of pager to include in the DataGrid
    if (PageNumeric.Checked == true)
    MyDataGrid.PagerStyle.Mode = PagerMode.NumericPages;
    else
    MyDataGrid.PagerStyle.Mode = PagerMode.NextPrev; // set the text for the pager to use when in NextPrev mode
    MyDataGrid.PagerStyle.NextPageText = NextText.Text;
    MyDataGrid.PagerStyle.PrevPageText = PrevText.Text; // get connection string from ..\global\connect-strings.ascx user control
    string strConnect = ctlConnectStrings.OLEDBConnectionString; //create a SQL statement to select some rows from the database
    string strSelect = "SELECT * FROM BookList"; // create a variable to hold an instance of a DataView object
    DataView objDataView; // get DataView from get-dataview-control.ascx user control
    objDataView = ctlDataView.GetDataView(strConnect, strSelect); if (objDataView == null)
    return; // set the DataSource property of the DataList
    MyDataGrid.DataSource = objDataView; // and bind the control to the data
    MyDataGrid.DataBind();
    }</script><!--------------------------------------------------------------------------->
    <!-- #include file="..\global\foot.inc" -->
    </body>
    </html>