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

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div>
        <strong>项目<br />
        </strong>
        <%-- AllowPaging="True" 允许分页
            PageSize="4" 每页4行
            DataKeyNames="Id" 强调这个设置是非常重要的,因为后台要通过获取这个Key值来唯一的定位这条记录,从而选择和删除操作
            (549)
        --%>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" AllowSorting="true" 
            DataSourceID="ProjectListDataSource" PageSize="4" DataKeyNames="Id" OnRowDeleted="GridView1_RowDeleted">
            <Columns>
            <%-- Visible="False" 这一列被隐藏 --%>
                <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" Visible="False" />
                <%-- 超链接域
                     DataNavigateUrlFields="Id"    DataNavigateUrlFormatString="ProjectEdit.aspx?id={0}
                     数据导航域ID                  数据导航URL
                                            组合
                     当数据源中包含多个必须组合用于创建 HyperLinkField 对象的超链接的字段时,请使用 DataNavigateUrlFields 属性。DataNavigateUrlFields 
                     属性中指定的字段与 DataNavigateUrlFormatString 属性中的格式字符串组合,以构造 HyperLinkField 对象中的超链接。 
                --%>
                <asp:HyperLinkField DataNavigateUrlFields="Id" DataNavigateUrlFormatString="ProjectEdit.aspx?id={0}"
                    DataTextField="Name" HeaderText="项目" />
                    <%-- ShowDeleteButton="True" SelectText="Edit" 
                        显示删除按钮             选择文本进入编辑模式
                    
                    --%>
                <asp:CommandField ShowDeleteButton="True" SelectText="Edit" />
            </Columns>
        </asp:GridView>
        <asp:LinkButton ID="NewProjectButton" runat="server" OnClick="NewProjectButton_Click">添加项目</asp:LinkButton>
        <br />
        <br />
        <asp:Label ID="ErrorLabel" runat="server" ForeColor="Red"></asp:Label>
        <csla:CslaDataSource ID="ProjectListDataSource" runat="server" TypeName="ProjectTracker.Library.ProjectList, ProjectTracker.Library"
            OnDeleteObject="ProjectListDataSource_DeleteObject" OnSelectObject="ProjectListDataSource_SelectObject"
            TypeSupportsPaging="False" TypeSupportsSorting="False">
        </csla:CslaDataSource>
        <br />
    </div>
</asp:Content>

 

//======================================================================
//   
//        Guid1:               d08d215e-c7a9-4079-95d1-54704d568a8a
//        CLR版本:             2.0.50727.3053
//        新建项输入的名称:    ProjectList
//        机器名称:            SOFTWARE-FRT-08
//        命名空间名称:        PTWeb
//        文件名:              ProjectList
//        当前系统时间:        2008-10-2 19:08:08
//        用户所在的域:        TECHSTAR
//        当前登录用户名:      frt
//        创建年份:            2008
//
//        Created by Terry.Feng.C (冯瑞涛) at 2008-10-2 19:08:08
//        
//
//======================================================================

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
{
    /// <summary>
    /// 这个类和RoleList页面几乎一样,可以参考前面的描述
    /// </summary>
    public partial class ProjectList : System.Web.UI.Page
    {
        #region 私有成员
        private void ApplyAuthorizationRules()
        {
            this.GridView1.Columns[this.GridView1.Columns.Count - 1].Visible = Project.CanDeleteObject();
            NewProjectButton.Visible = ProjectTracker.Library.Project.CanAddObject();
        }

        private ProjectTracker.Library.ProjectList GetProjectList()
        {
            object businessObject = Session["currentObject"];
            if (businessObject == null || !(businessObject is ProjectTracker.Library.ProjectList))
            {
                businessObject = ProjectTracker.Library.ProjectList.GetProjectList();
                Session["currentObject"] = businessObject;
            }
            return (ProjectTracker.Library.ProjectList)businessObject;
        }


        #endregion

        #region 事件方法

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["currentObject"] = null;
                //授权
                ApplyAuthorizationRules();
            }
            else
                ErrorLabel.Text = string.Empty;
        }

        #region GridView1

        /// <summary>
        /// 行删除之后引发此事件,清空Session,绑定GridView,因为之前采用的是理解删除,并且没有在DeleteObject事件中调用这两个动作,是无法将删除后的数据集合
        /// 反映到GridView当中的,DataBind操作会引发SelectObject事件,这个事件中又调用了GetProjectList,方法中判断Sessions,已经是Null,导致从数据库载入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            //将Session清空会引发,从数据调用新的数据装载集合
            Session["currentObject"] = null;
            //这个操作会引发SelectObject事件
            GridView1.DataBind();
        }

        protected void NewProjectButton_Click(object sender, EventArgs e)
        {
            // allow user to add a new project
            // 跳转至编辑页面,添加一个新的对象
            Response.Redirect("ProjectEdit.aspx");
        }

        #endregion


        #endregion

        #region 数据控制


        #region ProjectListDataSource

        /// <summary>
        /// 删除动作,会引发这个事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ProjectListDataSource_DeleteObject( object sender, Csla.Web.DeleteObjectArgs e)
        {
            try
            {
                //转换Guid类型,调用静态方法立即上除一个项目
                ProjectTracker.Library.Project.DeleteProject( new Guid(e.Keys["Id"].ToString()));
                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;
            }
        }

        protected void ProjectListDataSource_SelectObject( object sender, Csla.Web.SelectObjectArgs e)
        {
            //获取,并放回给数据源控件
            e.BusinessObject = GetProjectList();
        }

        #endregion


        #endregion

    }
}

 

posted on 2009-10-06 10:19  冯瑞涛  阅读(652)  评论(0编辑  收藏  举报