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


<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
  CodeFile="RolesEdit.aspx.cs" Inherits="PTWeb.RolesEdit" 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">
        <%--        
            AutoGenerateColumns="False"           
            DataKeyNames="Id",        ,    ,           
            DataSourceID="RolesDataSource"    
        --%>
        <asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="False"  DataSourceID="RolesDataSource" DataKeyNames="Id">
          <Columns>
            <%-- ReadOnly="True"              
                SortExpression="Name"     
            --%>
            <asp:BoundField DataField="Id" HeaderText="  " ReadOnly="True" SortExpression="Id" />
            <asp:BoundField DataField="Name" HeaderText="  " SortExpression="Name" />
            <%--             --%>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
          </Columns>
        </asp:GridView>
        <asp:LinkButton ID="AddRoleButton" runat="server" OnClick="AddRoleButton_Click">    </asp:LinkButton><br />
      </asp:View>
      <%--   2         --%>
      <asp:View ID="InsertView" runat="server">
        <%-- OnItemInserted="DetailsView1_ItemInserted"        
          OnModeChanged="DetailsView1_ModeChanged"       
          AutoGenerateRows="False"       
          DataSourceID="RolesDataSource"    
          
          --%>
        <asp:DetailsView ID="DetailsView1" runat="server" 
          AutoGenerateRows="False" DataSourceID="RolesDataSource"
          DefaultMode="Insert" Height="50px" Width="125px" 
          DataKeyNames="Id" OnItemInserted="DetailsView1_ItemInserted"
          OnModeChanged="DetailsView1_ModeChanged">
          <Fields>
            <asp:BoundField DataField="Id" HeaderText="  "   SortExpression="Id" />
            <asp:BoundField DataField="Name" HeaderText="  "  SortExpression="Name" />
            <%--        --%>
            <asp:CommandField ShowInsertButton="True" />
          </Fields>
        </asp:DetailsView>
      </asp:View>   
    </asp:MultiView>
    <%--       ,    --%>
    <asp:Label ID="ErrorLabel" runat="server" ForeColor="Red"></asp:Label><br />
    &nbsp;&nbsp;
    <%--       (539)
    TypeName="ProjectTracker.Library.Admin.Roles, ProjectTracker.Library"                        
    --%>
    <csla:CslaDataSource ID="RolesDataSource" runat="server"
      TypeName="ProjectTracker.Library.Admin.Roles, ProjectTracker.Library" 
      OnDeleteObject="RolesDataSource_DeleteObject"
      OnInsertObject="RolesDataSource_InsertObject" 
      OnSelectObject="RolesDataSource_SelectObject"
      OnUpdateObject="RolesDataSource_UpdateObject" TypeSupportsPaging="False" TypeSupportsSorting="False">
    </csla:CslaDataSource>
  </div>
</asp:Content>

 
 
 
//======================================================================
//
//        Copyright (C)       All Rights Reserved       
//        Guid1:               ac0ac771-edca-45ec-a03c-8e780c6168ba
//        CLR  :             2.0.50727.3053
//                :    RolesEdit
//            :            SOFTWARE-FRT-08
//              :        PTWeb
//           :              RolesEdit
//              :        2008-10-2 19:08:56
//               :      frt
//            :            2008
//
//        Created by Terry.Feng.C (   ) at 2008-10-2 19:08:56
//        
//
//======================================================================

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;

namespace PTWeb
{
    /// <summary>
    ///               ,      200 ,         
    /// </summary>
    public partial class RolesEdit : System.Web.UI.Page
    {
        #region     
        //  (539),                 ,       ,
        //  ,                MainView InsertView          ,            0 1    ,          
        private enum Views
        {
            MainView = 0,
            InsertView = 1
        }

        /// <summary>
        /// (540)         
        /// </summary>
        /// <returns></returns>
        private ProjectTracker.Library.Admin.Roles GetRoles()
        {
            //    Session  
            object businessObject = Session["currentObject"];
            //      ,               ,         
            if (businessObject == null || !(businessObject is ProjectTracker.Library.Admin.Roles))
            {
                //          
                businessObject = ProjectTracker.Library.Admin.Roles.GetRoles();
                //      
                Session["currentObject"] = businessObject;
            }
            //       
            return (ProjectTracker.Library.Admin.Roles)businessObject;
        }

        /// <summary>
        ///            
        /// </summary>
        private void ApplyAuthorizationRules()
        {
            //      CanEditObject       GridView     
            this.GridView1.Columns[this.GridView1.Columns.Count - 1].Visible = ProjectTracker.Library.Admin.Roles.CanEditObject();
            //        ,                                ,      Administrator
            this.AddRoleButton.Visible = ProjectTracker.Library.Admin.Roles.CanAddObject();
        }

        #endregion

        #region     
        /// <summary>
        /// Load                  ,                  
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //      ,        
            if (!IsPostBack)
            {
                
                //      (540)
                Session["currentObject"] = null;
                //                     (547)
                ApplyAuthorizationRules();
            }
            else
                this.ErrorLabel.Text = "";
        }

       

        /// <summary>
        /// (542)     
        /// </summary>
        /// <param name="sender">      </param>
        /// <param name="e">            </param>
        protected void AddRoleButton_Click(object sender, EventArgs e)
        {
            //         
            this.DetailsView1.DefaultMode = DetailsViewMode.Insert;
            //    DetailsView   View,         ,          
            MultiView1.ActiveViewIndex = (int)Views.InsertView;
        }

        /// <summary>
        ///       
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DetailsView1_ItemInserted( object sender, DetailsViewInsertedEventArgs e)
        {
            //     ,View    
            MultiView1.ActiveViewIndex = (int)Views.MainView;
            //(545)             ,DataBind     GridView    ,    RolesDataSource SelectObject   
            this.GridView1.DataBind();
        }

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

        #endregion

        #region     

        /// <summary>
        ///   ,              (547)
        /// </summary>
        /// <param name="sender">            ,       ,        。                  ,                     ,        ,
        ///        ,     ,               ,           。</param>
        /// <param name="e">                 ,             (                   ),               </param>
        protected void RolesDataSource_DeleteObject(object sender, Csla.Web.DeleteObjectArgs e)
        {
            try
            {
                ProjectTracker.Library.Admin.Roles obj = GetRoles();
                //               ,
                int id = (int)e.Keys["Id"];
                //              
                obj.Remove(id);

                //  Save    
                Session["currentObject"] = obj.Save();

                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>
        /// (543)     DetailsView    Role,          ,RolesDataSource       ,           
        /// 
        /// </summary>
        ///<param name="sender">            ,       ,        。                  ,                     ,        ,
        ///        ,     ,               ,           。</param>
        /// <param name="e">                 ,             (                   ),               </param>
        protected void RolesDataSource_InsertObject( object sender, Csla.Web.InsertObjectArgs e)
        {
            try
            {
                //(544)      
                ProjectTracker.Library.Admin.Roles obj = GetRoles();
                //  AddNew  ,           AddNewCore  ,  Roes.cs,    
                ProjectTracker.Library.Admin.Role role = obj.AddNew();
                //(544)  Values     , Role     ,        ,   Role.Name=e.Values["Name"].ToString;    
                Csla.Data.DataMapper.Map(e.Values, role);
                
                //  Save  ,           ,      ,             
                //               Session          ,                 ,         ,             
                Session["currentObject"] = obj.Save();
                //    
                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>
        ///   
        /// </summary>
        /// <param name="sender">            ,       ,        。                  ,                     ,        ,
        ///        ,     ,               ,           。</param>
        /// <param name="e">                 ,             (                   ),               </param>
        protected void RolesDataSource_SelectObject( object sender, Csla.Web.SelectObjectArgs e)
        {
            //      
            ProjectTracker.Library.Admin.Roles obj = GetRoles();
            //   ,  
            e.BusinessObject = obj;
        }

        /// <summary>
        ///   ,                     ,
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RolesDataSource_UpdateObject( object sender, Csla.Web.UpdateObjectArgs e)
        {
            ProjectTracker.Library.Admin.Roles obj = GetRoles();
            try
            {
                //        ,    ,                
                //e.Keys          DataKeyNames    
                ProjectTracker.Library.Admin.Role role = obj.GetRoleById(int.Parse(e.Keys["Id"].ToString()));
                //              Name  ,         Map  ,      
                role.Name = e.Values["Name"].ToString();
                //      
                Session["currentObject"] = obj.Save();
                //   
                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;
            }
        }

        #endregion
    }
}