posts - 44,  comments - 43,  trackbacks - 1

下次您關於ASPX相關設計的問題,可以到ASPX的版去發問會比較快
得到相關得回應。
ex: 我以下這個小範例
.aspx
       <asp:DataList id="dlPager" runat="server" Height="136px" DataKeyField="EmployeeID" Width="440px">
        <HeaderTemplate>
                  </HeaderTemplate>
        <FooterTemplate>
         <hr color="red">
        </FooterTemplate>
        <ItemTemplate>
         <%#DataBinder.Eval(Container.DataItem,"LastName")%>
         <%#DataBinder.Eval(Container.DataItem,"FirstName")%>
         <%#DataBinder.Eval(Container.DataItem,"Title")%>
         <%#DataBinder.Eval(Container.DataItem,"TitleOfCourtesy")%>
        </ItemTemplate>
       </asp:DataList>

<asp:label id="lblCurPage" runat="server"></asp:label>
       <asp:HyperLink id="lnkPrev" runat="server">上一頁</asp:HyperLink>
       <asp:HyperLink id="lnkNext" runat="server">下一頁</asp:HyperLink>

 

.cs

 private void Page_Load(object sender, System.EventArgs e)
  {
   
   if(!IsPostBack)
    DataListDataBind();
  }
  private void DataListDataBind()
  {
   //
   SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
   //
   SqlDataAdapter da = new SqlDataAdapter("select EmployeeID,LastName,FirstName,Title,TitleOfCourtesy from Employees",conn);
   //
   DataSet ds = new DataSet();  
   try
   {
    //
    da.Fill(ds,"testTable");
    ////建立分類頁
    PagedDataSource objPage = new PagedDataSource();
    //來源
    objPage.DataSource = ds.Tables["testTable"].DefaultView;
    //允許分頁
    objPage.AllowPaging = true;
    //可以顯示一頁的項目
    objPage.PageSize = 5;
    //保存當前頁的索引
    int CurPage;
    //判斷是否有要跳頁的動作
    if (Request.QueryString["Page"] != null)
     CurPage=Convert.ToInt32(Request.QueryString["Page"]);
    else
     CurPage=1;
    //設為目前頁的索引
    objPage.CurrentPageIndex = CurPage-1;
    //顯示訊息
    lblCurPage.Text = "目前在第" + CurPage.ToString()+"頁";
    //如果目前頁不是第一頁
    if (!objPage.IsFirstPage)
     //定義上一頁的超連結為 目前頁 -1
     lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage-1);
    //如果目前頁不是最後一頁
    if (!objPage.IsLastPage)
     //定義下一頁的超連結為 目前頁 +1
     lnkNext.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" + Convert.ToString(CurPage+1);

    //再把經過篩選的objPage給予DataList
    dlPager.DataSource = objPage;
    dlPager.DataBind();
   } 

posted on 2009-03-13 11:07  王波  阅读(375)  评论(0编辑  收藏