/*
'' ============================================================================================
'' 项目名称：{fyp_title}
'' 项目版本：{fyp_version}
'' 项目描述：{fyp_description}
'' 文件名称：{file_list_name}
'' 文件描述：信息列表文件{value_match_check value1="{table_description}" value2="" extend_match="unequal"} （{table_description}）{/value_match_check}
'' 公司名称：{fyp_company}
'' 开发人员：{fyp_designer}
'' 创建日期：{fyp_created_date}
'' 修订日期：{system_date_time format="yyyy-MM-dd hh:mm:ss"}
'' 版权信息：{fyp_copyright}

'' 项目升级时需读取以下生成工具的版权信息
'' 生成工具：{fysoft_name}
'' 当前版本：{fysoft_version}
'' 官方网站：{fysoft_website}
'' ============================================================================================
*/

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 {file_list_name type="2"} : 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)
        {
            FillGrid();
        }
    }

    /// <summary>
    /// 填充数据网格
    /// </summary>
    private void FillGrid()
    {
        {db_type_check value="sqlserver|sqlserver2005|sqlserver2008" match="false"}
        using (FypSystem.DBManager.Access dBManager = new Access())
        {/db_type_check}
        {db_type_check value="sqlserver|sqlserver2005|sqlserver2008" match="true"}
        using (FypSystem.DBManager.SqlServer dBManager = new SqlServer())
        {/db_type_check}
        {
            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 {fields_loop type="3" separator=", "}[{field_name}] desc{/fields_loop}";

            string strSql = "select {fields_loop separator=", " type="5"}[{field_name}]{/fields_loop} from [{table_name}] " + 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;
            }

            {remark note="说明：备用"}
            //格式化输出“字符型”字段
            /*
            foreach (DataRow row in tabSrouce.Rows)
            {
                {fields_loop}{field_usetype_name_check value="文本" match="true"}
                row["{field_name}"] = WebString.HtmlEncodeExtend(row["{field_name}"].ToString());{/field_usetype_name_check}{/fields_loop}
            }
            */
            {/remark}
            this.GridView1.DataSource = tabSrouce;
            this.GridView1.DataBind();
            tabSrouce.Dispose();
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    {remark note="说明：备用"}
    {batch_manage_check value="true" match="true"}
        //添加批量删除按钮
        if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.Footer)
        {
            HyperLink linkNew = new HyperLink();
            linkNew.Text = "新增 ";
            linkNew.NavigateUrl = "{file_add_name}";
            linkNew.Target = "_blank";
            //e.Row.Cells[e.Row.Cells.Count - 1].Controls.Add(linkNew);
            //this.GridView1.HeaderRow.Cells[this.GridView1.HeaderRow.Cells.Count - 1].Text = "";

            LinkButton lnkBtnDel = new LinkButton();
            lnkBtnDel.Text = " 删除";
            lnkBtnDel.Click += new EventHandler(lnkBtn_Del_Click);
            //e.Row.Cells[e.Row.Cells.Count - 1].Controls.Add(lnkBtnDel);
            //this.GridView1.HeaderRow.Cells[this.GridView1.HeaderRow.Cells.Count - 1].Text = "";
        }
    {/batch_manage_check}
    {/remark}

        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, '{batch_manage_check value="true" match="true"}CheckBox_RecMain{/batch_manage_check}', '{batch_manage_check value="true" match="true"}CheckBox_RecID{/batch_manage_check}', 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, '{batch_manage_check value="true" match="true"}CheckBox_RecMain{/batch_manage_check}', '{batch_manage_check value="true" match="true"}CheckBox_RecID{/batch_manage_check}', true, 'tableContentAlternating001', 'tableContentAlternatingOn001', 'tableContentClick001', 'tableContentClickOn001')");
            }

            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
{create_manage_check value="true" match="true"}
                //设置删除提示
                if (e.Row.FindControl("LinkButton_Del") != null)
                    ((LinkButton)e.Row.FindControl("LinkButton_Del")).Attributes.Add("onclick", "javascript:return confirm('是否确认删除本条记录?')");
{/create_manage_check}
                //格式化输出字段
        {db_type_check value="sqlserver|sqlserver2005|sqlserver2008" match="false"}
                using (FypSystem.DBManager.Access dbManager = new FypSystem.DBManager.Access())
        {/db_type_check}
        {db_type_check value="sqlserver|sqlserver2005|sqlserver2008" match="true"}
                using (FypSystem.DBManager.SqlServer dbManager = new FypSystem.DBManager.SqlServer())
        {/db_type_check}
                {
{fields_loop}
                    object objLab_{field_source_name} = e.Row.FindControl("lab_{control_name}_Show");
                    if (objLab_{field_source_name} is Label)
                    {
                        string str_{field_source_name}_value = ((Label)objLab_{field_source_name}).Text;
                    {field_seltable_name_check value="不链接表" match="true"}
                        {control_style_check value="日期选择" match="true"}                        
                        str_{field_source_name}_value = RegExp.RegMatch(@"([^\s]+)", str_{field_source_name}_value, "$1", true);{/control_style_check}

                        {control_style_check value="时分框" match="true"}                        
                        str_{field_source_name}_value = RegExp.RegMatch(@"(\d+:\d+)(:\d+)", str_{field_source_name}_value, "$1", true);{/control_style_check}

                        {control_style_check value="时分秒框" match="true"}
                        str_{field_source_name}_value = RegExp.RegMatch(@"(\d+:\d+:\d+)", str_{field_source_name}_value, "$1", true);{/control_style_check}

                        {field_display_style_check value="前20字符" match="true"}
                        str_{field_source_name}_value = StringExtend.Left(str_{field_source_name}_value, 20);{/field_display_style_check}

                        {field_display_style_check value="链接文件" match="true"}
                        str_{field_source_name}_value = "<a href='" + str_{field_source_name}_value + "'>打开文件 " + str_{field_source_name}_value + "</a>";{/field_display_style_check}

                        {field_display_style_check value="文本框" match="true"}
                        str_{field_source_name}_value = "<input value='" + str_{field_source_name}_value + "'>";{/field_display_style_check}

                        {field_display_style_check value="文本域" match="true"}
                        str_{field_source_name}_value = "<textarea>" + str_{field_source_name}_value + "</textarea>";{/field_display_style_check}

                        {field_display_style_check value="显示图片" match="true"}
                        if (str_{field_source_name}_value.Length > 0)
                            str_{field_source_name}_value = "<img src='" + str_{field_source_name}_value + "'>";{/field_display_style_check}

                        {field_display_style_check value="显示文字|前20字符" match="true"}
                        ((Label)objLab_{field_source_name}).Text = WebString.HtmlEncodeExtend(str_{field_source_name}_value);
                        {/field_display_style_check}

                        {field_display_style_check value="显示文字|前20字符" match="false"}
                        ((Label)objLab_{field_source_name}).Text = str_{field_source_name}_value;
                        {/field_display_style_check}
                    {/field_seltable_name_check}

                    {field_seltable_name_check value="不链接表" match="false"}
                        if (str_{field_source_name}_value.Length > 0)
                        {
                            string strSql = "select [{field_seltable_text}] from [{field_seltable_name}] where [{field_seltable_value}]={get_table_field_settings tag_name="field_single_quote" table="{field_seltable_name}" field="{field_seltable_value}"}" + str_{field_source_name}_value + "{get_table_field_settings tag_name="field_single_quote" table="{field_seltable_name}" field="{field_seltable_value}"}";
                            if (dbManager.Read(strSql).Count > 0)
                            {
                                string str_{field_source_name} = dbManager.Read(strSql)["{field_seltable_text}"].ToString();
                                ((Label)objLab_{field_source_name}).Text = WebString.HtmlEncodeExtend(str_{field_source_name});
                            }
                        }
                    {/field_seltable_name_check}
                    }
{/fields_loop}
                }
            }
        }
    }

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

{create_order_by_check value="true" match="true"}
    //排序
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string strSortExpression= e.SortExpression;
        if (StringExtend.NullToEmptyStr(ViewState["SortExpression"]) == strSortExpression)
        {
            //点击相同标题时，转换排序顺序
            if (StringExtend.NullToEmptyStr(ViewState["SortDirection"]) == "desc")
                ViewState["SortDirection"] = "asc";
            else
                ViewState["SortDirection"] = "desc";
        }
        else
        {
            //点击不同标题时，默认倒序排序
            ViewState["SortExpression"] = strSortExpression;
            ViewState["SortDirection"] = "desc";
        }

        //添加排序标识符号
        foreach (DataControlField field in this.GridView1.Columns)
        {
            //if (field is BoundField)
            //((BoundField)field).HtmlEncode = false;

            //field.HeaderText = field.HeaderText.Replace("▲", "").Replace("▼", "");
            field.HeaderText = field.HeaderText.Replace("<font face='webdings'>5</font>", "").Replace("<font face='webdings'>6</font>", "");
            //((BoundField)(this.GridView1.Columns[0])).DataField

            if (field.SortExpression.ToLower() == strSortExpression.ToLower())
            {
                if (ViewState["SortDirection"].ToString() == "desc")
                    field.HeaderText = field.HeaderText.Trim() + " <font face='webdings'>6</font>";
                else
                    field.HeaderText = field.HeaderText.Trim() + " <font face='webdings'>5</font>";
            }
        }
        FillGrid();
    }
{/create_order_by_check}

{create_manage_check value="true" match="true"}
{field_edit_in_list_all_check value="true" match="true" type="1"}
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        //当前编辑行背景色高亮
        this.GridView1.EditRowStyle.CssClass = "tableTitle1";
        //this.GridView1.EditRowStyle.BackColor = System.Drawing.Color.FromName("#F7CE90");
        FillGrid();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        FillGrid();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        {db_type_check value="sqlserver|sqlserver2005|sqlserver2008" match="false"}
        using (FypSystem.DBManager.Access dBManager = new Access())
        {/db_type_check}
        {db_type_check value="sqlserver|sqlserver2005|sqlserver2008" match="true"}
        using (FypSystem.DBManager.SqlServer dBManager = new SqlServer())
        {/db_type_check}
        {
            int i = e.RowIndex;
            string strWhere = " where {fields_loop type="3" separator=" and "}[{field_name}]={field_single_quote}" + this.GridView1.DataKeys[i].Values["{field_name}"].ToString() + "{field_single_quote}{/fields_loop}";

{fields_loop}{field_edit_in_list_check value="true" match="true"}
            string str{field_source_name} = ((TextBox)(GridView1.Rows[i].FindControl("{control_name}"))).Text.ToString();{/field_edit_in_list_check}{/fields_loop}

            string strSql = "update [{table_name}] set {fields_loop separator=", "}{field_edit_readonly_check value="false" match="true"}{field_edit_in_list_check value="true" match="true"}{field_bracket_name}={field_single_quote prefix=""}" + str{field_source_name} + "{field_single_quote}{/field_edit_in_list_check}{/field_edit_readonly_check}{/fields_loop}" + strWhere;
            dBManager.ExecNonQuery(strSql);
        }

        this.GridView1.EditIndex = -1;
        FillGrid();
    }
{/field_edit_in_list_all_check}
    {batch_manage_check value="true" match="true"}
    //批量删除记录
    protected void lnkBtn_Del_Click(object sender, EventArgs e)
    {
        {db_type_check value="sqlserver|sqlserver2005|sqlserver2008" match="false"}
        using (FypSystem.DBManager.Access dBManager = new Access())
        {/db_type_check}
        {db_type_check value="sqlserver|sqlserver2005|sqlserver2008" match="true"}
        using (FypSystem.DBManager.SqlServer dBManager = new SqlServer())
        {/db_type_check}
        {
            bool bFindChecked = false;
            {fields_loop type="3"}
            string strAllID_{field_source_name} = "";
            {/fields_loop}
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                CheckBox cbx = (CheckBox)GridView1.Rows[i].FindControl("CheckBox_RecID");
                if (cbx.Checked)
                {
                    {fields_loop type="3"}
                    strAllID_{field_source_name} += "{field_single_quote}" + this.GridView1.DataKeys[i].Values["{field_name}"].ToString() + "{field_single_quote},";
                    {/fields_loop}
                    bFindChecked = true;
                }
            }

            if (bFindChecked == true)
            {
                {fields_loop type="3"}
                strAllID_{field_source_name} = FypSystem.Common.StringExtend.TrimRight(strAllID_{field_source_name}, ",");
                {/fields_loop}
                string strSql;
                string strWhere = " where {fields_loop type="3" separator=" and "}[{field_name}] in (" + strAllID_{field_source_name} + ") {/fields_loop}";
                //如有上载到磁盘的文件，需先删除
                {control_style_all_check value="上载文件|上载图片" match="true" type="3"}
                strSql = "select {fields_loop type="-1" separator=", "}{control_style_check value="上载文件|上载图片" match="true"}[{field_name}]{/control_style_check}{/fields_loop} from [{table_name}] " + strWhere;
                DataTable tabFileName = dBManager.GetDataTable(strSql);
                foreach (DataRow row in tabFileName.Rows)
                {
                    string strFileName;
                    string strFilePath;
                    FypSystem.Common.FileSys fileSys = new FileSys();
                    {fields_loop type="-1"}
                    {control_style_check value="上载文件|上载图片" match="true"}
                    strFileName = row["{field_name}"].ToString().Trim();
                    if (strFileName.Length > 0)
                    {
                        strFilePath = fileSys.GetAbsPath(strFileName);
                        System.IO.File.Delete(strFilePath);
                        //this.Response.Write(strFilePath);
                    }
                    {/control_style_check}
                    {/fields_loop}
                }
                {/control_style_all_check}
                //然后再删除记录
                strSql = "delete from [{table_name}] " + strWhere;
                //this.Response.Write(strSql);
                dBManager.ExecNonQuery(strSql);
            }
        }

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

    {batch_manage_check value="false" match="true"}
    //单条删除记录
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        {db_type_check value="sqlserver|sqlserver2005|sqlserver2008" match="false"}
        using (FypSystem.DBManager.Access dBManager = new Access())
        {/db_type_check}
        {db_type_check value="sqlserver|sqlserver2005|sqlserver2008" match="true"}
        using (FypSystem.DBManager.SqlServer dBManager = new SqlServer())
        {/db_type_check}
        {
            int i = e.RowIndex;
            string strSql;
            string strWhere = " where {fields_loop type="3" separator=" and "}[{field_name}]={field_single_quote}" + this.GridView1.DataKeys[i].Values["{field_name}"].ToString() + "{field_single_quote}{/fields_loop}";
            //如有上载到磁盘的文件，需先删除
            {control_style_all_check value="上载文件|上载图片" match="true" type="3"}
            strSql = "select {fields_loop type="-1" separator=", "}{control_style_check value="上载文件|上载图片" match="true"}[{field_name}]{/control_style_check}{/fields_loop} from [{table_name}] " + strWhere;
            DataTable tabFileName = dBManager.GetDataTable(strSql);
            foreach (DataRow row in tabFileName.Rows)
            {
                string strFileName;
                string strFilePath;
                FypSystem.Common.FileSys fileSys = new FileSys();
                {fields_loop type="-1"}
                {control_style_check value="上载文件|上载图片" match="true"}
                strFileName = row["{field_name}"].ToString().Trim();
                if (strFileName.Length > 0)
                {
                    strFilePath = fileSys.GetAbsPath(strFileName);
                    System.IO.File.Delete(strFilePath);
                    //this.Response.Write(strFilePath);
                }
                {/control_style_check}
                {/fields_loop}
            }
            {/control_style_all_check}
            //然后再删除记录
            strSql = "delete from [{table_name}] " + strWhere;
            dBManager.ExecNonQuery(strSql);
        }

        this.GridView1.EditIndex = -1;
        FillGrid();
    }
    {/batch_manage_check}
{/create_manage_check}

{remark note="说明：备用"}
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string strID = e.CommandArgument.ToString();
        string[] arrID = strID.Split(new string[] { "|||" }, StringSplitOptions.None);

        if (e.CommandName == "Modify")
        {
            this.Response.Redirect("{file_edit_name}?{fields_loop type="3" separator="&"}{control_name}=" + arrID[{i}] + "{/fields_loop}");
        }
        else if (e.CommandName == "View")
        {
            this.Response.Redirect("{file_detail_name}?{fields_loop type="3" separator="&"}{control_name}=" + arrID[{i}] + "{/fields_loop}");
        }
    }
{/remark}
}