ASP.NET 4.0新特性之Web.config的改进
【IT168 技术文档】ASP.NET 4.0 的新增功能:1.简洁的 web.config 文件 2. 控件的新属性ViewStateMode - 控件的视图状态模式 3. 控件的新属性 ClientIDMode - 生成客户端 ID 的方式 4. 列表控件的新属性EnablePersistedSelection - 保存选中项的方式 5. 控件的其他一些增强点,RenderOuterTable - 指定控件在客户端呈现的时候,是否在外层加 table 标签,Menu 控件,在 asp.net 4.0 中将会以 ul li 的方式呈现在客户端,RepeatLayout - 布局模式,控件在客户端的 HTML 呈现方式,Wizard 和 CreateUserWizard 新增了 LayoutTemplate 模板, 原来使用 ListView 必须要有 LayoutTemplate ,在 asp.net 4.0 中可以不再用它了.
示例
1、简洁的 web.config,配置信息被移到了 machine.config
Web.config
<?xml version="1.0"?>
<!--
清爽的 web.config
配置信息一律都放到 machine.config 里了
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
<!--
清爽的 web.config
配置信息一律都放到 machine.config 里了
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
2、ViewStateMode 属性的用法
ViewStateDemo.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="ViewStateDemo.aspx.cs" Inherits="AspDotNet.ViewStateDemo" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<!--
ViewStateMode - 控件的视图状态模式
ViewStateMode.Enabled - 此控件启用 ViewState,并且其子控件中所有 ViewStateMode 为 Inherit 的控件也全部启用 ViewState
ViewStateMode.Disabled - 此控件禁用 ViewState,并且其子控件中所有 ViewStateMode 为 Inherit 的控件也全部奇偶能用 ViewState
ViewStateMode.Inherit - 此控件是否启用 ViewState,由其父控件的 ViewStateMode 的值决定
-->
<asp:PlaceHolder ID="PlaceHolder1" runat="server" ViewStateMode="Disabled">
<!--无 ViewState-->
<asp:Label ID="Label1" runat="server" ViewStateMode="Disabled" />
<br />
<!--有 ViewState-->
<asp:Label ID="Label2" runat="server" ViewStateMode="Enabled" />
<br />
<!--无 ViewState-->
<asp:Label ID="Label3" runat="server" ViewStateMode="Inherit" />
</asp:PlaceHolder>
<br />
<!--点击“回发”按钮后观察各个 Label 控件是否启用了 ViewState-->
<asp:Button ID="Button1" runat="server" Text="回发" />
</asp:Content>
CodeBehind="ViewStateDemo.aspx.cs" Inherits="AspDotNet.ViewStateDemo" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<!--
ViewStateMode - 控件的视图状态模式
ViewStateMode.Enabled - 此控件启用 ViewState,并且其子控件中所有 ViewStateMode 为 Inherit 的控件也全部启用 ViewState
ViewStateMode.Disabled - 此控件禁用 ViewState,并且其子控件中所有 ViewStateMode 为 Inherit 的控件也全部奇偶能用 ViewState
ViewStateMode.Inherit - 此控件是否启用 ViewState,由其父控件的 ViewStateMode 的值决定
-->
<asp:PlaceHolder ID="PlaceHolder1" runat="server" ViewStateMode="Disabled">
<!--无 ViewState-->
<asp:Label ID="Label1" runat="server" ViewStateMode="Disabled" />
<br />
<!--有 ViewState-->
<asp:Label ID="Label2" runat="server" ViewStateMode="Enabled" />
<br />
<!--无 ViewState-->
<asp:Label ID="Label3" runat="server" ViewStateMode="Inherit" />
</asp:PlaceHolder>
<br />
<!--点击“回发”按钮后观察各个 Label 控件是否启用了 ViewState-->
<asp:Button ID="Button1" runat="server" Text="回发" />
</asp:Content>
ViewStateDemo.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AspDotNet
{
public partial class ViewStateDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 页面第一次加载时,分别给三个 Label 赋值,用于演示是否启用了 ViewState
if (!Page.IsPostBack)
{
Label1.Text = "Label1";
Label2.Text = "Label2";
Label3.Text = "Label3";
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AspDotNet
{
public partial class ViewStateDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 页面第一次加载时,分别给三个 Label 赋值,用于演示是否启用了 ViewState
if (!Page.IsPostBack)
{
Label1.Text = "Label1";
Label2.Text = "Label2";
Label3.Text = "Label3";
}
}
}
}
0
相关文章