首页 > 学院 > 开发设计 > 正文

一个有趣的.net分页代码

2019-11-17 04:17:14
字体:
来源:转载
供稿:网友

一般由微软自带的分页效率极低

每次查询服务器端都会发送所有的查询记录到浏览器端

为此,很多人就用存储过程来提高效率,每次传送的都是一个页面的内容

 

今天自己弄了个分页的,呵呵

每次都传送一个页面的内容,不过没有用到存储过程,呵呵

 

看代码自己领悟吧,呵呵...

 

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

<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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表