我的第一个asp.net分页代码(不用gridview控件,手工分页)

第一次写分页的代码,写得有点糟,请多指点!另外这里还有一个table是发帖用的,点击发帖按钮后可以在网页中显示这个table,不点击时网页中没有显示这个表,我只是随手沾上的。

这是前台的代码

<!--帖子的有关内容-->
<div style="width: 970px; height: auto; text-align: left;">
    <asp:Table ID="tietable" runat="server" Width="100%" BorderWidth="1px" BorderColor="Gray" GridLines="Horizontal">
        <asp:TableRow>
            <asp:TableCell Width="55%" Text="标题"></asp:TableCell>
            <asp:TableCell Width="15%" Text="发帖人/发帖时间"></asp:TableCell>
            <asp:TableCell Width="15%" Text="回复量/浏览量"></asp:TableCell>
            <asp:TableCell Text="最后一次访问时间"></asp:TableCell>
        </asp:TableRow>
    </asp:Table>
</div>
<!--发帖按钮-->
<div style="width: 970px; height: 30px; text-align: left; vertical-align: middle; line-height: 30px;">
    <div style="width: 120px; float: left">
    <asp:ImageButton ID="fatie" runat="server" ImageUrl="image/zhuye_button.gif" OnClick="imagebutton1_click" /></div>
    <!--这里可以添加页数指引-->
    <table cellpadding="0" cellspacing="0" style="float: right;">
        <tr>
            <td style="width: 120px;">
                <asp:Label ID="label5" runat="server" Font-Size="10pt" Text="当前页为【"></asp:Label>
                <asp:Label ID="label7" runat="server" Font-Size="10pt" Text="1"></asp:Label>
                <asp:Label ID="label3" runat="server" Font-Size="10pt" Text="】"></asp:Label>
            </td>
            <td style="width: 40px;">
	<asp:LinkButton ID="linkbutton2" runat="server" Font-Size="10pt" ForeColor="red" OnClick="linkbutton2_click">首页</asp:LinkButton>
            </td>
            <td style="width: 30px;">
                <asp:LinkButton ID="linkbutton3" Width="42px" runat="server" Font-Size="10pt" ForeColor="red" OnClick="linkbutton3_click">上一页</asp:LinkButton>
            </td>
            <td style="width: 48px;">
                <asp:LinkButton ID="linkbutton4" runat="server" Font-Size="10pt" ForeColor="red" Width="48px" OnClick="linkbutton4_click">下一页</asp:LinkButton>
            </td>
            <td style="width: 49px;">
                <asp:LinkButton ID="linkbutton5" runat="server" Font-Size="10pt" ForeColor="red" Width="29px" OnClick="linkbutton5_click">尾页</asp:LinkButton>
            </td>
            <td style="width: 120px;" align="center">
<asp:Label ID="label1" runat="server" Text="总页数【"Font-Size="10pt"Width="52px"></asp:Label>
                <asp:Label ID="label2" runat="server" Font-Size="10pt"></asp:Label>
                <asp:Label ID="label4" runat="server" Font-Size="10pt" Text="】"></asp:Label>
            </td>
        </tr>
    </table>
</div>
<!--隐藏的发帖table-->
<asp:Table ID="fatiebgd" runat="server" Width="961px" Height="200px" GridLines="Both" HorizontalAlign="center" BackColor="green" Visible="false" Style="margin: 0px">
    <asp:TableRow Width="1000px">
        <asp:TableCell>
            <asp:Table ID="fatietable" runat="server" Width="961px" BorderWidth="1px" Height="200px" GridLines="Both" BorderColor="LightYellow" BorderStyle="Solid" BackColor="white">
                <asp:TableRow>
                    <asp:TableCell ID="TableCell1" runat="server" Width="15%" Height="20px" Text="标题:"></asp:TableCell>
                    <asp:TableCell ID="TableCell2" runat="server" HorizontalAlign="Left">
             <asp:TextBox runat="server" ID="headline" Width="85%"></asp:TextBox></asp:TableCell>
                </asp:TableRow>
                <asp:TableRow>
                    <asp:TableCell ID="TableCell3" runat="server" Width="15%" Height="160px" Text="内容:" VerticalAlign="Middle"></asp:TableCell>
                    <asp:TableCell ID="TableCell4" runat="server">
                   <asp:TextBox Width="99%" runat="server" ID="postscontents" TextMode="MultiLine" Rows="9"></asp:TextBox></asp:TableCell>
                </asp:TableRow>
                <asp:TableRow>
                    <asp:TableCell ColumnSpan="2" HorizontalAlign="center">
                        <asp:Button ID="OK" Text="提交" Width="80px" runat="server" OnClick="ok_click" />
            <asp:Button ID="kong" runat="server" Width="80px" Text="重置" OnClick="kong_click" />
                    </asp:TableCell>
                </asp:TableRow>
            </asp:Table>
        </asp:TableCell></asp:TableRow>
</asp:Table>

 这是后台的代码:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dbBind();
        }
    }

    protected void dbBind()
    {
        SqlConnection conn = new SqlConnection(CommonMethods.connstring);
        SqlCommand cmd = new SqlCommand("select 发帖时间 from title", conn);
        SqlDataReader rd = null;
        conn.Open();
        rd = cmd.ExecuteReader();
        int rowdata = 0;
        while (rd.Read())
        {
            rowdata++;
        }
        conn.Close();
        int allpage = 0;
        if (rowdata / 3 == 0)
        {
            allpage = rowdata / 3;
        }
        else
        {
            allpage = rowdata / 3 + 1;
        }
        this.label2.Text = allpage.ToString();
        int curpage = Convert.ToInt32(this.label7.Text);
        this.linkbutton2.Enabled = true;
        this.linkbutton3.Enabled = true;
        this.linkbutton4.Enabled = true;
        this.linkbutton5.Enabled = true;
        if (curpage == 1)//如果等于1则上一页和首页禁用
        {
            this.linkbutton2.Enabled = false;
            this.linkbutton3.Enabled = false;
        }
        if (curpage == Convert.ToInt32(this.label2.Text))//如果等于尾页则下一页和尾页链接禁用
        {
            this.linkbutton4.Enabled = false;
            this.linkbutton5.Enabled = false;
        }
 string sql = "select  top 3 标题,用户名,发帖时间,回复量,浏览量,最后一次访问时间 from title where 最后一次访问时间 NOT IN (select top " + 3 * (curpage - 1) + " 最后一次访问时间 from title order by 最后一次访问时间 desc) order by 最后一次访问时间 desc";
        SqlCommand command = new SqlCommand(sql, conn);
        conn.Open();
        rd = command.ExecuteReader();
        for (int i = 1; i < tietable.Rows.Count; i++)
        {
            tietable.Rows[i].Cells.Clear();
        }
        while (rd.Read())
        {
            TableRow row = new TableRow();
            TableCell c1 = new TableCell();
            TableCell c2 = new TableCell();
            TableCell c3 = new TableCell();
            TableCell c4 = new TableCell();
            c1.Text = rd["标题"].ToString();
            c2.Text = rd["用户名"].ToString() + "<br/>" + rd["发帖时间"].ToString();
            c3.Text = rd["回复量"].ToString() + "/" + rd["浏览量"].ToString();
            c4.Text = rd["最后一次访问时间"].ToString();
            row.Cells.Add(c1);
            row.Cells.Add(c2);
            row.Cells.Add(c3);
            row.Cells.Add(c4);
            tietable.Rows.Add(row);
        }
    }

    protected void linkbutton2_click(object sender, EventArgs e)
    {
        this.label7.Text = "1";
        this.dbBind();
    }

    protected void linkbutton3_click(object sender, EventArgs e)
    {
        this.label7.Text = Convert.ToString(Convert.ToUInt32(this.label7.Text) - 1);
        this.dbBind();
    }

    protected void linkbutton4_click(object sender, EventArgs e)
    {
        this.label7.Text = Convert.ToString(Convert.ToInt32(this.label7.Text) + 1);
        this.dbBind();
    }

    protected void linkbutton5_click(object sender, EventArgs e)
    {
        this.label7.Text = this.label2.Text;
        this.dbBind();
    }
    protected void imagebutton1_click(object sender, EventArgs e)
    {
        fatiebgd.Visible = true;//让发帖的表格显示在网页中
    }
    protected void ok_click(object sender, EventArgs e)
    {
        if (headline.Text==null||headline.Text==""||postscontents.Text==null||postscontents.Text=="")
        {
            Response.Write("<script>alert('标题和内容任一项都不能为空!');</script>");
        }
    }
    protected void kong_click(object sender, EventArgs e)
    {
        headline.Text = "";
        postscontents.Text = "";
    }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值