示例#1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            Administrator administrator;

            if (Session["Administrator"] != null) //Check if admin is logged in
            {
                administrator = (Administrator)Session["Administrator"];
            }
            else
            {
                Response.Redirect("~/Login.aspx");
            }

            /* Obtain the passed user for editing */
            if (Session["childEdit"] != null)
            {
                childEdit = (Child)Session["childEdit"];
            }
            else if (Session["parentEdit"] != null)
            {
                parentEdit = (Parent)Session["parentEdit"];
            }
            else if (Session["administratorEdit"] != null)
            {
                administratorEdit = (Administrator)Session["administratorEdit"];
            }
            else //If there isn't a user to edit redirect back
            {
                Response.Redirect("~/Default.aspx");
            }

            if (!IsPostBack)
            {
                this.Title = "Edit a user";

                List <Parent> parents          = parentDAO.getAllParents();
                DataTable     parentsFullNames = new DataTable();
                parentsFullNames.Columns.Add("ID");
                parentsFullNames.Columns.Add("FullName");
                foreach (Parent parent in parents)                                                 //Loop through all parents
                {
                    parentsFullNames.Rows.Add(parent.ID, parent.Surname + " " + parent.FirstName); //Create a custom datatable to display surname with firstname for parents
                }
                parentsDropdownList.DataSource     = parentsFullNames;                             //Assign the datatable as the datasource for the gridview
                parentsDropdownList.DataValueField = "ID";                                         //Use ID as the value field to later on identify selected parent
                parentsDropdownList.DataTextField  = "FullName";                                   //Use fullname as textfield to display in dropdown
                parentsDropdownList.DataBind();
                parentsDropdownList.Items.Insert(0, new ListItem("Please Select", "0"));

                setInputFields(); //Set the user data from passed user in the input fields
            }

            setValidators(userTypeDD.SelectedValue); //Update enabled/disabled validators when user type is changed
        }
        private void bindGrid(string user) //Get data for specified user and bind to gridview
        {
            if (user == "Child")
            {
                children = childDAO.getAllChildren();
                userGridView.DataSource = children;
            }
            if (user == "Parent")
            {
                parents = parentDAO.getAllParents();
                userGridView.DataSource = parents;
            }
            if (user == "Administrator")
            {
                administrators          = administratorDAO.getAllAdministrators();
                userGridView.DataSource = administrators;
            }

            userGridView.DataBind();
        }
        private void setParentsDropdownList()
        {
            parents = parentDAO.getAllParents();
            DataTable parentsFullNames = new DataTable();

            parentsFullNames.Columns.Add("ID");
            parentsFullNames.Columns.Add("FullName");
            foreach (Parent parent in parents)                                                 //Loop through all parents
            {
                parentsFullNames.Rows.Add(parent.ID, parent.Surname + " " + parent.FirstName); //Create a custom datatable to display surname with firstname for parents
            }
            parentsDropdownList.DataSource     = parentsFullNames;                             //Assign the datatable as the datasource for the gridview
            parentsDropdownList.DataValueField = "ID";                                         //Use ID as the value field to later on identify selected parent
            parentsDropdownList.DataTextField  = "FullName";                                   //Use fullname as textfield to display in dropdown
            parentsDropdownList.DataBind();
            parentsDropdownList.Items.Insert(0, new ListItem("Please Select", "0"));           //Add 'Please Select' to dropdown field as the first selection
        }