VS 2008 sp1 + .NET 3.5 sp1(8) - Dynamic Data(动态数据)

介绍
以Northwind为示例数据库,演示Dynamic Data(动态数据)
MetaModel - 数据库和域对象之间的映射的抽象
MetaModel.RegisterContext() - 使用指定的配置上下文注册指定的数据上下文
Scaffold - 译为基架。即基于数据库架构(linq to sql 或 entity framework)生成网页模板的机制
ScaffoldTableAttribute(false) - 隐藏指定的表
ScaffoldColumn(false) - 隐藏指定的字段
MetadataTypeAttribute(Type metadataClassType) - 指定要与数据模型类关联的元数据类
DynamicField - 显示指定的动态数据字段,相当于 BoundField
DynamicControl - 通过指定的字段模板显示指定的动态数据字段


示例
全局配置
Global.asax




   
    public static void RegisterRoutes(RouteCollection routes)
    {
        MetaModel model = new MetaModel();

        // MetaModel - 数据库和域对象之间的映射的抽象
        // MetaModel.RegisterContext(Type contextType, ContextConfiguration configuration) - 使用指定的配置上下文注册指定的数据上下文
        //     contextType - 数据模型中所定义的数据上下文类型
        //     configuration - 相关的配置。其 ScaffoldAllTables 属性为是否要启用基架,基架就是基于数据库架构(linq to sql 或 entity framework)生成网页模板的机制
        model.RegisterContext(typeof(VS2008SP1.Business.NorthwindEntities), new ContextConfiguration() { ScaffoldAllTables = true });

        // 下面的语句支持分页模式,在这种模式下,“列表”、“详细”、“插入”
        // 和“更新”任务是使用不同页执行的。若要启用此模式,请取消注释下面
        // 的 route 定义,并注释掉后面的合并页模式部分中的 route 定义。
        routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
        {
            Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
            Model = model
        });

        // 下面的语句支持合并页模式,在这种模式下,“列表”、“详细”、“插入”
        // 和“更新”任务是使用同一页执行的。若要启用此模式,请取消注释下面
        // 的 routes,并注释掉上面的分页模式部分中的 route 定义。
        // routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
        //     Action = PageAction.List,
        //     ViewName = "ListDetails",
        //     Model = model
        // });

        // routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
        //     Action = PageAction.Details,
        //     ViewName = "ListDetails",
        //     Model = model
        // });
    }

    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);
    }

 


1、数据驱动的 Web 应用程序
详见源代码中的DynamicDataSite项目。动态数据的目录结构详见MSDN
Scaffold.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace VS2008SP1.Business
{
    /**//*
     * Scaffold - 译为基架。即基于数据库架构(linq to sql 或 entity framework)生成网页模板的机制
     * ScaffoldTableAttribute(false) - 隐藏指定的表
     * ScaffoldColumn(false) - 隐藏指定的字段
     * MetadataTypeAttribute(Type metadataClassType) - 指定要与数据模型类关联的元数据类
     */

    [ScaffoldTable(false)]
    public partial class Region
    {
        // Region 表不会被路由(显示)
    }

    [MetadataType(typeof(Customers_Metadata))]
    public partial class Customers
    {
        // 将 Customers 的元数据关联到 Customers_Metadata
    }

    public class Customers_Metadata
    {
        [ScaffoldColumn(false)]
        public object Phone;

        // Phone 不会在 Customers 表中被显示
    }
}


Validation.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace VS2008SP1.Business
{
    [MetadataType(typeof(Products_Metadata))]
    public partial class Products
    {
        // entity framework 会自动生成类似 OnFieldChanging() 的部分方法
        // 如果想做字段的自定义输入验证,则可以重写此方法
        partial void OnUnitPriceChanging(global::System.Nullable value)
        {
            if (value > 1000)
            {
                throw new ValidationException("UnitPrice 不能大于 1000");
            }
        }
    }

    public class Products_Metadata
    {
        // [DataType(DataType.EmailAddress)] // 指定要与数据字段关联的附加类型的名称
        // [DisplayFormat()] // 格式化输出
        // [Range()] // 指定字段的范围约束
        // [RegularExpression()] // 正则表达式验证
        // [StringLength()] // 字段的字符长度验证
        [Required()] // 必填
        [UIHint("MyDecimal")] // 使用名为 MyDecimal 的字段模板
        public object UnitPrice;

        [DisplayName("产品名称")] // 指定的字段所显示的名称。在动态数据中,查看 Products 表,其 header 将显示为 产品名称
        [StartsWith("webabcd", ErrorMessage = "{0} 必须以 {1} 开头")] // 应用自定义 ValidationAttribute
        public object ProductName { get; set; }

    }


    // 编写一个自定义 ValidationAttribute,验证指定字段是否是以指定的字符串开头
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    sealed public class StartsWithAttribute : ValidationAttribute
    {
        readonly string _param;

        /**
        /// 构造函数
        ///
        /// 指定的开头字符串
        public StartsWithAttribute(string param)
        {
            _param = param;
        }

        /**
        /// 是否通过验证
        ///
        /// 输入值
        ///
        public override bool IsValid(object value)
        {
            return ((string)value).ToLower().StartsWith(this._param.ToLower());
        }

        /**
        /// 格式化错误信息
        ///
        /// 指定的字段名
        ///
        public override string FormatErrorMessage(string name)
        {
            return string.Format(ErrorMessageString, name, this._param);
        }
    }
}

 

2、以 Products 表为例,演示动态数据的应用
MyProducts.aspx

    Inherits="MyProducts" Title="以 Products 表为例,演示动态数据的应用" %>

    Namespace="System.Web.UI.WebControls" TagPrefix="asp" %>

   
   


        以 Products 表为例,演示动态数据的应用


            DataKeyNames="ProductId">
       
           

                        ProductId:
                   

                        <!--DynamicField - 显示指定的动态数据字段,相当于 BoundField--&gt
                        <!--DynamicControl - 通过指定的字段模板显示指定的动态数据字段--&gt
                       
                   

                        ProductName:
                   

                       
                   

                        UnitPrice:
                   

                       
                   

                                                    Text="New" />
                                                    Text="Edit" />
                                                    Text="Delete" />
                   

       
       
           

                        ProductId:
                   

                       
                   

                        ProductName:
                   

                        <!--
                            UIHint - 指定字段模板,此例的字段模板会以黄色背景显示数据
                            Mode - 设置呈现模式 [System.Web.UI.WebControls.DataBoundControlMode 枚举]
                                DataBoundControlMode.ReadOnly - 只读模式。默认值
                                DataBoundControlMode.Edit - 编辑模式
                                DataBoundControlMode.Insert - 插入模式
                        --&gt
                                                    UIHint="YelloText" />
                   

                        UnitPrice:
                   

                       
                   

                        Update
                        Cancel
                   

       
       
           

                        ProductName:
                   

                       
                   

                       
                                                    Text="Cancel" />
                   

       
       
   
            DefaultContainerName="NorthwindEntities" EntitySetName="Products" ContextTypeName="VS2008SP1.Business.NorthwindEntities"
        EnableInsert="True" EnableUpdate="True" EnableDelete="True">
   


 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-557466/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12639172/viewspace-557466/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值