datalist分页

后台代码:


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BLL;
using System.Data;
using System.Data.SqlClient;
using DBUtility;

public partial class online : System.Web.UI.Page
{

    private readonly string connectionString =
    System.Configuration.ConfigurationManager.ConnectionStrings["MW_PETConnectionString"].ToString();

    /// <summary>
    /// 数据适配器
    /// </summary>
    /// <param name="cmdText">sql命令</param>
    /// <param name="tablename">数据表的名称</param>
    /// <param name="sqlparams">sql命令参数</param>
    /// <returns>根据cmdText找到的数据集,若无返回null</returns>
    private DataSet Adapter(string cmdText, string tablename, params SqlParameter[] sqlparams)
    {
        SqlDataAdapter sdr = new SqlDataAdapter(cmdText, connectionString);
        if (sqlparams != null)
            sdr.SelectCommand.Parameters.AddRange(sqlparams);
        DataSet ds = new DataSet();
        try
        {
            sdr.Fill(ds, tablename);
            return ds;
        }
        catch
        {
            return null;
        }
        finally
        {
        }
    }

    //留言板绑定
    private bool PageBind(int CurrPage, string tablename, string id)
    {
        ViewState["CURRPAGE"] = CurrPage.ToString().Trim();
        string cmdText =
            "select  *  from problem as p inner join client as c on p.cid=c.cid  order by p.pid desc";
        PagedDataSource pagedDataSource = new PagedDataSource();
        DataSet ds = Adapter(cmdText, tablename, null);
        if (ds == null) return false;

        pagedDataSource.DataSource = ds.Tables[tablename].DefaultView;
        pagedDataSource.AllowPaging = true;
        pagedDataSource.PageSize = 10;
        if (CurrPage > pagedDataSource.PageCount || CurrPage < 1)
        {
            return false;
        }

        pagedDataSource.CurrentPageIndex = CurrPage - 1;
        LabelCurrPage.Text = "第" + CurrPage.ToString() + "页";
        LabelTotalPage.Text = "共" + pagedDataSource.PageCount.ToString() + "页";

        ButtonPrePage.Enabled = true;
        ButtonNextPage.Enabled = true;
        if (CurrPage == 1)
        {
            ButtonPrePage.Enabled = false;
        }
        if (CurrPage == pagedDataSource.PageCount)
        {
            ButtonNextPage.Enabled = false;
        }

        //每一行的id(主键)绑定
        DataList1.DataKeyField = id;
        //DataList1绑定数据源pageDataSource
        DataList1.DataSource = pagedDataSource;
        DataList1.DataBind();
        return true;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Literal l = Master.FindControl("ltr_title") as Literal;
            l.Text ="在线门诊";
            PageBind(1, "problem", "pid");
            
           
            if (Session["account"] == null)
            {
                BtnTJ.Enabled = false;
                online_f_s_r.Visible = true;
                online_f_s_m.Visible = false;
            }
            else if(Session["account"] != null)
            {
                BLL.Client cl = new Client();
                account.InnerHtml = cl.GetClient(Session["account"].ToString()).Account;

                online_f_s_r.Visible = false;
            }
        }
    }
    protected void BtnTJ_Click(object sender, EventArgs e)
    {
        BLL.Client cl = new Client();
        int cid = cl.GetClient(Session["account"].ToString()).Cid;

        Problem problem = new Problem();
        problem.Problem_insert(cid, tiwen.Value.Trim(), DateTime.Now.ToString("yyyy-MM-dd HH:mm"), false).ToString();

        
        if (ViewState["CURRPAGE"] == null) return;
        int CurrPage = Convert.ToInt32(ViewState["CURRPAGE"].ToString());
        PageBind(CurrPage, "problem", "pid");
    }

    protected void ButtonPrePage_Click(object sender, EventArgs e)
    {
        //显示前一页
        if (ViewState["CURRPAGE"] == null) return;
        int CurrPage = Convert.ToInt32(ViewState["CURRPAGE"].ToString());
        PageBind(CurrPage - 1, "problem", "pid");
    }

    protected void ButtonNextPage_Click(object sender, EventArgs e)
    {
        //显示后一页
        if (ViewState["CURRPAGE"] == null) return;
        int CurrPage = Convert.ToInt32(ViewState["CURRPAGE"].ToString());
        PageBind(CurrPage + 1, "problem", "pid");
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataList child = (DataList)e.Item.FindControl("DataList2");
            Label l = (Label)e.Item.FindControl("Label8");
            string cmdStr = "select  * from Reply where pid=@pid";
            try
            {
                DataSet ds = DBUtility.SqlHelper.ExecuteDataSet(connectionString, cmdStr, new SqlParameter[]
                    {
                        new SqlParameter("@pid",l.Text)
                    });
                child.DataSource = ds.Tables[0].DefaultView;
                child.DataBind();
            }
            catch
            {
                throw new Exception("数据库连接失败1");
            }
        }
    }
}

前台:

<%@ Page Title="在线门诊" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="online.aspx.cs" Inherits="online" Debug="true" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
    <style type="text/css">
        p
        {
            text-align: left;
        }
        
        #online_f
        {
            height: 270px;
        }
        #online_f_f
        {
            height: 153px;
        }
        #online_f_s
        {
            height: 50px;
        }
        .tiwen
        {
            width: 550px;
            height: 120px;
            margin-top: 0px;
        }
        #online_s
        {
            height: auto;
            width: 680px;
            padding-bottom: 20px;
        }
        .stuinfo
        {
            float: left;
            height: 25px;
        }
        .theme
        {
            font-size: 12px;
            color: #4C5D77;
            text-align: left;
            float: left;
            width: 400px;
        }
        .subtime
        {
            font-size: 12px;
            color: #999999;
            float: left;
            width: 200px;
            text-align: center;
        }
        
        .fenye
        {
            margin-top: 20px;
        }
        
        .fenye a
        {
            font-size: 12px;
            color: #005eac;
            text-decoration: none;
            margin-right: 5px;
        }
        .fenye a:hover
        {
            font-size: 12px;
            color: Red;
            text-decoration: underline;
            cursor: pointer;
        }
        .fen
        {
            border-right: 1px solid #E1e1e1;
            font-size: 12px;
            color: #FF6633;
        }
        .fen1
        {
            border-right: none;
            font-size: 12px;
            color: #FF6633;
        }
        .counttime
        {
            text-align: right;
            font-size: 12px;
            color: #666666;
        }
        .reply
        {
            margin-top: 10px;
            text-align: left;
            float: right;
            width: 530px;
            height: auto;
            padding-top: 20px;
        }
        .contents
        {
            font-size: 14px;
            color: #FF6633;
            margin-left: 27px;
            text-align: left;
            float: left;
            width: 500px;
            height: auto;
        }
        .submittime
        {
            margin-left: 25px;
            font-size: 12px;
            color: #999999;
            float: left;
            width: 500px;
            text-align: right;
        }
        #TextArea1
        {
            height: 74px;
            width: 448px;
        }
        .haveReplied
        {
            display: none;
        }
        .noReplied
        {
            left: 310px;
            top: -10px;
            position: absolute;
            display: block;
        }
        .huifu:hover
        {
            color: #005eac;
            cursor: pointer;
        }
        .huifu
        {
            cursor: default;
        }
        .huifu1:hover
        {
            cursor: pointer;
            color: Red;
        }
        .huifu1
        {
            font-size: 12px;
            color: #A0A0A0;
            cursor: default;
        }
        .dl1
        {
            margin-left: auto;
            margin-right: auto;
        }
        .pright
        {
            width: 520px;
            margin-left: 5px;
            float: left;
            height: auto;
        }
        .pleft
        {
            border-right: 1px solid #CCCCCC;
            float: left;
            width: 120px;
            height: 155px;
            margin-top: 5px;
            margin-bottom: 5px;
            margin-left: 5px;
        }
        .first
        {
            background-color: #E7F1F8;
            float: left;
            width: 540px;
            height: 20px;
        }
        .second
        {
            float: left;
            width: 500px;
            height: auto;
        }
        .second p
        {
            text-align: left;
            color: #005eac;
            margin-left: 20px;
            margin-top: 20px;
        }
        #online_f_s_l
        {
            text-align: right;
            width: 373px;
            float: left;
        }
        #online_f_s_r
        {
            text-align: center;
            width: 271px;
            float: left;
            height: 19px;
        }
        #online_f_s a
        {
            color: #034AF3;
            text-decoration: underline;
        }
        #online_f_s a:hover
        {
            color: #034AF3;
            text-decoration: underline;
        }
        .online_f_s_m
        {
            text-align: center;
            width: 271px;
            float: left;
            height: 19px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="page_main" runat="Server">
    <script type="text/javascript">
        function counts(_this) {
            var content = _this.value;
            if (content.length > 200) {
                _this.value = content.substring(0, 200);
                document.getElementById("shengyu").innerHTML = "0";
            }
            else {
                document.getElementById("shengyu").innerHTML = 200 - content.length;
            }
        }
        function changecolor(id) {
            var pa = document.getElementById(id);
            pa.style.backgroundColor = "#F1F9F8";
        }
        function backcolor(id) {
            var pa = document.getElementById(id);
            pa.style.backgroundColor = "transparent";
        }
        function pre() {
            document.getElementById('<%=ButtonPrePage.ClientID %>').click();
        }
        function next() {
            document.getElementById('<%=ButtonNextPage.ClientID %>').click();
        }
        function IsReplied(id) {
            document.getElementById(id)
        }

        function count(id) {
            var text = document.getElementById(id);
            var str = text.value;
            if (str.length > 200) {
                text.value = str.substring(0, 200);
            }
            else {
                document.getElementById("zishu" + id).innerHTML = 200 - str.length;
            }

        }
    </script>
    <div class="online">
        <div id="online_f">
            <p style="margin-left: 30px;">
                提问:
            </p>
            <div style="color: #666666; font-size: 12px; height: 16px; width: 609px; text-align: right;
                margin-bottom: 5px;">
                还剩余 <span id="shengyu" style="color: red">200</span>字
            </div>
            <div id="online_f_f">
                <textarea id="tiwen" name="S1" class="tiwen" οnkeyup="counts(this)" runat="server"></textarea></div>
            <div id="online_f_s">
                <div id="online_f_s_l">
                    <asp:Button ID="BtnTJ" runat="server" Text="提问" OnClick="BtnTJ_Click" />
                </div>
                <div id="online_f_s_m" class="online_f_s_m" runat="server">
                    欢迎您  
                    <span id="account" runat="server"></span>,  
                    <a href="client.aspx">查看</a>我的信息
                </div>
                <div id="online_f_s_r" runat="server">
                    提问请<a href="login.aspx">登录</a>,如果没有账号请先<a href="register.aspx">注册</a>
                </div>
            </div>
        </div>
        <%--计算时间--%>
        <div class="counttime">
            <script type="text/javascript" language="javascript">
                //用时计算
                //计时开始
                var tStart = new Date()
                StarTime_S = tStart.getTime()
                //计时结束
            </script>
            <span style="margin-right: 10px;">
                <script type="text/javascript" language="javascript">
                    var tEnd = new Date()
                    EndTime_S = tEnd.getTime()
                    document.write("用时:" + (EndTime_S - StarTime_S) * 0.001 + "秒 ");
                </script>
            </span>
        </div>
        <div id="online_s">
            <asp:DataList ID="DataList1" runat="server" DataKeyField="id" Height="387px" OnItemDataBound="DataList1_ItemDataBound"
                Width="680" CssClass="dl1">
                <ItemTemplate>
                    <div id='<%# Eval("pid") %>' class="parent" οnmοuseοut="backcolor('<%# Eval("pid") %>')"
                        οnmοuseοver="changecolor('<%# Eval("pid") %>')" style="border-bottom: 1px dashed #CCCCCC;
                        margin-bottom: 5px; margin-top: 5px; height: auto; float: left;">
                        <div class="pleft">
                            <div class="stuinfo">
                                姓名 :<asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>' />
                            </div>
                        </div>
                        <div class="pright">
                            <div class="first">
                                <div class="subtime">
                                    发表于:<asp:Label ID="submitTimeLabel" runat="server" Text='<%# Eval("submitTime")%>' />
                                </div>
                            </div>
                            <div class="second">
                                <p>
                                    <asp:Label ID="contentsLabel" runat="server" Text='<%# Eval("contents") %>' />
                                </p>
                            </div>
                        </div>
                        <div class="reply">
                            <div style="float: left; width: 400px; height: 20px;">
                                <img src="img/admin/online_member.gif" />
                                <a style="font-size: 12px">回复:</a>
                            </div>
                            <asp:DataList ID="DataList2" runat="server" Width="530px">
                                <ItemTemplate>
                                    <div class="contents">
                                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("contents") %>'></asp:Label>
                                    </div>
                                    <div class="submittime">
                                        回复于:<asp:Label ID="Label6" runat="server" Text='<%# Eval("subtime") %>'></asp:Label>
                                    </div>
                                </ItemTemplate>
                            </asp:DataList>
                        </div>
                        <div style="display: none;">
                            <asp:Label ID="Label8" runat="server" Text='<%# Eval("pid") %>'></asp:Label>
                            <asp:Label ID="Label7" runat="server" Text='<%#Eval("IsReplied") %>'></asp:Label>
                        </div>
                    </div>
                </ItemTemplate>
            </asp:DataList>
            <div class="fenye">
                <a οnclick="pre()" style="border-right: 1px solid #E1E1E1"><span style="margin-right: 5px;">
                    上一页</span></a> <a οnclick="next()" style="border-right: 1px solid #E1E1E1"><span
                        style="margin-right: 5px;">下一页</span></a>
                <asp:Label ID="LabelCurrPage" runat="server" Text="Label" CssClass="fen"></asp:Label>
                <asp:Label ID="LabelTotalPage" runat="server" Text="Label" CssClass="fen1"></asp:Label>
                <div style="display: none;">
                    <!--点击可连接到前一页,可将Button换成LinkButton等的触发-->
                    <asp:Button ID="ButtonPrePage" runat="server" Text="上一页" OnClick="ButtonPrePage_Click"
                        BorderStyle="None" CssClass="have" />
                    <!--点击可连接到后一页,可将Button换成LinkButton等的触发-->
                    <asp:Button ID="ButtonNextPage" runat="server" Text="下一页" OnClick="ButtonNextPage_Click"
                        BorderStyle="None" CssClass="have" />
                </div>
            </div>
        </div>
    </div>
    <div id="test">
    </div>
</asp:Content>



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值