projectcustomfields.ascx.cs

来自「BugNET is an issue tracking and project 」· CS 代码 · 共 656 行 · 第 1/2 页

CS
656
字号
                    }
                    if (txtValue.Text.Trim() == String.Empty)
                    {
                        lblError.Text = "Selection value can not be blank";
                        return;
                    }
                    int customFieldSelectionId = (int)((DataGrid)source).DataKeys[e.Item.ItemIndex];
                    
                    CustomFieldSelection cfs = CustomFieldSelection.GetCustomFieldSelectionById(customFieldSelectionId);
                    cfs.Name = txtName.Text;
                    cfs.Value = txtValue.Text;
                    cfs.Save();

                    lblError.Text = String.Empty;

                    foreach (DataGridItem item in grdCustomFields.Items)
                    {
                        DataGrid grdSelectionValues = (DataGrid)item.FindControl("grdSelectionValues");
                        if (null != grdSelectionValues)
                        {
                            grdSelectionValues.ShowFooter = true;
                            grdSelectionValues.EditItemIndex = -1;
                            BindCustomFieldSelections();
                        }
                    }
                }
                ViewState["EditingSubGrid"] = null;
                BindCustomFields();
            }
        }

        /// <summary>
        /// Binds the custom field selections.
        /// </summary>
        private void BindCustomFieldSelections()
        {
            foreach (DataGridItem item in grdCustomFields.Items)
            {
                DataGrid grdSelectionValues = (DataGrid)item.FindControl("grdSelectionValues");
                if (null != grdSelectionValues)
                {
                    grdSelectionValues.DataSource = GetCustomFieldSelections(Convert.ToInt32(item.Cells[0].Text.Trim()));
                    grdSelectionValues.DataKeyField = "Id";
                    grdSelectionValues.DataBind();
                }
            }
        }

        /// <summary>
        /// Handles the Edit event of the grdCustomFields control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.DataGridCommandEventArgs"/> instance containing the event data.</param>
        protected void grdCustomFields_Edit(object sender, DataGridCommandEventArgs e)
        {           
            grdCustomFields.EditItemIndex = e.Item.ItemIndex;
            grdCustomFields.DataBind();
        }

        /// <summary>
        /// Handles the Cancel event of the grdStatus control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.DataGridCommandEventArgs"/> instance containing the event data.</param>
        protected void grdCustomFields_Cancel(object sender, DataGridCommandEventArgs e)
        {
            grdCustomFields.EditItemIndex = -1;
            grdCustomFields.DataBind();
        }

        /// <summary>
        /// Gets the custom field selections.
        /// </summary>
        /// <param name="customFieldId">The custom field id.</param>
        /// <returns></returns>
        protected List<CustomFieldSelection> GetCustomFieldSelections(int customFieldId)
        {
            return CustomFieldSelection.GetCustomFieldsSelectionsByCustomFieldId(customFieldId);
        }

        /// <summary>
        /// Handles the Click event of the lnkAddCustomField control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected void lnkAddCustomField_Click(object sender, EventArgs e)
        {
            string NewName = txtName.Text.Trim();

            if (NewName == String.Empty)
                return;

            ValidationDataType DataType = (ValidationDataType)Enum.Parse(typeof(ValidationDataType), dropDataType.SelectedValue);
            CustomField.CustomFieldType FieldType = (CustomField.CustomFieldType)Enum.Parse(typeof(CustomField.CustomFieldType), rblCustomFieldType.SelectedValue);
            bool required = chkRequired.Checked;

            CustomField newCustomField = new CustomField(ProjectId, NewName, DataType, required, FieldType);
            if (newCustomField.Save())
            {
                txtName.Text = "";
                dropDataType.SelectedIndex = 0;
                chkRequired.Checked = false;
                BindCustomFields();
            }
            else
            {
                lblError.Text = "Could not save custom field";
            }
        }

        /// <summary>
        /// Deletes the custom field.
        /// </summary>
        /// <param name="s">The s.</param>
        /// <param name="e">The <see cref="T:System.Web.UI.WebControls.DataGridCommandEventArgs"/> instance containing the event data.</param>
        void DeleteCustomField(Object s, DataGridCommandEventArgs e)
        {
            int customFieldId = (int)grdCustomFields.DataKeys[e.Item.ItemIndex];

            if (!CustomField.DeleteCustomField(customFieldId))
                lblError.Text = "Could not delete custom field";
            else
                BindCustomFields();
        }

        /// <summary>
        /// Handles the Update event of the grdCustomFields control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.DataGridCommandEventArgs"/> instance containing the event data.</param>
        protected void grdCustomFields_Update(object sender, DataGridCommandEventArgs e)
        {
            CustomField cf = CustomField.GetCustomFieldById(Convert.ToInt32(grdCustomFields.DataKeys[e.Item.ItemIndex]));
            TextBox txtCustomFieldName = (TextBox)e.Item.FindControl("txtCustomFieldName");
            DropDownList customFieldType = (DropDownList)e.Item.FindControl("dropCustomFieldType");
            DropDownList dataType = (DropDownList)e.Item.FindControl("dropEditDataType");
            CheckBox required = (CheckBox)e.Item.FindControl("chkEditRequired");

            cf.Name = txtCustomFieldName.Text;

            ValidationDataType DataType = (ValidationDataType)Enum.Parse(typeof(ValidationDataType),  dataType.SelectedValue);
            CustomField.CustomFieldType FieldType = (CustomField.CustomFieldType)Enum.Parse(typeof(CustomField.CustomFieldType), customFieldType.SelectedValue);
            cf.FieldType = FieldType;
            cf.DataType = DataType;
            cf.Required = required.Checked;

            cf.Save();
            grdCustomFields.EditItemIndex = -1;
            BindCustomFields();

        }
        /// <summary>
        /// Handles the ItemDataBound event of the grdCustomFields control.
        /// </summary>
        /// <param name="s">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.Web.UI.WebControls.DataGridItemEventArgs"/> instance containing the event data.</param>
        void grdCustomFields_ItemDataBound(Object s, DataGridItemEventArgs e)
        {
            Control container = e.Item;

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem)
            {
                if (e.Item.DataItem == null)
                {
                    return;
                }

                HtmlImage btnExpandButton = (HtmlImage)container.FindControl("image_");
                if (btnExpandButton != null)
                {
                    HtmlControl c = (HtmlControl)e.Item.FindControl("divSelectionValues");
                    btnExpandButton.Attributes.Add("OnClick", string.Format("Toggle('{0}', '{1}');",c.ClientID,btnExpandButton.ClientID));
                }	

                CustomField currentCustomField = (CustomField)e.Item.DataItem;

                Label lblName = (Label)e.Item.FindControl("lblName");
                lblName.Text = currentCustomField.Name;

                Label lblDataType = (Label)e.Item.FindControl("lblDataType");
                lblDataType.Text = currentCustomField.DataType.ToString();

                Label lblFieldType = (Label)e.Item.FindControl("lblFieldType");
                lblFieldType.Text = currentCustomField.FieldType.ToString();

                Label lblRequired = (Label)e.Item.FindControl("lblRequired");
                lblRequired.Text = currentCustomField.Required.ToString();

                ImageButton btnDelete = (ImageButton)e.Item.FindControl("btnDelete");
                btnDelete.OnClientClick = String.Format("return confirm('Are you sure you want to delete the \"{0}\" field?');", currentCustomField.Name);

                //only drop down list fields have selection values.
                if (currentCustomField.FieldType == CustomField.CustomFieldType.DropDownList)
                {
                    DataGrid grid = (DataGrid)e.Item.Cells[grdCustomFields.Columns.Count - 1].FindControl("grdSelectionValues");
                    if (grid != null)
                    {
                        this.ViewState["CustomFieldId"] = currentCustomField.Id;
                        grid.DataSource = GetCustomFieldSelections(currentCustomField.Id);
                        grid.DataBind();
                    }
                }
                else
                {
                    e.Item.FindControl("image_").Visible = false;
                    e.Item.Cells[e.Item.Cells.Count - 1].Visible = false;  
                }
            }
            if (e.Item.ItemType == ListItemType.EditItem)
            {
                CustomField currentCustomField = (CustomField)e.Item.DataItem;
                TextBox txtCustomFieldName = (TextBox)e.Item.FindControl("txtCustomFieldName");
                DropDownList customFieldType = (DropDownList)e.Item.FindControl("dropCustomFieldType");
                DropDownList dataType = (DropDownList)e.Item.FindControl("dropEditDataType");
                CheckBox required = (CheckBox)e.Item.FindControl("chkEditRequired");

                required.Checked = currentCustomField.Required;
                txtCustomFieldName.Text  = currentCustomField.Name;
                customFieldType.SelectedValue = Convert.ToString((int)currentCustomField.FieldType);
                dataType.SelectedValue = currentCustomField.DataType.ToString();
                dataType.Items.Clear();
                dataType.DataSource = Enum.GetNames(typeof(ValidationDataType));
                dataType.DataBind();
            }
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the DropFieldType control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void DropFieldType_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList dropEditDataType = (DropDownList)grdCustomFields.Items[grdCustomFields.EditItemIndex].FindControl("dropEditDataType");
            DropDownList dropEditCustomFieldType = (DropDownList)grdCustomFields.Items[grdCustomFields.EditItemIndex].FindControl("dropCustomFieldType");
            CheckBox chkEditRequired = (CheckBox)grdCustomFields.Items[grdCustomFields.EditItemIndex].FindControl("chkEditRequired");

            dropEditDataType.Items.Clear();
            dropEditDataType.DataSource = Enum.GetNames(typeof(ValidationDataType));
            dropEditDataType.DataBind();
            dropEditDataType.Enabled = true;

            switch (int.Parse(dropEditCustomFieldType.SelectedValue))
            {
                case 1:
                    dropEditDataType.Items.Remove(dropEditDataType.Items.FindByValue(ValidationDataType.Date.ToString()));
                    break;
                case 2:
                    dropEditDataType.SelectedValue = ValidationDataType.String.ToString();
                    dropEditDataType.Enabled = false;
                    break;
                case 3:
                    dropEditDataType.SelectedValue = ValidationDataType.Date.ToString();
                    dropEditDataType.Enabled = false;
                    break;
                case 4:
                    dropEditDataType.SelectedValue = ValidationDataType.String.ToString();
                    dropEditDataType.Enabled = false;
                    break;
                case 5:
                    dropEditDataType.SelectedValue = ValidationDataType.Integer.ToString();
                    dropEditDataType.Enabled = false;
                    chkEditRequired.Enabled = false;
                    break;
                case 6:
                    dropEditDataType.SelectedValue = ValidationDataType.String.ToString();
                    dropEditDataType.Enabled = false;
                    break;
            }
      
        }

        /// <summary>
        /// Handles the SelectedIndexChange event of the rblCustomFieldType control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected void rblCustomFieldType_SelectedIndexChanged(object sender, EventArgs e)
        {
            dropDataType.Items.Clear();
            dropDataType.DataSource = Enum.GetNames(typeof(ValidationDataType));
            dropDataType.DataBind();
            dropDataType.Enabled = true;

            switch (int.Parse(rblCustomFieldType.SelectedValue))
            {
                case 1:
                    dropDataType.Items.Remove(dropDataType.Items.FindByValue(ValidationDataType.Date.ToString()));
                    break;
                case 2:
                    dropDataType.SelectedValue = ValidationDataType.String.ToString();
                    dropDataType.Enabled = false;
                    break;
                case 3:
                    dropDataType.SelectedValue = ValidationDataType.Date.ToString();
                    dropDataType.Enabled = false;
                    break;
                case 4:
                    dropDataType.SelectedValue = ValidationDataType.String.ToString();
                    dropDataType.Enabled = false;
                    break;
                case 5:
                    dropDataType.SelectedValue = ValidationDataType.Integer.ToString();
                    dropDataType.Enabled = false;
                    chkRequired.Enabled = false;
                    break;
                case 6:
                    dropDataType.SelectedValue = ValidationDataType.String.ToString();
                    dropDataType.Enabled = false;
                    break;
            }
        }

        /// <summary>
        /// Handles the Click event of the cmdCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected void cmdCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Administration/Projects/EditProject.aspx?id=" + ProjectId.ToString());
        }

       

    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?