1. 程式人生 > >GridView結合ASPNETPager的分頁程式碼

GridView結合ASPNETPager的分頁程式碼

這兩天終於把AspNetPager分頁控制元件學會了,以前寫的分頁程式碼很多也很複雜,用上分頁控制元件後,程式簡潔了許多。剛開始學習程式設計的時候我以為程式碼越多越好,如果一個程式有十幾萬行程式碼,那肯定挺牛,而現在我不這麼認為了,如果同樣實現一個功能,程式碼能夠做到越簡潔越好。

花了兩天時間,將安國珠寶的分頁改成AspNetPager控制元件分頁,不但少了很多行的程式碼,功能方面也比我原來寫的還要強大很多,那為什麼自己還想著把以前的分頁程式碼記錄下來呢?因為我覺得以前的程式碼並非一無是處,留下來供以後參考還是可以的。當然,在稍後我會把AspNetPager控制元件的使用方法和下載地址釋出出來,讓更多的朋友開發時可以更加快捷。

前臺程式碼

    <asp:GridView ID="GV_News" runat="server" AutoGenerateColumns="False" CellPadding="1" Width="100%" Height="35px" BorderStyle="None" BorderWidth="0px" ShowHeader="False" GridLines="None" PageSize="20" >
      <Columns>
         <asp:TemplateField >
           <ItemTemplate>
             <ul>
               <li>
   <span class="time"><asp:Label ID="lab_date" runat="server" Text='<%# Bind("欄位名") %>'></asp:Label></span>
        </li>
      </ul>
           </ItemTemplate>
          </asp:TemplateField>
        </Columns>
       <PagerSettings Visible="False" />
          <EmptyDataTemplate>
           暫時沒有任何新聞資訊!
          </EmptyDataTemplate>
     </asp:GridView>

<ul>
    <li>共有&nbsp;<asp:Label ID="lab_Totally" runat="server" ForeColor="red"></asp:Label>&nbsp;行記錄</li>
    <li>當前第&nbsp;<asp:Label ID="lab_Front" runat="server" ForeColor="red"><%=GV_News.PageIndex + 1%></asp:Label>&nbsp;頁/共&nbsp;<asp:Label ID="lab_all" runat="server"><%=GV_News.PageCount%></asp:Label> &nbsp;頁&nbsp;&nbsp;20&nbsp;行/頁</li>
    <li><asp:LinkButton ID="lkbt_first" runat="server" CommandArgument="first" OnClick="lkbt_first_Click">首 頁</asp:LinkButton></li>
    <li><asp:LinkButton ID="lkbt_prev" runat="server" CommandArgument="prev" OnClick="lkbt_prev_Click">上一頁</asp:LinkButton></li>
    <li><asp:LinkButton ID="lkbt_next" runat="server" CommandArgument="next" OnClick="lkbt_next_Click">下一頁</asp:LinkButton></li>
    <li><asp:LinkButton ID="lkbt_last" runat="server" CommandArgument="last" OnClick="lkbt_last_Click">尾 頁</asp:LinkButton></li>
    <li>跳轉至第&nbsp;<asp:DropDownList ID="ddl_tz" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddl_tz_SelectedIndexChanged"></asp:DropDownList></li>
    <li>
</ul>

後臺程式碼

    private void BindNews()

    {

           try
            {
                SqlConnection conn =new SqlConnection(ConfigurationManager.ConnectionStrings["DBpath"].ConnectionString);
                conn.Open();
                string sql = "select * from 表名";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "temp");
                GV_News.DataSource = ds.Tables["news"].DefaultView;
                GV_News.AllowPaging = true;
                GV_News.PageSize = 20;

                int num = ds.Tables[0].Rows.Count;
                lab_Totally.Text = Convert.ToString(num);

                GV_News.DataBind();//繫結datalist

                if (num < 21)
                {//假如記錄條數少於21條,所有分頁不可用。
                    lkbt_first.Enabled = false;
                    lkbt_prev.Enabled = false;
                    lkbt_next.Enabled = false;
                    lkbt_last.Enabled = false;
                }
                else
                {
                    lkbt_first.Enabled = true;
                    lkbt_prev.Enabled = true;
                    lkbt_next.Enabled = true;
                    lkbt_last.Enabled = true;
                }
                if (GV_News.PageIndex == 0)
                { //假如在首頁,設定“首頁”和“上一頁”按鈕不可用
                    lkbt_first.Enabled = false;
                    lkbt_prev.Enabled = false;
                }
                else
                {
                    lkbt_first.Enabled = true;
                    lkbt_prev.Enabled = true;
                }
                if (GV_News.PageIndex == GV_News.PageCount - 1)
                {//假如在末頁,設定“下一頁”和“尾頁”按鈕不可用
                    lkbt_next.Enabled = false;
                    lkbt_last.Enabled = false;
                }
                else
                {
                    lkbt_next.Enabled = true;
                    lkbt_last.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                //提示錯誤資訊
            }
            finally
            {
                conn.Close();
            }
        }

    }

        protected void lkbt_first_Click(object sender, EventArgs e)
        {//首頁
            switch (((LinkButton)sender).CommandArgument.ToString())
            {
                case "first"://“first”在“lb_sy”按鈕的CommandArgument屬性中設定
                    GV_News.PageIndex = 0;
                    break;
                case "last":
                    GV_News.PageIndex = GV_News.PageCount - 1;
                    break;
                case "prev":
                    GV_News.PageIndex = GV_News.PageIndex - 1;
                    break;
                case "next":
                    GV_News.PageIndex = GV_News.PageIndex + 1;
                    break;

            }
            BindNews();
        }

        protected void lkbt_prev_Click(object sender, EventArgs e)
        {//上一頁
            switch (((LinkButton)sender).CommandArgument.ToString())
            {
                case "first":
                    GV_News.PageIndex = 0;
                    break;
                case "last":
                    GV_News.PageIndex = GV_News.PageCount - 1;
                    break;
                case "prev":
                    GV_News.PageIndex = GV_News.PageIndex - 1;
                    break;
                case "next":
                    GV_News.PageIndex = GV_News.PageIndex + 1;
                    break;

            }
            BindNews();
        }

        protected void lkbt_next_Click(object sender, EventArgs e)
        {//下一頁
            switch (((LinkButton)sender).CommandArgument.ToString())
            {
                case "first":
                    GV_News.PageIndex = 0;
                    break;
                case "last":
                    GV_News.PageIndex = GV_News.PageCount - 1;
                    break;
                case "prev":
                    GV_News.PageIndex = GV_News.PageIndex - 1;
                    break;
                case "next":
                    GV_News.PageIndex = GV_News.PageIndex + 1;
                    break;

            }
            BindNews();
        }

        protected void lkbt_last_Click(object sender, EventArgs e)
        {//尾頁
            switch (((LinkButton)sender).CommandArgument.ToString())
            {
                case "first":
                    GV_News.PageIndex = 0;
                    break;
                case "last":
                    GV_News.PageIndex = GV_News.PageCount - 1;
                    break;
                case "prev":
                    GV_News.PageIndex = GV_News.PageIndex - 1;
                    break;
                case "next":
                    GV_News.PageIndex = GV_News.PageIndex + 1;
                    break;

            }
            BindNews();
        }

        protected void ddl_tz_SelectedIndexChanged(object sender, EventArgs e)
        {//跳轉至第幾頁
            int CurPage;
            //判斷是否有分頁的請求;
            ///
            if (ddl_tz.SelectedValue != null)
            {
                CurPage = Convert.ToInt32(ddl_tz.SelectedValue);
            }
            else
            {
                CurPage = 1;
            }
            //設定當前頁;
            GV_News.PageIndex = CurPage - 1;
            BindNews();
        }