protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            #region 获取页面参数
            ViewState["Report"]    = Request.QueryString["Report"] != null ? new Guid(Request.QueryString["Report"]) : Guid.Empty;
            ViewState["LoadCache"] = Request.QueryString["LoadCache"] != null ? Request.QueryString["LoadCache"] != "false" : true;
            #endregion

            if ((Guid)ViewState["Report"] == Guid.Empty)
            {
                Response.Redirect("Rpt_ReportList.aspx");
            }

            #region 判断有无浏览权限
            Rpt_Report rpt = new Rpt_ReportBLL((Guid)ViewState["Report"]).Model;
            if (rpt == null)
            {
                Response.Redirect("Rpt_ReportList.aspx");
            }

            IList <Rpt_FolderRight> rights = Rpt_FolderRightBLL.GetAssignedRightByUser(Session["UserName"].ToString()).Where(p => p.Folder == rpt.Folder).ToList();
            if (rights.Count == 0)
            {
                MessageBox.ShowAndRedirect(this, "对不起,您没有权限浏览该报表", "Rpt_ReportList.aspx");
                return;
            }
            #endregion

            //增加浏览记录
            Rpt_ReportBLL.AddViewTimes(new Guid(ViewState["Report"].ToString()), (int)Session["UserID"]);
        }
    }
    private void BindGrid()
    {
        if (tr_List.SelectedNode != null)
        {
            bt_Add.Visible             = false;
            gv_List.Columns[1].Visible = false;

            int folder = int.Parse(tr_List.SelectedNode.Value);

            IList <Rpt_FolderRight> rights = Rpt_FolderRightBLL.GetAssignedRightByUser(Session["UserName"].ToString()).Where(p => p.Folder == folder).ToList();

            string ConditionStr = " Rpt_Report.Folder =  " + folder.ToString();
            if (rights.Where(p => p.Action == 1 || p.Action == 2).Count() == 0 && rights.Where(p => p.Action == 3).Count() > 0)
            {
                //只有创建、查看自己报表的权限
                ConditionStr += " AND Rpt_Report.InsertStaff = " + Session["UserID"].ToString();

                bt_Add.Visible             = true;                                         //新增报表
                gv_List.Columns[1].Visible = true;                                         //设计报表
            }
            else if (rights.Where(p => p.Action == 2).Count() > 0)
            {
                bt_Add.Visible             = true;                                         //新增报表
                gv_List.Columns[1].Visible = true;                                         //设计报表
            }

            gv_List.ConditionString = ConditionStr;
            gv_List.BindGrid();
        }
    }
    private void BindTree(TreeNodeCollection TNC, int SuperID)
    {
        IList <Rpt_FolderRight> rights = Rpt_FolderRightBLL.GetAssignedRightByUser(Session["UserName"].ToString());

        foreach (Rpt_Folder folder in Rpt_FolderBLL.GetModelList("SuperID=" + SuperID.ToString()))
        {
            if (folder.ID > 1 && rights.FirstOrDefault(p => p.Folder == folder.ID) == null)
            {
                continue;
            }
            TreeNode tn = new TreeNode();
            tn.Text     = folder.Name;
            tn.Value    = folder.ID.ToString();
            tn.ImageUrl = "~/Images/gif/gif-0030.gif";
            TNC.Add(tn);
            //if (folder.ID == 1)
            BindTree(tn.ChildNodes, folder.ID);
        }
    }
    private void BindRight()
    {
        cbx_RoleList1.DataSource = Roles.GetAllRoles();
        cbx_RoleList1.DataBind();

        cbx_RoleList2.DataSource = Roles.GetAllRoles();
        cbx_RoleList2.DataBind();

        cbx_RoleList3.DataSource = Roles.GetAllRoles();
        cbx_RoleList3.DataBind();

        IList <Rpt_FolderRight> rights = Rpt_FolderRightBLL.GetModelList("Folder = " + ViewState["ID"].ToString());

        foreach (Rpt_FolderRight r in rights)
        {
            if (r.Based_On == 2)
            {
                ListItem item = null;
                switch (r.Action)
                {
                case 1:
                    item = cbx_RoleList1.Items.FindByText(r.RoleName);
                    break;

                case 2:
                    item = cbx_RoleList2.Items.FindByText(r.RoleName);
                    break;

                case 3:
                    item = cbx_RoleList3.Items.FindByText(r.RoleName);
                    break;

                default:
                    break;
                }
                if (item != null)
                {
                    item.Selected = true;
                }
            }
        }
    }
    private void SaveRight()
    {
        foreach (ListItem item in cbx_RoleList2.Items)
        {
            IList <Rpt_FolderRight> rights = Rpt_FolderRightBLL.GetModelList("Folder = " + ViewState["ID"].ToString() +
                                                                             " AND Action = 2 AND Based_On = 2 AND RoleName='" + item.Text + "'");
            if (item.Selected)
            {
                if (rights.Count == 0)
                {
                    Rpt_FolderRightBLL bll = new Rpt_FolderRightBLL();
                    bll.Model.Folder      = (int)ViewState["ID"];
                    bll.Model.Action      = 2;
                    bll.Model.Based_On    = 2;
                    bll.Model.RoleName    = item.Text;
                    bll.Model.InsertStaff = (int)Session["UserID"];
                    bll.Add();
                }
            }
            else
            {
                if (rights.Count > 0)
                {
                    new Rpt_FolderRightBLL(rights[0].ID).Delete();
                }
            }
        }

        foreach (ListItem item in cbx_RoleList1.Items)
        {
            IList <Rpt_FolderRight> rights = Rpt_FolderRightBLL.GetModelList("Folder = " + ViewState["ID"].ToString() +
                                                                             " AND Action = 1 AND Based_On = 2 AND RoleName='" + item.Text + "'");
            if (item.Selected)
            {
                if (rights.Count == 0 && !cbx_RoleList2.Items.FindByText(item.Text).Selected)
                {
                    Rpt_FolderRightBLL bll = new Rpt_FolderRightBLL();
                    bll.Model.Folder      = (int)ViewState["ID"];
                    bll.Model.Action      = 1;
                    bll.Model.Based_On    = 2;
                    bll.Model.RoleName    = item.Text;
                    bll.Model.InsertStaff = (int)Session["UserID"];
                    bll.Add();
                }
            }
            else
            {
                if (rights.Count > 0)
                {
                    new Rpt_FolderRightBLL(rights[0].ID).Delete();
                }
            }
        }


        foreach (ListItem item in cbx_RoleList3.Items)
        {
            IList <Rpt_FolderRight> rights = Rpt_FolderRightBLL.GetModelList("Folder = " + ViewState["ID"].ToString() +
                                                                             " AND Action = 3 AND Based_On = 2 AND RoleName='" + item.Text + "'");
            if (item.Selected)
            {
                if (rights.Count == 0 && !cbx_RoleList1.Items.FindByText(item.Text).Selected&& !cbx_RoleList2.Items.FindByText(item.Text).Selected)
                {
                    Rpt_FolderRightBLL bll = new Rpt_FolderRightBLL();
                    bll.Model.Folder      = (int)ViewState["ID"];
                    bll.Model.Action      = 3;
                    bll.Model.Based_On    = 2;
                    bll.Model.RoleName    = item.Text;
                    bll.Model.InsertStaff = (int)Session["UserID"];
                    bll.Add();
                }
            }
            else
            {
                if (rights.Count > 0)
                {
                    new Rpt_FolderRightBLL(rights[0].ID).Delete();
                }
            }
        }
    }
    private void SaveRight()
    {
        foreach (ListItem item in cbx_RoleList2.Items)
        {
            IList<Rpt_FolderRight> rights = Rpt_FolderRightBLL.GetModelList("Folder = " + ViewState["ID"].ToString() +
                " AND Action = 2 AND Based_On = 2 AND RoleName='" + item.Text + "'");
            if (item.Selected)
            {
                if (rights.Count == 0)
                {
                    Rpt_FolderRightBLL bll = new Rpt_FolderRightBLL();
                    bll.Model.Folder = (int)ViewState["ID"];
                    bll.Model.Action = 2;
                    bll.Model.Based_On = 2;
                    bll.Model.RoleName = item.Text;
                    bll.Model.InsertStaff = (int)Session["UserID"];
                    bll.Add();
                }
            }
            else
            {
                if (rights.Count > 0) new Rpt_FolderRightBLL(rights[0].ID).Delete();
            }
        }

        foreach (ListItem item in cbx_RoleList1.Items)
        {
            IList<Rpt_FolderRight> rights = Rpt_FolderRightBLL.GetModelList("Folder = " + ViewState["ID"].ToString() +
                " AND Action = 1 AND Based_On = 2 AND RoleName='" + item.Text + "'");
            if (item.Selected)
            {
                if (rights.Count == 0 && !cbx_RoleList2.Items.FindByText(item.Text).Selected)
                {
                    Rpt_FolderRightBLL bll = new Rpt_FolderRightBLL();
                    bll.Model.Folder = (int)ViewState["ID"];
                    bll.Model.Action = 1;
                    bll.Model.Based_On = 2;
                    bll.Model.RoleName = item.Text;
                    bll.Model.InsertStaff = (int)Session["UserID"];
                    bll.Add();
                }
            }
            else
            {
                if (rights.Count > 0) new Rpt_FolderRightBLL(rights[0].ID).Delete();
            }
        }

        foreach (ListItem item in cbx_RoleList3.Items)
        {
            IList<Rpt_FolderRight> rights = Rpt_FolderRightBLL.GetModelList("Folder = " + ViewState["ID"].ToString() +
                " AND Action = 3 AND Based_On = 2 AND RoleName='" + item.Text + "'");
            if (item.Selected)
            {
                if (rights.Count == 0 && !cbx_RoleList1.Items.FindByText(item.Text).Selected && !cbx_RoleList2.Items.FindByText(item.Text).Selected)
                {
                    Rpt_FolderRightBLL bll = new Rpt_FolderRightBLL();
                    bll.Model.Folder = (int)ViewState["ID"];
                    bll.Model.Action = 3;
                    bll.Model.Based_On = 2;
                    bll.Model.RoleName = item.Text;
                    bll.Model.InsertStaff = (int)Session["UserID"];
                    bll.Add();
                }
            }
            else
            {
                if (rights.Count > 0) new Rpt_FolderRightBLL(rights[0].ID).Delete();
            }
        }
    }