projectcustomfields.ascx.cs

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

CS
656
字号
namespace BugNET.Administration.Projects.UserControls
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Collections.Generic;
    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 BugNET.BusinessLogicLayer;
    using BugNET.UserInterfaceLayer;

    /// <summary>
    /// 
    /// </summary>
    public partial class ProjectCustomFields : System.Web.UI.UserControl, IEditProjectControl
    {
        protected System.Web.UI.WebControls.DataGrid grdSelectionValues;

        /// <summary>
        /// Handles the Load event of the Page 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 Page_Load(object sender, EventArgs e)
        {

        }

        #region Web Form Designer generated code
        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init"></see> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"></see> object that contains the event data.</param>
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        ///		Required method for Designer support - do not modify
        ///		the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.grdCustomFields.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DeleteCustomField);
            this.grdCustomFields.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.grdCustomFields_ItemDataBound);
            this.grdCustomFields.ItemCommand += new DataGridCommandEventHandler(grdCustomFields_ItemCommand);
        }

        /// <summary>
        /// Handles the ItemCreated 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.DataGridItemEventArgs"/> instance containing the event data.</param>
        protected void grdCustomFields_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            grdSelectionValues = (DataGrid)e.Item.FindControl("grdSelectionValues");
            if (null != grdSelectionValues)
            {
                grdSelectionValues.ItemDataBound += new DataGridItemEventHandler(this.grdSelectionValues_ItemDataBound);
                grdSelectionValues.ItemCommand += new DataGridCommandEventHandler(this.grdSelectionValues_ItemCommand);
                grdSelectionValues.CancelCommand += new DataGridCommandEventHandler(this.grdSelectionValues_CancelCommand);
                grdSelectionValues.EditCommand += new DataGridCommandEventHandler(this.grdSelectionValues_Edit);
                grdSelectionValues.UpdateCommand += new DataGridCommandEventHandler(this.grdSelectionValues_Update);
            }
        }

        /// <summary>
        /// Handles the ItemCommand event of the grdCustomFields control.
        /// </summary>
        /// <param name="source">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_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            //switch(e.CommandName)
            //{
            //    case "Expand":
            //        PlaceHolder ChildRows = (PlaceHolder)e.Item.Cells[grdCustomFields.Columns.Count - 1].FindControl("ChildRows");
                    
            //        ImageButton imgbtnExpand = (ImageButton)e.Item.Cells[0].FindControl("imgbtnExpand");

            //        if (imgbtnExpand.ImageUrl == "~/images/plus.gif")
            //        {
            //            imgbtnExpand.ImageUrl = "~/images/minus.gif";
            //            ChildRows.Visible = true;
            //        }
            //        else
            //        {
            //            imgbtnExpand.ImageUrl = "~/images/plus.gif";
            //            ChildRows.Visible = false;
            //        }

            //        break;
            //}
        }

        #endregion

        private int _ProjectId = -1;


        /// <summary>
        /// Gets or sets the project id.
        /// </summary>
        /// <value>The project id.</value>
        public int ProjectId
        {
            get { return _ProjectId; }
            set { _ProjectId = value; }
        }


        /// <summary>
        /// Updates this instance.
        /// </summary>
        /// <returns></returns>
        public bool Update()
        {
            return true;
        }

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        public void Initialize()
        {
            dropDataType.DataSource = Enum.GetNames(typeof(ValidationDataType));
            dropDataType.DataBind();
            BindCustomFields();
        }

        /// <summary>
        /// Binds the custom fields.
        /// </summary>
        void BindCustomFields()
        {
            //check if we are editing the subgrid - needed to fire updatecommand on the nested grid.
            if (ViewState["EditingSubGrid"] == null)
            {
                grdCustomFields.DataSource = CustomField.GetCustomFieldsByProjectId(ProjectId);
                grdCustomFields.DataKeyField = "Id";
                grdCustomFields.DataBind();

                if (grdCustomFields.Items.Count == 0)
                    grdCustomFields.Visible = false;
                else
                    grdCustomFields.Visible = true;
            }
        }


        /// <summary>
        /// Handles the ItemDataBound event of the grdSelectionValues control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.DataGridItemEventArgs"/> instance containing the event data.</param>
        protected void grdSelectionValues_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            Control container = e.Item;
            ListItemType itemType = e.Item.ItemType;

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

                System.Web.UI.WebControls.Button btnDelete;
                btnDelete = (System.Web.UI.WebControls.Button)e.Item.FindControl("btnDelete");
                if (btnDelete != null)
                {
                    btnDelete.Attributes.Add("onclick", "return confirm('Are you sure you want to delete?')");
                }
            }
            else if (itemType == ListItemType.Footer)
            {
                if (this.ViewState["CustomFieldId"] != null)
                {
                    Button btnAddSelectionValue = (Button)e.Item.FindControl("btnAddSelectionValue");

                    if (btnAddSelectionValue != null)
                    {
                        btnAddSelectionValue.CommandArgument =this.ViewState["CustomFieldId"].ToString();
                    }
                }
                this.ViewState["CustomFieldId"] = null;
            }
           
        }

        /// <summary>
        /// Handles the Edit event of the dg2 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 grdSelectionValues_Edit(object sender, DataGridCommandEventArgs e)
        {
            lblError.Text = String.Empty;
            Label lblCustomFieldId = (Label)e.Item.FindControl("lblCustomFieldId");

            if (lblCustomFieldId != null)
            {
                foreach (DataGridItem item in grdCustomFields.Items)
                {
                    DataGrid grdSelectionValues = (DataGrid)item.FindControl("grdSelectionValues");
                    grdSelectionValues.ShowFooter = false;
                    grdSelectionValues.EditItemIndex = -1;
                    if (lblCustomFieldId.Text == item.Cells[0].Text)
                    {
                        if (null != grdSelectionValues)
                        {
                            grdSelectionValues.EditItemIndex = e.Item.ItemIndex;
                            //set a property to say we are editing the subgrid,
                            //rebinding the master grid will not fire the update command
                            ViewState["EditingSubGrid"] = true;
                        }
                    }
                    
                }
            }
       
            BindCustomFieldSelections();

        }

        /// <summary>
        /// Handles the ItemCommand event of the grdSelectionValues control.
        /// </summary>
        /// <param name="source">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 grdSelectionValues_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            CustomFieldSelection  cfs;
            int itemIndex = e.Item.ItemIndex;
            int itemId;
            switch (e.CommandName)
            {
                case "up":
                    //move row up
                    if (itemIndex == 0)
                        return;
                    itemId = Convert.ToInt32(e.Item.Cells[0].Text);
                    cfs = CustomFieldSelection.GetCustomFieldSelectionById(itemId);
                    cfs.SortOrder -= 1;
                    cfs.Save();
                    break;
                case "down":
                    //move row down
                    if (itemIndex == ((DataGrid)source).Items.Count - 1)
                        return;
                    itemId = Convert.ToInt32(e.Item.Cells[0].Text);
                    cfs = CustomFieldSelection.GetCustomFieldSelectionById(itemId);
                    cfs.SortOrder += 1;
                    cfs.Save();
                    break;
                case "add":
                    TextBox txtAddSelectionName = (TextBox)e.Item.FindControl("txtAddSelectionName");
                    TextBox txtAddSelectionValue = (TextBox)e.Item.FindControl("txtAddSelectionValue");
                    if (txtAddSelectionName.Text.Trim() == String.Empty)
                    {
                        lblError.Text = "Selection name can not be blank";
                        return;
                    }
                    if (txtAddSelectionValue.Text.Trim() == String.Empty)
                    {
                        lblError.Text = "Selection value can not be blank";
                        return;
                    }

                     cfs = new CustomFieldSelection(Convert.ToInt32(e.CommandArgument),
                        txtAddSelectionName.Text.Trim(), 
                        txtAddSelectionValue.Text.Trim());
                    cfs.Save();                  
                    break;
            }
            BindCustomFieldSelections();
        }

        /// <summary>
        /// Handles the CancelCommand event of the grdSelectionValues control.
        /// </summary>
        /// <param name="source">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 grdSelectionValues_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            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;
                }
            }
            ViewState["EditingSubGrid"] = null;
            BindCustomFieldSelections();
            BindCustomFields();
        }

        /// <summary>
        /// Handles the UpdateCommand event of the grdSelectionValues control.
        /// </summary>
        /// <param name="source">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 grdSelectionValues_Update(object source, DataGridCommandEventArgs e)
        {
            lblError.Text = String.Empty;
            if (ListItemType.EditItem == e.Item.ItemType)
            {
                TextBox txtName =  ((TextBox)e.Item.Cells[2].Controls[0]);
                TextBox txtValue =  ((TextBox)e.Item.Cells[3].Controls[0]);
                if (txtName != null && txtValue != null)
                {
                    if (txtName.Text.Trim() == String.Empty)
                    {
                        lblError.Text = "Selection name can not be blank";
                        return;

⌨️ 快捷键说明

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