/*
'' ============================================================================================
'' 项目名称：风越项目范例ASPX单层框架
'' 项目版本：V1.0
'' 项目描述：
'' 文件名称：YongHuXinXi_search.aspx
'' 文件描述：信息搜索文件 （用户信息）
'' 公司名称：中极网盛
'' 开发人员：刘怒
'' 创建日期：2008-8-7 21:34:32
'' 修订日期：2009-03-10 09:10:30
'' 版权信息：Copyright (C) 2008

'' 项目升级时需读取以下生成工具的版权信息
'' 生成工具：风越代码生成器 [FireCode Creator]
'' 当前版本：通用版 V1.8
'' 官方网站：http://www.sino8848.com
'' ============================================================================================
*/

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 FypSystem.Common;
using FypSystem.DBManager;

public partial class YongHuXinXi_search : System.Web.UI.Page
{
    /// <summary>
    /// 数据库是否为空
    /// </summary>
    bool bEmptyDataTable = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = WebPage.GetConfig("fypWebName");

        if (!Page.IsPostBack)
        {
            Initialize();
            FillGrid();
        }
    }


    /// <summary>
    /// 导入搜索框的默认值、数据
    /// </summary>
    private void Initialize()
    {
        //标头文字
        string strHeader = @"<a href='YongHuXinXi_add.aspx' target='_blank'> 新增 </a> ";
        this.GridView1.Columns[5].HeaderText = strHeader;
        this.GridView1.Columns[5].FooterText = strHeader;

        //绑定属性
        

        using (FypSystem.DBManager.Access dbManager = new FypSystem.DBManager.Access())


        {

            //填充下拉框
            
            
            

            
            string strSql_USER_COMPANY = "select [COMPANY_NAME], [COMPANY_ID] from [COMPANY]";
            dbManager.FillListControl(this.USER_COMPANY, strSql_USER_COMPANY, "COMPANY_NAME", "COMPANY_ID", "--请选择--", "");
            

            
            
            
            
            

            

            
            
            
            if (this.USER_COMPANY.SelectedIndex >= 0 && this.USER_COMPANY.SelectedValue.Length > 0)
            {
                string strWhere_USER_DEPT = "";
                strWhere_USER_DEPT = " Where [COMPANY_ID]=" + this.USER_COMPANY.SelectedValue + "";
                string strSql_USER_DEPT = "select [DEPARTMENT_NAME], [DEPARTMENT_ID] from [DEPARTMENT] " + strWhere_USER_DEPT;
                dbManager.FillListControl(this.USER_DEPT, strSql_USER_DEPT, "DEPARTMENT_NAME", "DEPARTMENT_ID", "--请选择--", "");
            }
            

            
            

            
            
            


        }
    }







    protected void USER_COMPANY_SelectedIndexChanged(object sender, EventArgs e)
    {
        //填充下拉框
        if (this.USER_COMPANY.SelectedValue.Length > 0)
        {

            using (FypSystem.DBManager.Access dbManager = new FypSystem.DBManager.Access())


            {
                string strWhere = "";
                
                
                strWhere = " Where [COMPANY_ID]=" + this.USER_COMPANY.SelectedValue + "";
                

                

                dbManager.FillListControl(this.USER_DEPT, "select [DEPARTMENT_NAME], [DEPARTMENT_ID] from [DEPARTMENT] " + strWhere, "DEPARTMENT_NAME", "DEPARTMENT_ID", "--请选择--", "");
            }
        }
    }






    //执行搜索
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (this.Page.IsValid)
        {
            string strWhere = "";
            

            
            if (this.USER_ID.Text.Length > 0)
                strWhere += " And [USER_ID] like '%" + this.USER_ID.Text + "%' ";
            

            
            

            
            if (this.USER_NAME.Text.Length > 0)
                strWhere += " And [USER_NAME] like '%" + this.USER_NAME.Text + "%' ";
            

            
            

            
            if (this.USER_PWD.Text.Length > 0)
                strWhere += " And [USER_PWD] like '%" + this.USER_PWD.Text + "%' ";
            

            
            

            

            
            if (this.USER_COMPANY.Text.Length > 0)
                strWhere += " And [USER_COMPANY] = " + this.USER_COMPANY.SelectedValue + " ";
            
            

            

            
            if (this.USER_DEPT.Text.Length > 0)
                strWhere += " And [USER_DEPT] = " + this.USER_DEPT.SelectedValue + " ";
            
            

            if (strWhere.Trim().Length > 4)
                strWhere = " Where " + strWhere.Substring(4);

            ViewState["WhereCondition"] = strWhere;
            FillGrid();
        }
    }


    /// <summary>
    /// 填充数据网格
    /// </summary>
    private void FillGrid()
    {
        
        using (FypSystem.DBManager.Access dBManager = new Access())
        
        
        {
            string strOrderBy = "";
            string strSortExpression = StringExtend.NullToEmptyStr(ViewState["SortExpression"]);
            string strSortDirection = StringExtend.NullToEmptyStr(ViewState["SortDirection"]);
            string strWhere = StringExtend.NullToEmptyStr(ViewState["WhereCondition"]);

            if (strSortExpression.Length > 0)
                strOrderBy = " Order By [" + strSortExpression + "] " + strSortDirection;
            else
                strOrderBy = " Order By [USER_ID] desc, [USER_NAME] desc";

            string strSql = "select [USER_ID], [USER_NAME], [USER_PWD], [USER_COMPANY], [USER_DEPT] from [USER] " + strWhere + strOrderBy;
            //this.Response.Write(strSql);
            DataTable tabSrouce = new DataTable();
            tabSrouce = dBManager.GetDataTable(strSql);

            //如数据库中无记录，则生成一条空值记录，以便显示表格
            if (tabSrouce.Rows.Count == 0)
            {
                DataRow rowEmpty = tabSrouce.NewRow();
                //rowEmpty[0] = DBNull.Value;
                tabSrouce.Rows.Add(rowEmpty);
                bEmptyDataTable = true;
            }

            
            this.GridView1.DataSource = tabSrouce;
            this.GridView1.DataBind();
            tabSrouce.Dispose();
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //如数据库中无记录，设置临时行状态
            if (bEmptyDataTable)
            {
                e.Row.Enabled = false;
                return;
            }

            //鼠标进入、离开表格行时设置其背景色
            if (e.Row.RowState == DataControlRowState.Normal)
            {
                e.Row.Attributes.Add("onmouseover", "SetTRcolorOver(this, 'tableContent001', 'tableContentOn001', 'tableContentClick001', 'tableContentClickOn001')");
                e.Row.Attributes.Add("onmouseout", "SetTRcolorOut(this, 'tableContent001', 'tableContentOn001', 'tableContentClick001', 'tableContentClickOn001')");
                e.Row.Attributes.Add("onclick", "SetTRcolorClick(true, " + this.GridView1.ClientID + ", this, '', '', true, 'tableContent001', 'tableContentOn001', 'tableContentClick001', 'tableContentClickOn001')");
            }
            else if (e.Row.RowState == DataControlRowState.Alternate)
            {
                e.Row.Attributes.Add("onmouseover", "SetTRcolorOver(this, 'tableContentAlternating001', 'tableContentAlternatingOn001', 'tableContentClick001', 'tableContentClickOn001')");
                e.Row.Attributes.Add("onmouseout", "SetTRcolorOut(this, 'tableContentAlternating001', 'tableContentAlternatingOn001', 'tableContentClick001', 'tableContentClickOn001')");
                e.Row.Attributes.Add("onclick", "SetTRcolorClick(true, " + this.GridView1.ClientID + ", this, '', '', true, 'tableContentAlternating001', 'tableContentAlternatingOn001', 'tableContentClick001', 'tableContentClickOn001')");
            }

            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {

                //设置删除提示
                if (e.Row.FindControl("LinkButton_Del") != null)
                    ((LinkButton)e.Row.FindControl("LinkButton_Del")).Attributes.Add("onclick", "javascript:return confirm('是否确认删除本条记录?')");

                //格式化输出字段
        
                using (FypSystem.DBManager.Access dbManager = new FypSystem.DBManager.Access())
        
                {

                    object objLab_USER_ID = e.Row.FindControl("lab_USER_ID_Show");
                    if (objLab_USER_ID is Label)
                    {
                        string str_USER_ID_value = ((Label)objLab_USER_ID).Text;
                    
                        

                        

                        

                        

                        

                        

                        

                        

                        
                        ((Label)objLab_USER_ID).Text = WebString.HtmlEncodeExtend(str_USER_ID_value);
                        

                        
                    

                    
                    }

                    object objLab_USER_NAME = e.Row.FindControl("lab_USER_NAME_Show");
                    if (objLab_USER_NAME is Label)
                    {
                        string str_USER_NAME_value = ((Label)objLab_USER_NAME).Text;
                    
                        

                        

                        

                        

                        

                        

                        

                        

                        
                        ((Label)objLab_USER_NAME).Text = WebString.HtmlEncodeExtend(str_USER_NAME_value);
                        

                        
                    

                    
                    }

                    object objLab_USER_PWD = e.Row.FindControl("lab_USER_PWD_Show");
                    if (objLab_USER_PWD is Label)
                    {
                        string str_USER_PWD_value = ((Label)objLab_USER_PWD).Text;
                    
                        

                        

                        

                        

                        

                        

                        

                        

                        
                        ((Label)objLab_USER_PWD).Text = WebString.HtmlEncodeExtend(str_USER_PWD_value);
                        

                        
                    

                    
                    }

                    object objLab_USER_COMPANY = e.Row.FindControl("lab_USER_COMPANY_Show");
                    if (objLab_USER_COMPANY is Label)
                    {
                        string str_USER_COMPANY_value = ((Label)objLab_USER_COMPANY).Text;
                    

                    
                        if (str_USER_COMPANY_value.Length > 0)
                        {
                            string strSql = "select [COMPANY_NAME] from [COMPANY] where [COMPANY_ID]=" + str_USER_COMPANY_value + "";
                            if (dbManager.Read(strSql).Count > 0)
                            {
                                string str_USER_COMPANY = dbManager.Read(strSql)["COMPANY_NAME"].ToString();
                                ((Label)objLab_USER_COMPANY).Text = WebString.HtmlEncodeExtend(str_USER_COMPANY);
                            }
                        }
                    
                    }

                    object objLab_USER_DEPT = e.Row.FindControl("lab_USER_DEPT_Show");
                    if (objLab_USER_DEPT is Label)
                    {
                        string str_USER_DEPT_value = ((Label)objLab_USER_DEPT).Text;
                    

                    
                        if (str_USER_DEPT_value.Length > 0)
                        {
                            string strSql = "select [DEPARTMENT_NAME] from [DEPARTMENT] where [DEPARTMENT_ID]=" + str_USER_DEPT_value + "";
                            if (dbManager.Read(strSql).Count > 0)
                            {
                                string str_USER_DEPT = dbManager.Read(strSql)["DEPARTMENT_NAME"].ToString();
                                ((Label)objLab_USER_DEPT).Text = WebString.HtmlEncodeExtend(str_USER_DEPT);
                            }
                        }
                    
                    }

                }
            }
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        FillGrid();
    }





    

    
    //单条删除记录
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        
        using (FypSystem.DBManager.Access dBManager = new Access())
        
        
        {
            int i = e.RowIndex;
            string strSql;
            string strWhere = " where [USER_ID]=" + this.GridView1.DataKeys[i].Values["USER_ID"].ToString() + " and [USER_NAME]='" + this.GridView1.DataKeys[i].Values["USER_NAME"].ToString() + "'";
            //如有上载到磁盘的文件，需先删除
            
            strSql = "select [USER_PHOTO] from [USER] " + strWhere;
            DataTable tabFileName = dBManager.GetDataTable(strSql);
            foreach (DataRow row in tabFileName.Rows)
            {
                string strFileName;
                string strFilePath;
                FypSystem.Common.FileSys fileSys = new FileSys();
                
                
                strFileName = row["USER_PHOTO"].ToString().Trim();
                if (strFileName.Length > 0)
                {
                    strFilePath = fileSys.GetAbsPath(strFileName);
                    System.IO.File.Delete(strFilePath);
                    //this.Response.Write(strFilePath);
                }
                
                
            }
            
            //然后再删除记录
            strSql = "delete from [USER] " + strWhere;
            dBManager.ExecNonQuery(strSql);
        }

        this.GridView1.EditIndex = -1;
        FillGrid();
    }
    



}