[索引页]
[×××]


精进不休 .NET 4.0 (3) - asp.net 4.0 新特性之动态数据(Dynamic Data)增强, AJAX增强; IDE之Visual Studio 2010增强


作者:webabcd


介绍
asp.net 4.0 的新增功能
动态数据(Dynamic Data)增强(之前的版本:http://webabcd.blog.51cto.com/1787395/341156
  • EnableDynamicData - 启用 Dynamic Data 的功能 
  • DynamicHyperLink - 用于方便地生成在 Dynamic Data 站点中导航的超级链接 
  • Entity Template - 实体模板是一个新增的用于自定义数据显示的模板,其基于 FormView 控件做数据呈现 
  • DisplayAttribute - 新增的一个 Attribute(可以设置字段的Name和Order) 
  • 其它新特性
AJAX 增强
Visual Studio 2010 增强


示例
1、Dynamic Data 之 EnableDynamicData
Demo/EnableDynamicData.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EnableDynamicData.aspx.cs"
        Inherits="DynamicData.Demo.EnableDynamicData" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
        <title></title>
</head>
<body>
        <form id="form1" runat="server">
        <div>
                <!--收集并显示由 Dynamic Data 所做的数据验证的结果-->
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" EnableClientScript="true" HeaderText="验证错误的列表" />

                <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" DataKeyNames="ProductID"
                        DataSourceID="EntityDataSource1">
                        <Fields>
                                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
                        </Fields>
                </asp:DetailsView>
                <asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=AdventureWorksEntities"
                        DefaultContainerName="AdventureWorksEntities" EnableDelete="True" EnableInsert="True"
                        EnableUpdate="True" EntitySetName="Products" EnableFlattening="False">
                </asp:EntityDataSource>
        </div>
        </form>
</body>
</html>
 
Demo/EnableDynamicData.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DynamicData.Demo
{
        public partial class EnableDynamicData : System.Web.UI.Page
        {
void Page_Init() void Page_Init()
                {
                        /*
                         * EnableDynamicData - 启用 Dynamic Data 的功能(包括字段模板,数据验证在内的等等 Dynamic Data 的功能都会被启用)
                         *         第一个参数:需要启用 Dynamic Data 功能的实体类型
                         *         第二个参数:当控件启用插入模板的时候,为字段指定默认值
                         */
                        DetailsView1.EnableDynamicData(typeof(Product), new { Name = "默认名称" });
                }

void Page_Load() void Page_Load(object sender, EventArgs e)
                {

                }
        }
}
 
 
2、Dynamic Data 之 DynamicHyperLink
Demo/DynamicHyperLinkDemo.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DynamicHyperLinkDemo.aspx.cs"
        Inherits="DynamicData.Demo.DynamicHyperLinkDemo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
        <title></title>
</head>
<body>
        <form id="form1" runat="server">
        <div>
                <!--
                        DynamicHyperLink - 用于方便地生成在 Dynamic Data 站点中导航的超级链接
                                Action - 指定 Action(可选值有 List|Details|Edit|Insert)
                                TableName - 需要链接到的目标表名
                                ContextTypeName - 上下文的类全名
                -->
                <asp:DynamicHyperLink ID="ListHyperLink" runat="server" Text="全部产品" Action="List"
                        TableName="Products" ContextTypeName="DynamicData.AdventureWorksEntities">    
                </asp:DynamicHyperLink>

                <!--
                        生成的 HTML 代码如下:
                        <a id="ListHyperLink" href="/Products/List.aspx">全部产品</a>
                -->
        </div>
        </form>
</body>
</html>
 
 
3、Dynamic Data 之 Entity Template
DynamicData/EntityTemplates/Products.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Products.ascx.cs" Inherits="DynamicData.DynamicData.EntityTemplates.Products" %>

<!--
        Entity Template - 实体模板是一个新增的用于自定义数据显示的模板,其是基于 FormView 控件做数据呈现的
        以下演示如何实现自定义的 Entity Template ,进入产品详情页可查看此 Demo 的效果
        以下内容会被替换到 PageTemplates/Details.aspx <asp:DynamicEntity runat="server" /> 中去
-->
<tr>
        <td>
                产品ID
        </td>
        <td>
                <asp:DynamicControl ID="DynamicControl1" runat="server" DataField="ProductID" />
        </td>
</tr>
<tr>
        <td>
                产品名称
        </td>
        <td>
                <asp:DynamicControl ID="DynamicControl2" runat="server" DataField="Name" />
        </td>
</tr>
 
DynamicData/EntityTemplates/Products.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DynamicData.DynamicData.EntityTemplates
{
        // 自定义的 EntityTemplate 的基类是 System.Web.DynamicData.EntityTemplateUserControl
        public partial class Products : System.Web.DynamicData.EntityTemplateUserControl
        {
                
        }
}
 
 
4、Dynamic Data 之 DisplayAttribute
Demo/Metadata.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.ComponentModel.DataAnnotations;

namespace DynamicData
{
        [MetadataType(typeof(Product_Metadata))]
        public partial class Product
        {
        
        }

        public class Product_Metadata
        {
                // DisplayAttribute - 新增的一个 Attribute
                //         Name - 指定字段的名称(参考 Demo/EnableDynamicData.aspx ,其“ProductID”字段显示为“产品ID”)
                //         Order - 指定字段在所有字段中的排序
                [Display(Name="产品ID", Order=0)]
                public object ProductID { get; set; }

                [Display(Name = "产品名称", Order = 1)]
                public object Name { get; set; }

                [Range(1, 100, ErrorMessage="{0}的范围必须是{1}-{2}")]
                [Display(Name = "单价", Order = 2)]
                public object ListPrice;
        }
}
 
 
5、Dynamic Data 之 其它新特性
Demo/Others.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Others.aspx.cs" Inherits="DynamicData.Demo.Others" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
        <title></title>
        <style>
                body
                {
                        font-size: 12px;
                }
                textarea
                {
                        width: 99%;
                }
        </style>
</head>
<body>
        <form id="form1" runat="server">
        <div>
                <p>
                        1、DynamicDataManager 控件新增了 DataControls 属性,在其内可以声明多个 DataControlReference,用于指定哪些控件需要启用 Dynamic Data 的功能
                        <textarea>
                                <asp:DynamicDataManager ID="DynamicDataManager1" runat="server" AutoLoadForeignKeys="true">    
                                        <DataControls>    
                                                <asp:DataControlReference ControlID="GridView1" />    
                                        </DataControls>
                                </asp:DynamicDataManager>    
                                <asp:GridView id="GridView1" runat="server" />
                        </textarea>
                </p>
                <p>
                        2、新增了一个 EnumDataTypeAttribute,可以声明某字段为枚举类型。对应的字段模板里也新增了 Enumeration.ascx 和 Enumeration_Edit.ascx
                </p>
                <p>
                        3、新增了两个数据类型    
                        <ul>
                                <li>[DataType(DataType.EmailAddress)] 对应的字段模板里也新增了 EmailAddress.ascx</li>
                                <li>[DataType(DataType.Url)] 对应的字段模板里也新增了 Url.ascx</li>
                        </ul>
                </p>
                <p>
                        4、支持多对多的关系(限 Entity Framework 模型)。对应的字段模板里也新增了 ManyToMany.ascx 和 ManyToMany_Edit.ascx
                </p>
                <p>
                        5、原 Dynamic Data 会自动地将类型为 Boolean, Enumeration, ForeignKey 的字段作为查询条件输出到页面上。现在新增了 Filter Template(查询模板),可以对这些查询条件的样式和逻辑等做自定义修改(详见 DynamicData/Filters 下的文件)
                </p>
        </div>
        </form>
</body>
</html>
 
 
6、AJAX 增强
List.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
        <title></title>
        <style>
                body
                {
                        font-size: 12px;
                }
                textarea
                {
                        width: 99%;
                }
        </style>
</head>
<body>
        <p>
                1、Visual Studio 2010 内包含了 jQuery
        </p>
        <p>
                2、JavaScript 框架的 CDN
                <ul>
                        <li>jQuery - <script src="http://ajax.Microsoft.com/ajax/jquery-1.3.2.js" type="text/javascript"></script></li>
                        <li>asp.net ajax - <asp:ScriptManager ID=”ScriptManager1” EnableCdn=”true” runat=”server” /></li>
                </ul>
        </p>
        <p>
                3、asp.net ajax 的按需加载脚本,如下所示
                <textarea rows="10">
<asp:ScriptManager ID="ScriptManager1" AjaxFrameworkMode="Explicit" runat="server">
        <Scripts>
                <asp:ScriptReference Name="MicrosoftAjaxCore.js" />
                <asp:ScriptReference Name="MicrosoftAjaxComponentModel.js" />
                <asp:ScriptReference Name="MicrosoftAjaxSerialization.js" />
                <asp:ScriptReference Name="MicrosoftAjaxNetwork.js" />        
        </Scripts>
</asp:ScriptManager>
                </textarea>
        </p>
</body>
</html>
 
 
7、Visual Studio 2010 增强
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
        CodeBehind="List.aspx.cs" Inherits="VisualStudio.List" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
        <style>
                body
                {
                        font-size: 12px;
                }
                textarea
                {
                        width: 99%;
                }
        </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <p>
                1、HTML 和 JavaScript 支持 Code Snippets(代码片段)
                <ul>
                        <li>组合键 ctrl+k ctrl+x 用于插入代码片段</li>
                        <li>组合键 ctrl+k ctrl+b 用于调出代码片段管理器</li>
                        <li>
                                比如要在 HTML 的代码片段中增加一个 fieldset 的代码片段,可参考如下写法,将其保存为 snippet 格式的文件保存到“我的文档\Visual Studio 2010\Code Snippets\Visual Web Developer\My HTML Snippets”
                                <textarea rows="6">
<CodeSnippet Format="1.1.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
        <Header>
                <Title>fieldset</Title>
                <Author>webabcd</Author>
                <Shortcut>fieldset</Shortcut>
                <Description>Markup snippet for a fieldset</Description>
                <SnippetTypes>
                        <SnippetType>Expansion</SnippetType>
                        <SnippetType>SurroundsWith</SnippetType>
                </SnippetTypes>
        </Header>
        <Snippet>
                <Declarations>
                        <Literal>
                                <ID>legend</ID>
                                <ToolTip>legend</ToolTip>
                                <Default>标题</Default>
                        </Literal>
                        <Literal>
                                <ID>content</ID>
                                <ToolTip>content</ToolTip>
                                <Default>content</Default>
                        </Literal>
                </Declarations>
                <Code Language="html">
                        <![CDATA[<fieldset>
                                <legend>$legend$</legend>
                                <div>
                                        $content$
                                </div>
                        </fieldset>$end$]]>
                </Code>
        </Snippet>
</CodeSnippet>
                                </textarea>
                        </li>
                </ul>
        </p>
        <p>
                2、根据不同配置生成不同版本的 Web.config 文件
                <ul>
                        <li>在 Web.config 文件点右键,选择 “Add Config Transforms”,可生成不同配置的 Web.config 文件(生成包的时候,按哪种配置生成则生成哪种配置的 Web.config 文件)</li>
                        <li>默认有两种配置,即 Debug 和 Release ,可以在 Build -> Configuration Manager 中做 新增/删除/修改/更新 配置的操作</li>
                </ul>
        </p>
        <p>
                3、智能提示的改进。比如在对象后面“点”属性的时候,它将根据已输入的字母做筛选。例:如果在 TextBox 对象后键入 text, 则智能提示只会显示 Text, TextChanged, TextMode(以前会把对象的所有属性、方法、事件都列出来)
        </p>
        <p>
                4、将光标放到某一个标识上的时候,则该页所有使用了此标识的地方都会被 IDE 突出显示出来
        </p>
        <p>
                5、新增了一个开发环境配置 - Code Optimized ,在此模式下只有 html 编辑视图,而没有设计视图。在第一次启动 Visual Studio 2010 的时候,可以选择开发环境配置。以后如果需要修改开发环境配置,可以在此处操作:Tools > Import & Export Settings > Reset all settings option
        </p>
        <p>
                6、在项目属性中新增了两个选项卡,Package/Publish Web 和 Package/Publish SQL,分别用于生成 Web 包和 SQL 包
        </p>
</asp:Content>