Exemplo n.º 1
0
        // Loads data from the database and binds the UI controls.
        private void BindGrid(int editIndex)
        {
            MainSecurity objSercu = new MainSecurity();
            Role         objrole  = objSercu.GetRole(Page.User.Identity.Name);

            using (MainDB db = new MainDB()) {
                int    totalRows  = 0;
                int    startIndex = gridUser.CurrentPageIndex * gridUser.PageSize;
                string sort       = "";
                if (this.Sort.Length > 0)
                {
                    sort = this.Sort + " " + this.SortDirection;
                }
                string where = "";

                string strSearchName = txtSearch.Text.Trim().ToLower();

                if (ddlChannel.SelectedIndex >= 0)
                {
                    //lấy danh sách user của 1 channel
                    Channel_UserRow[] row    = db.Channel_UserCollection.GetByChannel_ID(Convert.ToInt32(ddlChannel.SelectedValue));
                    string            inuser = "";
                    if (row != null && row.Length > 0)
                    {
                        //duyệt qua danh sách user của 1 channel
                        foreach (Channel_UserRow userRow in row)
                        {
                            // Xu ly viec tim kiem theo UserName
                            // -- Neu Username nay trung voi keyword thi moi dc xu ly tiep
                            // -- Con ko co keyword thi hien thi all
                            if (strSearchName != "" && userRow.User_ID.ToLower().IndexOf(strSearchName) == -1)
                            {
                                continue;
                            }


                            // Neu la account Quan Tri kenh thi ko nhin thay account Channelvn va Admin
                            if (objrole.isQuanTriKenh && objrole.isAdministrator == false)
                            {
                                if (userRow.User_ID == "channelvn" || userRow.User_ID == "admin")
                                {
                                    continue;
                                }
                            }

                            //lọc theo role
                            if (ddlRole.SelectedIndex > 0)
                            {
                                Channel_User_RoleRow[] curr = db.Channel_User_RoleCollection.GetByCU_ID(userRow.CU_ID);
                                if (curr != null && curr.Length > 0)
                                {
                                    //duyệt qua các role 1 user có
                                    foreach (Channel_User_RoleRow roleRow in curr)
                                    {
                                        //nếu có role nào trùng với role cần lọc
                                        if (roleRow.Role_ID == Int32.Parse(ddlRole.SelectedValue))
                                        {
                                            //đưa vào danh sách cần select
                                            inuser += "'" + userRow.User_ID + "',";
                                            break;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                //đưa vào danh sách cần select
                                inuser += "'" + userRow.User_ID + "',";
                            }
                        }
                        inuser += "'" + Const.Global_Admin + "','" + Const.TONG_BIEN_TAP + "',";
                        Business.User User    = new Business.User();
                        string        inWhere = User.CheckDupplicate(inuser);
                        where = "User_ID in (" + inWhere.Remove(inWhere.Length - 1) + ")";
                    }
                    else
                    {
                        where = "User_ID in('" + Const.Global_Admin + "','" + Const.TONG_BIEN_TAP + "')";
                    }
                }

                if (objrole.isQuanTriKenh && objrole.isAdministrator == false)
                {
                    where += " AND User_ID NOT IN ('channelvn','admin') ";
                }

                DataTable table = db.UserCollection.GetAsDataTable(where, sort, startIndex, gridUser.PageSize, ref totalRows);

                int viewStateFirst = gridUser.CurrentPageIndex * gridUser.PageSize;
                int viewStateLast;

                if (gridUser.AllowPaging)
                {
                    viewStateLast = Math.Min(viewStateFirst + gridUser.PageSize, table.Rows.Count);
                }
                else
                {
                    viewStateLast = table.Rows.Count;
                }

                gridUser.VirtualItemCount = totalRows;
                gridUser.DataSource       = table;
                ViewState["dtuser"]       = table;
                gridUser.EditItemIndex    = editIndex;
                gridUser.DataBind();

                ShowHideButton();
            }
        }