程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 一個有趣的.net分頁代碼

一個有趣的.net分頁代碼

編輯:.NET實例教程

一般由微軟自帶的分頁效率極低

每次查詢服務器端都會發送所有的查詢記錄到浏覽器端

為此,很多人就用存儲過程來提高效率,每次傳送的都是一個頁面的內容

 

今天自己弄了個分頁的,呵呵

每次都傳送一個頁面的內容,不過沒有用到存儲過程,呵呵

 

看代碼自己領悟吧,呵呵...

 

在前台弄些分頁的常用控件,呵呵

<ASP:Button ID="Button3" runat="server" Text="首頁" OnClick="Button3_Click" />
<ASP:Button ID="Button1" runat="server" Text="上一頁" OnClick="Button1_Click" />

當前第<asp:Label ID="Label1" runat="server" Text="Label"></ASP:Label>頁
<ASP:Button ID="Button2" runat="server" Text="下一頁" OnClick="Button2_Click" />
<ASP:Button ID="Button4" runat="server" Text="尾頁" OnClick="Button4_Click" />
跳到第<asp:TextBox ID="TextBox1" runat="server" Width="31px"></asp:TextBox>頁<ASP:Button ID="Button5" runat="server" OnClick="Button5_Click" Text="GO" />

 

後台代碼如下,呵呵...

private static int pagesize;
    private static int n;//count
    private static int c;//current index
    string connectionstring = ConfigurationManager.APPSettings["SqlConnectionString"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Button1.Enabled = false;
            Label1.Text = "1";
            pagesize = 5;
            c=n=count();
            msg_top(c);
        }
    }
    private int count()
    {
        SqlConnection con = new SqlConnection(connectionstring);
        con.Open();
        string sql = "select count(*) from MSGPlate";
        SqlCommand cmd = new SqlCommand(sql, con);
        return (int)cmd.ExecuteScalar();
    }
   void msg_top(int c)
    {
        string msg_top = "";
        SqlConnection con = new SqlConnection(connectionstring);
        con.Open();
        string sql = "select top "+pagesize+" * from  MSGPlate where id in(select top "+c+" id from  MSGPlate order by id desc)order by id asc";
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader rs = cmd.ExecuteReader();

        Response.Write("<div id='MPback'>");
        Response.Write("<div id='MPline'>");
        Response.Write("<div id='MPicon'>&nbsp;&nbsp;</div>");
        Response.Write("<div id='MPpage'>");
        Response.Write("<strong>最新留言</strong>&nbsp;[<a href=msg_add.ASPx >添加留言</a>]");
        Response.Write("</div>");
        Response.Write("<div id='MPfont'>");
        Response.Write("<SPAN id=fontCtrl title=設置字體大小>大字/<A class=b1 href='Javascript:setSize(12)'><font color='FFFD68'>小字</font></A></SPAN>");
        Response.Write("</div>");
        Response.Write("</div>");
        Response.Write("</div>");

        if (rs.HasRows)
        {
            while (rs.Read())
            {
                Response.Write("<div class='MPct'>");
                Response.Write("<div class='MPcta'>");
                Response.Write("<strong>&nbsp;標題:" + rs["title"].ToString() + "</strong>");
                Response.Write("</div>");
                Response.Write("<div class='MPctb'>");
                Response.Write("操作");
                Response.Write("</div>");
                Response.Write("</div>");

                Response.Write("<div class='MPcl'>");

                Response.Write("<div class='MPclc'>");
                Response.Write("" + rs["content"].ToString() + ""
);
                Response.Write("</div>");
                Response.Write("<div class='MPclinfo'>");
                Response.Write("</div>");

                Response.Write("<div class='MPclin'>");
                Response.Write("<div class='MPlefts'>");
                Response.Write("<strong>&nbsp;</strong>");
                Response.Write("</div>");
                Response.Write("<div style='width:100%; height:auto; font-weight:600; text-align:left; padding-left:30px'>回復:</div>");
                Response.Write("<div style='width:100%; height:auto; text-align:left; padding-left:40px;'>" + rs["recall"].ToString() + "</div>");
                Response.Write("<div class='MPrights'>");
                Response.Write("<strong>&nbsp;發表:</strong>" + rs["cname"].ToString() + "");
                Response.Write("<strong>&nbsp;時間:</strong>" + rs["addtime"].ToString() + "");
                Response.Write("</div></div>");

                Response.Write("</div>");
            }
        }
        con.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Button1.Enabled = true;
        Button2.Enabled = true;
        if (c >= n-pagesize)
        {
            Button1.Enabled = false;
        }

            Response.
Clear();
            c = c + pagesize;
            Response.Write(c.ToString());
            msg_top(c);
            Label1.Text = Convert.ToString((n - c) / pagesize + 1);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Button1.Enabled = true;
        Button2.Enabled = true;
        
        if (c <=pagesize*2)
        {
            Button2.Enabled = false;
        }

            Response.Clear();
            c = c - pagesize;
            Response.Write(c.ToString());
            msg_top(c);
        Label1.Text=Convert.ToString((n-c)/pagesize+1);
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Button1.Enabled = false;
        Button2.Enabled = true;
        c = n;
        Response.Clear();
        Response.Write(c.ToString());
        msg_top(c);
        Label1.Text = "1";
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Button1.Enabled = true;
        Button2.Enabled = false;
        c =n- (n / pagesize)*pagesize;
        Response.Clear();
        Response.Write(c.ToString());
        msg_top(c);
        Label1.Text = Convert.ToString(n/pagesize + 1);
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        int gx = Convert.ToInt32(TextBox1.Text);
        if (gx < 1 || gx > n / pagesize)
       
; {

        }
        else
        {
            Button1.Enabled = true;
            Button2.Enabled = true;
            if (c >= n - pagesize)
            {
                Button1.Enabled = false;
            }
            if (c <= pagesize * 2)
            {
                Button2.Enabled = false;
            }

            c = n - ((gx - 1) * pagesize);
            Response.Clear();
            Response.Write(c.ToString());
            msg_top(c);
            Label1.Text = Convert.ToString((n - c) / pagesize + 1);
        }
    }

ok了,雖然不是什麼傑作,不過好像效果還不錯,呵呵,效率都歸於top的魅力,呵呵

有興趣交個朋友的加我QQ,呵呵,648952046

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved