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

public partial class Admin_Message_main : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            string keyword = Request.QueryString["Keywords"];
            string sql = "select * from A_Message where 1=1 ";
            if (keyword != "" && keyword != null)
            {
                sql += "and Title like'%" + keyword + "%' ";
            }
            //   数据源
            PagedDataSource Pgds = new PagedDataSource();

            DataTable table = new DataTable();

            if (ISDatabase.SelectDatebase() == 1)
                table = DbHelp.DataTableSql(sql);
            else
                table = DbHelperOleDb.DataTableSql(sql);
            //        
            Pgds.DataSource = table.DefaultView;
            //        设置允许分页
            Pgds.AllowPaging = true;
            //        每页显示为6行
            Pgds.PageSize = 6;
            //        显示总共页数
            //
            lblTotalPage.Text = Pgds.PageCount.ToString();
            //        当前页
            int CurrentPage;
            //        请求页码为不为null设置当前页，否则为第一页
            if (Request.QueryString["Page"] != null && Request.QueryString["Page"] != "")
            {

                CurrentPage = Convert.ToInt32(Request.QueryString["Page"]);
            }
            else
            {
                CurrentPage = 1;
            }
            //   当前页所引为页码-1
            Pgds.CurrentPageIndex = CurrentPage - 1;
            //   显示当前页码
            lblCurrentPage.Text = CurrentPage.ToString();
            //   如果不是第一页，通过参数Page设置上一页为当前页-1，否则不显示连接
            if (!Pgds.IsFirstPage)
            {
                // Request.CurrentExecutionFilePath为当前请求虚拟路径
                lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage - 1) + "&Keywords=" + keyword + "";
            }
            //        End If
            //   如果不是最后一页，通过参数Page设置下一页为当前页+1，否则不显示连接
            if (!Pgds.IsLastPage)
            {
                //    Request.CurrentExecutionFilePath为当前请求虚拟路径
                lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage + 1) + "&Keywords=" + keyword + "";
            }
            //   模板绑定数据源  
            RepeterList.DataSource = Pgds;
            RepeterList.DataBind();


        }

    }



    /// <summary>
    ///   关健字查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSearch_Click(object sender, EventArgs e)
    {

        string titlena = txtKeywords.Text;
        string sql;
        if (titlena == "" || titlena == null)
        {
            sql = "select * from A_Message";
        }
        else
        {
            sql = "select * from A_Message where Title like'%" + titlena + "%' ";
        }
        //   数据源
        PagedDataSource Pgds = new PagedDataSource();
        DataTable table = new DataTable();

        if (ISDatabase.SelectDatebase() == 1)
            table = DbHelp.DataTableSql(sql);
        else
            table = DbHelperOleDb.DataTableSql(sql);

        Pgds.DataSource = table.DefaultView;
        //        设置允许分页
        Pgds.AllowPaging = true;
        //        每页显示为6行
        Pgds.PageSize = 6;
        //        显示总共页数
        //
        lblTotalPage.Text = Pgds.PageCount.ToString();
        //        当前页
        int CurrentPage;
        //        请求页码为不为null设置当前页，否则为第一页
        if (Request.QueryString["Page"] != null && Request.QueryString["Page"] != "")
        {

            CurrentPage = Convert.ToInt32(Request.QueryString["Page"]);
        }

        else
        {

            CurrentPage = 1;
        }
        //   当前页所引为页码-1
        Pgds.CurrentPageIndex = CurrentPage - 1;
        //   显示当前页码
        lblCurrentPage.Text = CurrentPage.ToString();
        //   如果不是第一页，通过参数Page设置上一页为当前页-1，否则不显示连接
        if (!Pgds.IsFirstPage)
        {
            //            Request.CurrentExecutionFilePath为当前请求虚拟路径
            lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + CurrentPage + "&Keywords=" + titlena + "";
        }
        //        End If
        //   如果不是最后一页，通过参数Page设置下一页为当前页+1，否则不显示连接
        if (!Pgds.IsLastPage)
        {
            //    Request.CurrentExecutionFilePath为当前请求虚拟路径
            lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + CurrentPage + "&Keywords=" + titlena + "";
        }
        //   模板绑定数据源  
        RepeterList.DataSource = Pgds;
        RepeterList.DataBind();
        Response.Redirect(Request.CurrentExecutionFilePath + "?Page=1&Keywords=" + titlena + "");


    }



    protected void RepeterList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }


    /// <summary>
    ///   批量删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void btnDelete_Click(object sender, EventArgs e)
    {

        for (int i = 0; i < RepeterList.Items.Count; i++)
        {
            int id = Convert.ToInt32(((HiddenField)RepeterList.Items[i].FindControl("hidId")).Value);
            CheckBox cb = (CheckBox)RepeterList.Items[i].FindControl("chkId");
            if (cb.Checked)
            {
                string sql = "delete from A_Message where id=" + id + "";
                if (ISDatabase.SelectDatebase() == 1)
                    DbHelp.ExecuteSql(sql);
                else
                    DbHelperOleDb.ExecuteSql(sql);

            }
        }

        string sb = "<script>$(function () { $.ligerDialog.alert(\"批量删除成功啦！\", \"提示\", \"success\", function (result) { window.location.href ='main.aspx'; }); });</script>";
        Page.ClientScript.RegisterStartupScript(Page.GetType(), "Messager", sb.ToString());
    }
    //author:Angel_asp
    //QQ：709047174
    //website:www.angelasp.com



}