CSLA.Net 3.0.5プロジェクト管理例UI ProjectEdit.aspx


<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeFile="ProjectEdit.aspx.cs" Inherits="PTWeb.ProjectEdit" Title="    " %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div>
        <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
            <!--   1 -->
            <asp:View ID="MainView" runat="server">
                <!--          
                AutoGenerateRows="False"            ,          ,            DataBind()    
                -->
                <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="536px" AutoGenerateRows="False"
                    DataSourceID="ProjectDataSource" DataKeyNames="Id" OnItemDeleted="DetailsView1_ItemDeleted"
                    OnItemInserted="DetailsView1_ItemInserted" OnItemUpdated="DetailsView1_ItemUpdated"
                    OnItemCreated="DetailsView1_ItemCreated">
                    <Fields>
                        <asp:BoundField DataField="Id" HeaderText="  " ReadOnly="True" SortExpression="Id"
                            InsertVisible="False">
                            <ItemStyle Width="90%" />
                        </asp:BoundField>
                        <asp:BoundField DataField="Name" HeaderText="  " SortExpression="Name">
                            <ControlStyle Width="90%" />
                        </asp:BoundField>
                        <asp:BoundField DataField="Started" HeaderText="    " SortExpression="Started">
                            <ControlStyle Width="90%" />
                        </asp:BoundField>
                        <asp:BoundField DataField="Ended" HeaderText="    " SortExpression="Ended">
                            <ControlStyle Width="90%" />
                        </asp:BoundField>
                        <%--         ,    
                         ,  ,           
                       
                       --%>
                        <asp:TemplateField HeaderText="  " SortExpression="Description">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" TextMode="MultiLine" Width="90%" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <InsertItemTemplate>
                                <asp:TextBox ID="TextBox1" TextMode="MultiLine" Width="90%" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
                            </InsertItemTemplate>
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox1" TextMode="MultiLine" ReadOnly="true" Width="90%" runat="server"
                                    Text='<%# Bind("Description") %>'></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
                    </Fields>
                </asp:DetailsView>
                &nbsp;&nbsp;&nbsp;<br />
                <!--              ,          -->
                <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                    DataSourceID="ResourcesDataSource" Width="432px" DataKeyNames="ResourceId">
                    <Columns>
                        <asp:BoundField DataField="ResourceId" HeaderText="ResourceId" ReadOnly="True" SortExpression="ResourceId"
                            Visible="False" />
                        <%--       
                            DataTextField="FullName"   
                        --%>
                        <asp:HyperLinkField DataNavigateUrlFields="ResourceId" DataNavigateUrlFormatString="ResourceEdit.aspx?id={0}"
                            DataTextField="FullName" HeaderText="  " />
                        <asp:BoundField DataField="Assigned" HeaderText="  " ReadOnly="True" SortExpression="Assigned" />
                        <%--         ,      ,
                                       
                                   Enabled="False"     
                            DataTextField="Value" DataValueField="Key"              
                                  RoleList(     )
                            SelectedValue='<%# Bind("Role") %>'>    ,      <%# Bind("Role") %>   ,   GridView1   
                                       DDL(DropDownList)         
                         --%>
                        <asp:TemplateField HeaderText="  " SortExpression="Role">
                            <EditItemTemplate>
                                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="RoleListDataSource"
                                    DataTextField="Value" DataValueField="Key" SelectedValue='<%# Bind("Role") %>'>
                                </asp:DropDownList>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="RoleListDataSource"
                                    DataTextField="Value" DataValueField="Key" Enabled="False" SelectedValue='<%# Bind("Role") %>'>
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                    </Columns>
                </asp:GridView>
                <asp:LinkButton ID="AddResourceButton" runat="server" OnClick="AddResourceButton_Click">    </asp:LinkButton><br />
                <br />
                &nbsp;
            </asp:View>
            <!--   2 -->
            <asp:View ID="AssignView" runat="server">
                <strong>   :</strong><br />
                <!-- OnSelectedIndexChanged             -->
                <asp:GridView ID="GridView2" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                    DataKeyNames="Id" DataSourceID="ResourceListDataSource" OnSelectedIndexChanged="GridView2_SelectedIndexChanged">
                    <Columns>
                        <asp:BoundField DataField="Name" HeaderText="  " ReadOnly="True" SortExpression="Name" />
                        <asp:CommandField ShowSelectButton="True" />
                    </Columns>
                </asp:GridView>
                <asp:LinkButton ID="CancelAssignButton" runat="server" OnClick="CancelAssignButton_Click">  </asp:LinkButton><br />
                <%--          --%>
                <csla:CslaDataSource ID="ResourceListDataSource" runat="server" TypeName="ProjectTracker.Library.ResourceList, ProjectTracker.Library"
                    OnSelectObject="ResourceListDataSource_SelectObject" TypeSupportsPaging="False"
                    TypeSupportsSorting="False">
                </csla:CslaDataSource>
            </asp:View>
        </asp:MultiView>
        
        <asp:Label ID="ErrorLabel" runat="server" ForeColor="Red"></asp:Label><br />
       
         <%--               --%>
        &nbsp;<csla:CslaDataSource ID="ProjectDataSource" runat="server" TypeName="ProjectTracker.Library.Project, ProjectTracker.Library"
            OnDeleteObject="ProjectDataSource_DeleteObject" OnInsertObject="ProjectDataSource_InsertObject"
            OnSelectObject="ProjectDataSource_SelectObject" OnUpdateObject="ProjectDataSource_UpdateObject"
            TypeSupportsPaging="False" TypeSupportsSorting="False">
        </csla:CslaDataSource>
        
        <%--        ,       --%>
        <csla:CslaDataSource ID="ResourcesDataSource" runat="server" TypeName="ProjectTracker.Library.ProjectResources, ProjectTracker.Library"
            OnDeleteObject="ResourcesDataSource_DeleteObject" OnSelectObject="ResourcesDataSource_SelectObject"
            OnUpdateObject="ResourcesDataSource_UpdateObject" TypeSupportsPaging="False"
            TypeSupportsSorting="False">
        </csla:CslaDataSource>
        
        <%--       --%>
        <csla:CslaDataSource ID="RoleListDataSource" runat="server" TypeName="ProjectTracker.Library.RoleList, ProjectTracker.Library"
            OnSelectObject="RoleListDataSource_SelectObject" TypeSupportsPaging="False" TypeSupportsSorting="False">
        </csla:CslaDataSource>
        <br />
    </div>
    <br />
    &nbsp;
</asp:Content>

 
//======================================================================
//     
//        Guid1:               91a22c9c-c56a-4986-8ea5-d3db474ed789
//        CLR  :             2.0.50727.3053
//                :    ProjectEdit
//            :            SOFTWARE-FRT-08
//              :        PTWeb
//           :              ProjectEdit
//              :        2008-10-2 19:07:41
//              :        TECHSTAR
//               :      frt
//            :            2008
//
//        Created by Terry.Feng.C (   ) at 2008-10-2 19:07:41
//        
//
//======================================================================

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 ProjectTracker.Library;

namespace PTWeb
{
    public partial class ProjectEdit : System.Web.UI.Page
    {
        #region     
        private enum Views
        {
            MainView = 0,
            AssignView = 1
        }

        private void ApplyAuthorizationRules()
        {
            Project obj = GetProject();
            //   
            if (Project.CanEditObject())
            {
                if (obj.IsNew)
                    this.DetailsView1.DefaultMode = DetailsViewMode.Insert;
                else
                    this.DetailsView1.DefaultMode = DetailsViewMode.Edit;
                
                //       ,                (      )
                
                //***              
                //                 ,            ,       ,             ,         ,    
                //CSLA     ,          ,       ,                         ,       ,
                //      GridView2_SelectedIndexChanged    ,         SaveProject,        ,    ,   !
                //
                //   !   Session Obj              ! 98 
                //
                //                  ,
                //1。              ,                    ,                    


                //this.AddResourceButton.Visible = !obj.IsNew;
            }
            else
            {
                this.DetailsView1.DefaultMode = DetailsViewMode.ReadOnly;
                this.AddResourceButton.Visible = false;
            }

            //         1  
            //  Detailview    ,    Rows.Count-1        
            this.DetailsView1.Rows[this.DetailsView1.Rows.Count - 1].Visible = Project.CanEditObject();

            //   
            //             
            this.GridView1.Columns[this.GridView1.Columns.Count - 1].Visible = Project.CanEditObject();
        }

        /// <summary>
        /// /// </summary>
        /// <returns></returns>
        private Project GetProject()
        {
            //          
            //  Session,     Object,           ,      ,  Object    ,   GetProject         ,      
            //Session["currentObject"]   ,         ,    Session   ,               Session   
            object businessObject = Session["currentObject"]; //Session
            
            //     ,  
            if (businessObject == null || !(businessObject is Project))
            {
                try
                {
                    string idString = Request.QueryString["id"];
                    //    ,               
                    if (!string.IsNullOrEmpty(idString))
                    {
                        //        Guid
                        Guid id = new Guid(idString);
                        //      
                        businessObject = Project.GetProject(id);
                    }
                    else     //      
                        businessObject = Project.NewProject();


                    //             Session
                    Session["currentObject"] = businessObject; //====Session
                }
                catch (System.Security.SecurityException)
                {
                    Response.Redirect("ProjectList.aspx");
                }
            }
            //       
            return (Project)businessObject; //     ,      Session["currentObject"]   
        }

        /// <summary>
        ///             ,      ,   ,                ,(556)
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        private int SaveProject(Project project)
        {
            //         
            int rowsAffected;
            //  
            try
            {
                 //  ,   
                Session["currentObject"] = project.Save();//====Session
                //      ,        
                rowsAffected = 1;
            }
                //    (559)
            catch (Csla.Validation.ValidationException ex)
            {
                //       
                System.Text.StringBuilder message = new System.Text.StringBuilder();
                //       ,    
                message.AppendFormat("{0}<br/>", ex.Message);
                //            ,     
                if (project.BrokenRulesCollection.Count == 1)
                    message.AppendFormat("* {0}: {1}", project.BrokenRulesCollection[0].Property, project.BrokenRulesCollection[0].Description);
                else
                    //     ,          ,          <br/>,        ,        
                    foreach (Csla.Validation.BrokenRule rule in project.BrokenRulesCollection)
                        message.AppendFormat("* {0}: {1}<br/>", rule.Property, rule.Description);
                //     
                this.ErrorLabel.Text = message.ToString();
                rowsAffected = 0;
            }
                //      
            catch (Csla.DataPortalException ex)
            {
                this.ErrorLabel.Text = ex.BusinessException.Message;
                rowsAffected = 0;
            }
                //     
            catch (Exception ex)
            {
                this.ErrorLabel.Text = ex.Message;
                rowsAffected = 0;
            }
            return rowsAffected;
        }


        #endregion

        #region     

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //    
                ApplyAuthorizationRules();
            }
            else

                this.ErrorLabel.Text = string.Empty;
        }



        #endregion

        #region     

        #region ProjectDataSource
        /// <summary>
        ///   Detailview            ,             
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ProjectDataSource_DeleteObject( object sender, Csla.Web.DeleteObjectArgs e)
        {
            try
            {
                Project.DeleteProject(new Guid(e.Keys["Id"].ToString()));
                //         
                Session["currentObject"] = null; ////====Session
                e.RowsAffected = 1;
            }
            catch (Csla.DataPortalException ex)
            {
                this.ErrorLabel.Text = ex.BusinessException.Message;
                e.RowsAffected = 0;
            }
            catch (Exception ex)
            {
                this.ErrorLabel.Text = ex.Message;
                e.RowsAffected = 0;
            }
        }

        /// <summary>
        /// Detailview        
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ProjectDataSource_InsertObject(  object sender, Csla.Web.InsertObjectArgs e)
        {
            Project obj = GetProject();
            Csla.Data.DataMapper.Map(e.Values, obj, "Id");
            e.RowsAffected = SaveProject(obj);
        }

        /// <summary>
        /// ///        ,               ,                 , Detailview           
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ProjectDataSource_SelectObject(object sender, Csla.Web.SelectObjectArgs e)
        {
            e.BusinessObject = GetProject();
        }

        /// <summary>
        ///  Detailview       
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ProjectDataSource_UpdateObject(object sender, Csla.Web.UpdateObjectArgs e)
        {
            Project obj = GetProject();
            Csla.Data.DataMapper.Map(e.Values, obj);
            //      ,         
            e.RowsAffected = SaveProject(obj);
        }

        #endregion

        #region ResourcesDataSource
        /// <summary>
        /// ResourcesDataSource 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ResourcesDataSource_DeleteObject( object sender, Csla.Web.DeleteObjectArgs e)
        {
            Project obj = GetProject();
            int rid = int.Parse(e.Keys["ResourceId"].ToString());
            obj.Resources.Remove(rid);
            e.RowsAffected = SaveProject(obj);
        }

        /// <summary>
        ///                
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ResourcesDataSource_SelectObject( object sender, Csla.Web.SelectObjectArgs e)
        {
            Project obj = GetProject();
            //           
            e.BusinessObject = obj.Resources;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ResourcesDataSource_UpdateObject( object sender, Csla.Web.UpdateObjectArgs e)
        {
            //      
            Project obj = GetProject();
            //       id
            int rid = int.Parse(e.Keys["ResourceId"].ToString());
            //             
            ProjectResource res = obj.Resources.GetItem(rid);
            //    
            Csla.Data.DataMapper.Map(e.Values, res);
            //    ,
            e.RowsAffected = SaveProject(obj);
        }

        #endregion

        #region ResourceListDataSource
        /// <summary>
        /// ResourceListDataSource         ,            
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ResourceListDataSource_SelectObject(  object sender, Csla.Web.SelectObjectArgs e)
        {
            //    
            e.BusinessObject = ProjectTracker.Library.ResourceList.GetResourceList();
        }

        #endregion

        #region RoleListDataSource
        /// <summary>
        /// RoleListDataSource            ,                    ,          
        ///            ,                    ,           
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RoleListDataSource_SelectObject( object sender, Csla.Web.SelectObjectArgs e)
        {
            //       
            e.BusinessObject = RoleList.GetList();
        }

        #endregion


        #endregion

        #region     
        #region Project DetailsView

        /// <summary>
        ///  DetailsView      ,  ,      
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DetailsView1_ItemCreated(object sender, EventArgs e)
        {
            //    ,               ,      
            if (DetailsView1.DefaultMode == DetailsViewMode.Insert)
            {
                //      ,      ,        ,       
                Project obj = GetProject();
                //               Text     ,     0    ,          ,   Details     ,         
                ((TextBox)DetailsView1.Rows[1].Cells[1].Controls[0]).Text = obj.Name;
                //               Text     
                ((TextBox)DetailsView1.Rows[2].Cells[1].Controls[0]).Text = obj.Started;
                //               Text     
                ((TextBox)DetailsView1.Rows[3].Cells[1].Controls[0]).Text = obj.Ended;

                //  DetailsView1.     TextBox     Text  
                ((TextBox)DetailsView1.FindControl("TextBox1")).Text = obj.Description;
            }
        }
        /// <summary>
        ///  Details       ,MSDN:    DetailsView     “  ”   ,          。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            Project project = GetProject();

            //         
            if (!project.IsNew)
                Response.Redirect("ProjectEdit.aspx?id=" + project.Id.ToString());
        }
        /// <summary>
        ///     DetailsView     “  ”   ,          。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DetailsView1_ItemUpdated(  object sender, DetailsViewUpdatedEventArgs e)
        {
            ApplyAuthorizationRules();
        }

        /// <summary>
        ///     DetailsView     “  ”   ,          。             ,        
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DetailsView1_ItemDeleted( object sender, DetailsViewDeletedEventArgs e)
        {
            Response.Redirect("ProjectList.aspx");
        }

        #endregion

        #region Resource Grid

        /// <summary>
        /// /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AddResourceButton_Click( object sender, EventArgs e)
        {
            this.MultiView1.ActiveViewIndex =(int)Views.AssignView;
        }

        /// <summary>
        /// GridView2         ,             
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView2_SelectedIndexChanged( object sender, EventArgs e)
        {
            //         
            Project obj = GetProject();
            try
            {
                //           ,     
                //GridView2.SelectedDataKey.Value   ,          (559)
                obj.Resources.Assign(int.Parse( this.GridView2.SelectedDataKey.Value.ToString()));
                //        ,      0,  ,     1     ,       
                //     if        

                //if (SaveProject(obj) > 0)
                //{
                    this.GridView1.DataBind();
                    this.MultiView1.ActiveViewIndex = (int)Views.MainView;
                //}
            }
                //              (    ),Assign       ,           ,             ErrorLabel     
            catch (InvalidOperationException ex)
            {
                ErrorLabel.Text = ex.Message;
            }
        }

        /// <summary>
        /// /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void CancelAssignButton_Click(object sender, EventArgs e)
        {
            this.MultiView1.ActiveViewIndex = (int)Views.MainView;
        }

        #endregion

        #endregion

    }
}