<a href="http://webabcd.blog.51cto.com/1787395/341053" target="_blank">[索引頁]</a>
<a href="http://cid-ebc0a6c186317ea6.office.live.com/self.aspx/Share/VS2010.rar" target="_blank">[源碼下載下傳]</a>
精進不休 .NET 4.0 (1) - asp.net 4.0 新特性之web.config的改進, ViewStateMode, ClientIDMode, EnablePersistedSelection, 控件的其它一些改進
介紹
asp.net 4.0 的新增功能
簡潔的 web.config 檔案
控件的新屬性 ViewStateMode - 控件的視圖狀态模式
控件的新屬性 ClientIDMode - 生成用戶端 ID 的方式
清單控件的新屬性 EnablePersistedSelection - 儲存選中項的方式
控件的其他一些增強點
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>
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" />
<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
{
void Page_Load() void Page_Load(object sender, EventArgs e)
{
// 頁面第一次加載時,分别給三個 Label 指派,用于示範是否啟用了 ViewState
if (!Page.IsPostBack)
{
Label1.Text = "Label1";
Label2.Text = "Label2";
Label3.Text = "Label3";
}
}
}
}
3、ClientIDMode 屬性的用法
ClientID.aspx
<%@ Page Title="ClientID" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="ClientID.aspx.cs" Inherits="AspDotNet.ClientID" ClientIDMode="Static" %>
ClientIDMode - 生成用戶端 ID 的方式
ClientIDMode.AutoID - 生成方式和以前一樣,為保證唯一,會把其以上各層級的控件ID拿過來拼成一個頁面中的唯一ID
ClientIDMode.Inherit - 繼承父控件的用戶端ID生成方式
ClientIDMode.Static - 靜态方式。在服務端設定的ID是什麼,用戶端所呈現的ID就是什麼
ClientIDMode.Predictable - 生成ID的方式為:[Prefix]_[ID]_[Suffix]
注意:
在某控件層級中如果沒有設定 ClientIDMode,則其預設值為 AutoID
如果在控件層級中的父級控件設定了 ClientIDMode,則其子控件的預設值為 Inherit
<!-- ClientIDMode.AutoID 的 Demo -->
<fieldset>
<legend>Legacy</legend>
<asp:TextBox ID="txtLegacy" ClientIDMode="AutoID" runat="server" Text="ID: txtLegacy" />
</fieldset>
<!-- ClientIDMode.Static 的 Demo -->
<legend>Static</legend>
<asp:TextBox ID="txtStatic" ClientIDMode="Static" runat="server" Text="ID: txtStatic" />
<!-- ClientIDMode.Inherit 的 Demo (注意:Page 上的 ClientIDMode 的值為 Static,是以此控件的用戶端ID生成方式也是 Static)-->
<legend>Inherit</legend>
<asp:TextBox ID="txtInherit" ClientIDMode="Inherit" runat="server" Text="ID: txtInherit" />
<!-- Predictable 模式中自動配置設定 Suffix 的方式 -->
<legend>Predictable Repeater </legend>
<div id="repeaterContainer">
<asp:Repeater ID="repeater" runat="server" ClientIDMode="Static">
<ItemTemplate>
<div>
<asp:Label ID="productPrice" ClientIDMode="Predictable" runat="server">
<%# string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:c}", Eval("ProductPrice"))%>
</asp:Label>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
<asp:TextBox ID="txtPredictableRepeater" runat="server" TextMode="MultiLine" Rows="10"
ClientIDMode="Static" Style="width: 99%;" />
<!-- Predictable 模式中配置設定指定 Suffix 的方式(ClientIDRowSuffix 指定 Suffix 的資料來源) -->
<legend>Predictable ListView </legend>
<asp:ListView ID="listView" runat="server" ClientIDMode="Static" ClientIDRowSuffix="ProductId">
<ItemTemplate>
<div>
<asp:Label ID="productPrice" ClientIDMode="Predictable" runat="server">
</asp:Label>
</div>
</ItemTemplate>
<LayoutTemplate>
<div id="listViewContainer">
<div id="itemPlaceholder" runat="server" />
</LayoutTemplate>
</asp:ListView>
<asp:TextBox ID="txtPredictableListView" runat="server" TextMode="MultiLine" Rows="10"
<script type="text/javascript">
() () {
document.getElementById('<%= txtLegacy.ClientID %>').value += " ClientID: " + '<%= txtLegacy.ClientID %>';
document.getElementById('<%= txtStatic.ClientID %>').value += " ClientID: " + '<%= txtStatic.ClientID %>';
document.getElementById('<%= txtInherit.ClientID %>').value += " ClientID: " + '<%= txtInherit.ClientID %>';
document.getElementById('txtPredictableRepeater').value = document.getElementById('repeaterContainer').innerHTML;
document.getElementById('txtPredictableListView').value = document.getElementById('listViewContainer').innerHTML;
</script>
ClientID.aspx.cs
public partial class ClientID : System.Web.UI.Page
BindData();
// 綁定資料到 ListView
void BindData() void BindData()
Random random = new Random();
List<Product> products = new List<Product>();
for (int i = 0; i < 5; i++)
products.Add(new Product { ProductId = i + 100, ProductName = "名稱", ProductPrice = random.NextDouble() });
listView.DataSource = products;
listView.DataBind();
repeater.DataSource = products;
repeater.DataBind();
class Product
public int ProductId { get; set; }
public string ProductName { get; set; }
public double ProductPrice { get; set; }
4、EnablePersistedSelection 屬性的用法
EnablePersistedSelection.aspx
CodeBehind="EnablePersistedSelection.aspx.cs" Inherits="AspDotNet.EnablePersistedSelection" %>
EnablePersistedSelection - 儲存選中項的方式
true - 根據 DataKeyNames 指定的字段做為關鍵字儲存選中項(分頁操作不會改變選中項)
false - 根據行在目前頁的表中的索引做為關鍵字儲存選中項(分頁後,選中項會發生改變。比如,在第一頁選中了第一行,那麼分頁到第二頁的時候選此頁的第一行就會被當成選中項,也就是選中項發生了改變)
<asp:GridView ID="gridView" runat="server" AllowPaging="True" DataSourceID="ObjectDataSource1"
CellPadding="4" ForeColor="#333333" GridLines="None" EnablePersistedSelection="true"
DataKeyNames="productId">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="productId" HeaderText="productId" SortExpression="productId" />
<asp:BoundField DataField="productName" HeaderText="productName" SortExpression="productName" />
<asp:BoundField DataField="productPrice" HeaderText="productPrice" SortExpression="productPrice" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetData"
TypeName="AspDotNet.EnablePersistedSelection"></asp:ObjectDataSource>
EnablePersistedSelection.aspx.cs
public partial class EnablePersistedSelection : System.Web.UI.Page
// 為 GridView 提供資料
List<Product> GetData() List<Product> GetData()
for (int i = 0; i < 100; i++)
products.Add(new Product { ProductId = i + 1, ProductName = "名稱", ProductPrice = random.NextDouble() });
return products;
// 為 GridView 提供資料的實體類
public class Product
public int ProductId { get; set; }
public string ProductName { get; set; }
public double ProductPrice { get; set; }
5、控件的其他一些增強點
ControlsEnhancement.aspx
CodeBehind="ControlsEnhancement.aspx.cs" Inherits="AspDotNet.ControlsEnhancement" %>
RenderOuterTable - 指定控件在用戶端呈現的時候,是否在外層加 table 标簽
FormView,Login,PasswordRecovery,ChangePassword 控件均有此屬性
<asp:FormView ID="FormView1" runat="server" DefaultMode="Insert" RenderOuterTable="false">
<InsertItemTemplate>
FormView 的插入模闆
</InsertItemTemplate>
</asp:FormView>
<br /><br />
Menu 控件,在 asp.net 4.0 中将會以 ul li 的方式呈現在用戶端
<asp:Menu ID="Menu1" runat="server">
<Items>
<asp:MenuItem Text="Level 1 - Item 1" Value="1">
<asp:MenuItem Text="New Item" Value="3"></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Level 1 - Item 2" Value="2">
<asp:MenuItem Text="Level 2 - Item 1" Value="4"></asp:MenuItem>
<asp:MenuItem Text="Level 2 - Item 2" Value="5"></asp:MenuItem>
</Items>
</asp:Menu>
RepeatLayout - 布局模式,控件在用戶端的 HTML 呈現方式
RepeatLayout.Flow - 流式布局,一行一個選項
RepeatLayout.OrderedList - ol li 布局
RepeatLayout.UnorderedList - ul li 布局
RepeatLayout.Table - Table 布局
CheckBoxList,RadioButton 控件均有此屬性
<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatLayout="UnorderedList">
<asp:ListItem Text="Item1" />
<asp:ListItem Text="Item2" />
<asp:ListItem Text="Item3" />
<asp:ListItem Text="Item4" />
<asp:ListItem Text="Item5" />
<asp:ListItem Text="Item6" />
</asp:CheckBoxList>
Wizard 和 CreateUserWizard 新增了 LayoutTemplate 模闆 ,如下
headerPlaceholder - runtime時,其内容會被 HeaderTemplate 中的内容替換掉
sideBarPlaceholder - runtime時,其内容會被 SideBarTemplate 中的内容替換掉
wizardStepPlaceholder - runtime時,其内容會被 WizardStepTemplate 中的内容替換掉
navigationPlaceholder - runtime時,其内容會被導航模闆中的内容替換掉
<asp:Wizard ID="Wizard1" runat="server">
<LayoutTemplate>
<div style="background-color: Yellow">
<asp:PlaceHolder ID="headerPlaceholder" runat="server" />
</div>
<asp:PlaceHolder ID="sideBarPlaceholder" runat="server" />
<asp:PlaceHolder ID="wizardStepPlaceholder" runat="server" />
<div style="background-color: Red">
<asp:PlaceHolder ID="navigationPlaceholder" runat="server" />
</LayoutTemplate>
<HeaderTemplate>
Header
</HeaderTemplate>
<WizardSteps>
<asp:WizardStep runat="server" Title="Step 1">
</asp:WizardStep>
<asp:WizardStep runat="server" Title="Step 2">
</WizardSteps>
</asp:Wizard>
原來使用 ListView 必須要有 LayoutTemplate ,在 asp.net 4.0 中可以不再用它了
<asp:ListView ID="listView" runat="server" ClientIDRowSuffix="ProductId">
<ItemTemplate>
<div style="background-color: Fuchsia">
<%# string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:c}", Eval("ProductPrice"))%>
</ItemTemplate>
<%--
<div id="itemPlaceholder" runat="server" />
--%>
</asp:ListView>
ControlsEnhancement.aspx.cs
public partial class ControlsEnhancement : System.Web.UI.Page
{
OK
本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/341048,如需轉載請自行聯系原作者