using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class dairy : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lbclass.Text = Encryption.Decrypt(Request.QueryString["daiclass"]);
            curpage.Text = Convert.ToString(1);
            topbind();
            fanye();            //翻页函数
        }
    }

    protected void topbind()
    {
        SqlConnection one = conn.sqlconn();
        
        PagedDataSource ps = new PagedDataSource();
        SqlDataAdapter sda = new SqlDataAdapter("select top 3 * from dairy where dtop=1 order by id desc", one);    //建立容器
        DataSet ds = new DataSet();        //数据设置对象
        try
        {
            one.Open();
            sda.Fill(ds, "order");
            ps.DataSource = ds.Tables["order"].DefaultView;
            ps.AllowPaging = false;
            ps.PageSize = 3;
            DataList2.DataSource = ps;
            DataList2.DataBind();

        }
        catch (System.Exception ee)
        {
            Response.Write(ee.Message.ToString());
        }
        finally
        {
            DataList2.Dispose();
            ds.Dispose();
            sda.Dispose();
            one.Dispose();
            one.Close();
        }
    }

    //短日期处理
    protected string short_time(object times)
    {
        stringdo shortime = new stringdo();
        return shortime.short_time(times);
    }

    protected void fanye()       //上一步，下一步，首尾页翻页函数
    {
        SqlConnection one = conn.sqlconn();
        PagedDataSource ps = new PagedDataSource();
        SqlDataAdapter sda = new SqlDataAdapter("select * from dairy where dclass='" + lbclass.Text + "' and dtop=0 order by id desc", one);    //建立容器
        DataSet ds = new DataSet();        //数据设置对象
        try
        {
            one.Open();
            sda.Fill(ds, "order");
            ps.DataSource = ds.Tables["order"].DefaultView;
            ps.AllowPaging = true;
            ps.PageSize = 4;
            ps.CurrentPageIndex = Convert.ToInt32(curpage.Text) - 1;
            allpage.Text = Convert.ToString(ps.PageCount);
            DataList1.DataSource = ps;

            //初始化下拉列表－－－－－－－－－－
            int i = 1;
            pagelist.Items.Clear();

            while (i <= ps.PageCount)
            {
                pagelist.Items.Add(new ListItem(Convert.ToString(i), Convert.ToString(i)));
                i = i + 1;
            }
            pagelist.SelectedIndex = int.Parse(curpage.Text) - 1;
            //结束－－－－－－－－－－－－－－－－－

            first.Enabled = true;
            pre.Enabled = true;
            next.Enabled = true;
            last.Enabled = true;
            if (curpage.Text == "1")
            {
                first.Enabled = false;
                pre.Enabled = false;
            }
            if (curpage.Text == allpage.Text)
            {
                next.Enabled = false;
                last.Enabled = false;
            }
            DataList1.DataBind();

        }
        catch (System.Exception ee)
        {
            Response.Write(ee.Message.ToString());
        }
        finally
        {
            DataList1.Dispose();
            ds.Dispose();
            sda.Dispose();
            one.Dispose();
            one.Close();
        }

    }

    protected void first_Click(object sender, EventArgs e)
    {
        curpage.Text = Convert.ToString(1);
        fanye();
    }
    protected void pre_Click(object sender, EventArgs e)
    {
        curpage.Text = Convert.ToString(Convert.ToInt32(curpage.Text) - 1);
        fanye();
    }
    protected void next_Click(object sender, EventArgs e)
    {
        curpage.Text = Convert.ToString(Convert.ToInt32(curpage.Text) + 1);
        fanye();
    }
    protected void last_Click(object sender, EventArgs e)
    {
        curpage.Text = allpage.Text;
        fanye();
    }

    protected void downchange(object sender, EventArgs e)   //下拉列框的翻页
    {
        curpage.Text = Convert.ToString(pagelist.SelectedValue);
        fanye();
    }

    //字符串截取
    protected string strcut(string mystr,int len)
    {
        stringdo str = new stringdo();
        string restr=str.CutString(mystr, len);
        return restr;
    }

    //判断是否最新的日志
    protected string isnew(string times)
    {
        string temp="";
        if (times == DateTime.Now.ToShortDateString().ToString())
        {
            temp = "&nbsp;&nbsp;<img src=\"images/new.gif\" width=\"27\" height=\"11\" />";
        }
        return temp;
    }

    //评论数目
    protected int coupl(int drid)
    {
        SqlConnection one = conn.sqlconn();
        int cou=0;
        try
        {
            one.Open();
            string sql = "select count(*) from redai where drid=" + drid;
            SqlCommand cmd = new SqlCommand(sql, one);
            cou = Convert.ToInt16(cmd.ExecuteScalar());
        }
        catch { }
        finally 
        {
            one.Close();
        }
        return cou;
    }
}
