using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using manage.program;
using manage.aortorsTableAdapters;
using System.Data;
using System.Data.SqlClient;namespace manage
{
    public partial class Teacherchange : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {                if (Session["admin"] == null || (string)Session["admin"] == "")
                {
                    Response.Write("您还未登录!");
                    Response.Redirect("~/Login.aspx");
                }
                else
                {
                    ListBind();                    (Master.FindControl("labUser") as Label).Text = Session["admin"].ToString();                }            }        }        protected void update_Click(object sender, ImageClickEventArgs e)
        {
            communityTableAdapter update = new communityTableAdapter();
            update.UpdateteacherQuery(teahcer.Text, community2.Text);
            update.UpdateteacherQuery(DropDownList1.SelectedValue, community1.Text);
            Response.Write("更换成功");        }        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string co = DropDownList1.SelectedValue;
            communityTableAdapter update = new communityTableAdapter();            community2.Text = update.GetDataByteacher(co).Rows[0][0].ToString();
        }        protected void teacherddl_OnItemDataBound(object source, DataListCommandEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Label l = e.Item.FindControl("Label1") as Label;
                l.Text = DataBinder.Eval(e.Item.DataItem, "teacher").ToString();
                Button b = e.Item.FindControl("loginSuccess") as Button;
                b.CommandName = DataBinder.Eval(e.Item.DataItem, "teacher").ToString();            }
        }        protected void loginSuccess_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            teahcer.Text = b.CommandName;
        }
        private void ListBind()
        {
            string sql = "SELECT community, teacher FROM community WHERE (psss =1)";//自定义的SQL语句
            int recordcount;
            DataSet ds = GetPage(sql, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out recordcount);
            this.AspNetPager1.RecordCount = recordcount;
                        //.net的自定义分页
            PagedDataSource pds = new PagedDataSource();
            pds.AllowPaging = true;
            pds.PageSize = AspNetPager1.PageSize;
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            pds.DataSource = ds.Tables[0].DefaultView;
            teacherddl.DataSource = pds;
            teacherddl.DataBind();
            AspNetPager1.CustomInfoHTML = "记录总数:<b>" + AspNetPager1.RecordCount.ToString() + "</b>";
            AspNetPager1.CustomInfoHTML += " 总页数:<b>" + AspNetPager1.PageCount.ToString() + "</b>";
            AspNetPager1.CustomInfoHTML += " 当前页:<font><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
        }        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            ListBind();
        }        public DataSet GetPage(string sql, int currentPage, int pagesize, out int recordcount)//获取页面数据
        {
            SqlConnection cn = new SqlConnection("Data Source = CX-PC\\HAPPY;Initial Catalog =Community management;User ID=sa;");
            SqlDataAdapter ada = new SqlDataAdapter(sql, cn);
            DataSet ds = new DataSet();
            int startRow = (currentPage - 1) * pagesize;
            ada.Fill(ds, startRow, pagesize, "community");
            recordcount = GetPageRecord(sql);
            return ds;
        }        public int GetPagerCount(string sql, int pagesize)
        {
            int recordcount = GetPageRecord(sql);//方法见上
            int pagecount = recordcount / pagesize;
            return (recordcount % pagesize == 0 ? pagecount : pagecount + 1);
        }
        public int GetPageRecord(string sql)
        {
            SqlConnection cn = new SqlConnection("Data Source = CX-PC\\HAPPY;Initial Catalog =Community management;User ID=sa;");            sql = "select count(*) FROM community WHERE (psss =1)";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.Connection.Open();
            int recordcount = (int)cmd.ExecuteScalar();
            return recordcount;
        }
    }
}