[索引页]
[×××]
作者: webabcd
介绍
asp.net 4.0 的新增功能
示例
1、简洁的 web.config,配置信息被移到了 machine.config
Web.config
[×××]
精进不休 .NET 4.0 (1) - asp.net 4.0 新特性之web.config的改进, ViewStateMode, ClientIDMode, EnablePersistedSelection, 控件的其它一些改进
作者: webabcd
介绍
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
"1.0"?>
"true" targetFramework=
"4.0" />
2、ViewStateMode 属性的用法
ViewStateDemo.aspx
ViewStateDemo.aspx
<%@ Page Title=
"" Language="C#
" MasterPageFile="~/Site.Master
" AutoEventWireup="
true"
CodeBehind= "ViewStateDemo.aspx.cs" Inherits= "AspDotNet.ViewStateDemo" %>
"Content1" ContentPlaceHolderID=
"head" runat=
"server">
"Content2" ContentPlaceHolderID=
"ContentPlaceHolder1" runat=
"server">
"PlaceHolder1" runat=
"server" ViewStateMode=
"Disabled">
"Label1" runat=
"server" ViewStateMode=
"Disabled" />
"Label2" runat=
"server" ViewStateMode=
"Enabled" />
"Label3" runat=
"server" ViewStateMode=
"Inherit" />
"Button1" runat=
"server" Text=
"回发" />
CodeBehind= "ViewStateDemo.aspx.cs" Inherits= "AspDotNet.ViewStateDemo" %>
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";
}
}
}
}
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
ClientID.aspx
<%@ Page Title=
"ClientID" Language=
"C#" MasterPageFile=
"~/Site.Master" AutoEventWireup=
"true"
CodeBehind= "ClientID.aspx.cs" Inherits= "AspDotNet.ClientID" ClientIDMode= "Static" %>
"Content1" ContentPlaceHolderID=
"head" runat=
"server">
"Content2" ContentPlaceHolderID=
"ContentPlaceHolder1" runat=
"server">
CodeBehind= "ClientID.aspx.cs" Inherits= "AspDotNet.ClientID" ClientIDMode= "Static" %>
ClientID.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 ClientID : System.Web.UI.Page
{
void Page_Load() void Page_Load(object sender, EventArgs e)
{
BindData();
}
// 绑定数据到 ListView
void BindData() void BindData()
{
Random random = new Random();
List products =
new List();
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; }
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AspDotNet
{
public partial class ClientID : System.Web.UI.Page
{
void Page_Load() void Page_Load(object sender, EventArgs e)
{
BindData();
}
// 绑定数据到 ListView
void BindData() void BindData()
{
Random random = new Random();
List
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
EnablePersistedSelection.aspx
<%@ Page Title=
"" Language="C#
" MasterPageFile="~/Site.Master
" AutoEventWireup="
true"
CodeBehind= "EnablePersistedSelection.aspx.cs" Inherits= "AspDotNet.EnablePersistedSelection" %>
"Content1" ContentPlaceHolderID=
"head" runat=
"server">
"Content2" ContentPlaceHolderID=
"ContentPlaceHolder1" runat=
"server">
"gridView" runat=
"server" AllowPaging=
"True" DataSourceID=
"ObjectDataSource1"
CellPadding= "4" ForeColor= "#333333" GridLines= "None" EnablePersistedSelection= "true"
DataKeyNames= "productId">
"White" />
"True" />
"productId" HeaderText=
"productId" SortExpression=
"productId" />
"productName" HeaderText=
"productName" SortExpression=
"productName" />
"productPrice" HeaderText=
"productPrice" SortExpression=
"productPrice" />
"#990000" Font-Bold=
"True" ForeColor=
"White" />
"#990000" Font-Bold=
"True" ForeColor=
"White" />
"#FFCC66" ForeColor=
"#333333" HorizontalAlign=
"Center" />
"#FFFBD6" ForeColor=
"#333333" />
"#FFCC66" Font-Bold=
"True" ForeColor=
"Navy" />
"#FDF5AC" />
"#4D0000" />
"#FCF6C0" />
"#820000" />
"ObjectDataSource1" runat=
"server" SelectMethod=
"GetData"
TypeName= "AspDotNet.EnablePersistedSelection">
CodeBehind= "EnablePersistedSelection.aspx.cs" Inherits= "AspDotNet.EnablePersistedSelection" %>
CellPadding= "4" ForeColor= "#333333" GridLines= "None" EnablePersistedSelection= "true"
DataKeyNames= "productId">
TypeName= "AspDotNet.EnablePersistedSelection">
EnablePersistedSelection.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 EnablePersistedSelection : System.Web.UI.Page
{
void Page_Load() void Page_Load(object sender, EventArgs e)
{
}
// 为 GridView 提供数据
List GetData() List GetData()
{
Random random = new Random();
List products =
new List();
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; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AspDotNet
{
public partial class EnablePersistedSelection : System.Web.UI.Page
{
void Page_Load() void Page_Load(object sender, EventArgs e)
{
}
// 为 GridView 提供数据
List
{
Random random = new Random();
List
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
ControlsEnhancement.aspx
<%@ Page Title=
"" Language="C#
" MasterPageFile="~/Site.Master
" AutoEventWireup="
true"
CodeBehind= "ControlsEnhancement.aspx.cs" Inherits= "AspDotNet.ControlsEnhancement" %>
"Content1" ContentPlaceHolderID=
"head" runat=
"server">
"Content2" ContentPlaceHolderID=
"ContentPlaceHolder1" runat=
"server">
"FormView1" runat=
"server" DefaultMode=
"Insert" RenderOuterTable=
"false">
FormView 的插入模板
"Menu1" runat=
"server">
"Level 1 - Item 1" Value=
"1">
"New Item" Value=
"3">
"Level 1 - Item 2" Value=
"2">
"Level 2 - Item 1" Value=
"4">
"Level 2 - Item 2" Value=
"5">
"CheckBoxList1" runat=
"server" RepeatLayout=
"UnorderedList">
"Item1" />
"Item2" />
"Item3" />
"Item4" />
"Item5" />
"Item6" />
"Wizard1" runat=
"server">
"sideBarPlaceholder" runat=
"server" />
"wizardStepPlaceholder" runat=
"server" />
Header
"server" Title=
"Step 1">
"server" Title=
"Step 2">
"listView" runat=
"server" ClientIDRowSuffix=
"ProductId">
<%--
CodeBehind= "ControlsEnhancement.aspx.cs" Inherits= "AspDotNet.ControlsEnhancement" %>
FormView 的插入模板
"background-color: Yellow">
"headerPlaceholder" runat=
"server" />
"background-color: Red">
"navigationPlaceholder" runat=
"server" />
Header
"background-color: Fuchsia">
<%# string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:c}", Eval( "ProductPrice"))%>
<%# string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:c}", Eval( "ProductPrice"))%>
<%--
"background-color: Fuchsia">
--%>
"itemPlaceholder" runat=
"server" />
--%>
ControlsEnhancement.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 ControlsEnhancement : System.Web.UI.Page
{
void Page_Load() void Page_Load(object sender, EventArgs e)
{
BindData();
}
// 绑定数据到 ListView
void BindData() void BindData()
{
Random random = new Random();
List products =
new List();
for (int i = 0; i < 5; i++)
{
products.Add( new Product { ProductId = i + 1, ProductName = "名称", ProductPrice = random.NextDouble() });
}
listView.DataSource = products;
listView.DataBind();
}
class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public double ProductPrice { get; set; }
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AspDotNet
{
public partial class ControlsEnhancement : System.Web.UI.Page
{
void Page_Load() void Page_Load(object sender, EventArgs e)
{
BindData();
}
// 绑定数据到 ListView
void BindData() void BindData()
{
Random random = new Random();
List
for (int i = 0; i < 5; i++)
{
products.Add( new Product { ProductId = i + 1, ProductName = "名称", ProductPrice = random.NextDouble() });
}
listView.DataSource = products;
listView.DataBind();
}
class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public double ProductPrice { get; set; }
}
}
}
OK
[×××]
[×××]