Asp.Net 4.0 新特性,输出更纯净的Html代码 ClientIDMode,ViewStateMode等
1. 在Asp.Net 4.0 中我们可以控制输出html中的对象id的形式了,之前我们常常看到类似这样的id
ctl00_MainContent_txtAutoID
这在客户端控制时很不方便,也没有多少实质的意义
现在我们可以通过设置控件的ClientIDMode修改clientId的输出形式,例如下面代码:
1
2
3
4
|
<
asp:TextBox
ID
=
"txtPredictable"
runat
=
"server"
ClientIDMode
=
"Predictable"
></
asp:TextBox
>
<
asp:TextBox
ID
=
"txtAutoID"
runat
=
"server"
ClientIDMode
=
"AutoID"
></
asp:TextBox
>
<
asp:TextBox
ID
=
"txtInherit"
runat
=
"server"
ClientIDMode
=
"Inherit"
></
asp:TextBox
>
<
asp:TextBox
ID
=
"txtStatic"
runat
=
"server"
ClientIDMode
=
"Static"
></
asp:TextBox
>
|
将分别输出
1
2
3
4
|
<
input
name
=
"ctl00$MainContent$txtPredictable"
type
=
"text"
id
=
"MainContent_txtPredictable"
/>
<
input
name
=
"ctl00$MainContent$txtAutoID"
type
=
"text"
id
=
"ctl00_MainContent_txtAutoID"
/>
<
input
name
=
"ctl00$MainContent$txtInherit"
type
=
"text"
id
=
"MainContent_txtInherit"
/>
<
input
name
=
"ctl00$MainContent$txtStatic"
type
=
"text"
id
=
"txtStatic"
/>
|
AutoID: 自动输出和之前版本输出的clientID一样
Static:输出和server端指定的id一样的id
Predictable:对于dataGrid,listView特别有用,使ClientID值格式象clientIDrowsuffix一样;
Inherit:这个值指定控件象它的父对象一样产生ID,即我们常说的继承。
2. ASP.NET4.0 提供了一个叫做 ViewStateMode 的新属性,这个属性可以单独设置控件的视图状态。在以前版本的 ASP.NET 中,控件的视图只有在 Page 的 ViewState 启用的前提下才可以单独控制。在 ASP.NET4.0 中,控件的视图状态可以单独设置了,也就是说,即使页面的视图状态没有启用,控件依然可以启用视图状态。
ViewStateMode 属性有三种取值:
Inherit:视图状态从父控件继承;
Enabled:即使父控件的视图状态没有启用,也启用该控件的视图状态;
Disabled:即使父控件的视图状态启用了,也禁用此控件的视图状态。
3. CheckBoxList和RadioButtonList,以及Menu控件都支持使用ul或者ol输出html,这个很简单知道有这么回事,用的时候去用就好了
C# 4.0 新特性dynamic、可选参数、命名参数等
1.dynamic ExpandoObject
熟悉js的朋友都知道js可以这么写 :
1
2
3
|
var t =
new
Object();
t.Abc = ‘something’;
t.Value = 243;
|
现在这个js动态语言的特性,我们也可以在c#中使用了,前提是将一个变量声明为ExpandoObject类型。如下例:
1
2
3
4
5
6
7
8
|
static
void
Main(
string
[] args)
{
dynamic t =
new
ExpandoObject();
t.Abc =
"abc"
;
t.Value = 10000;
Console.WriteLine(
"t's abc = {0},t's value = {1}"
, t.Abc, t.Value);
Console.ReadLine();
}
|
C# 4.0中新增了一个命名空间System.Dynamic来实现对此应用的支持,这种用法的意义何在,现在我还不太清楚,也是是c#向动态语言过渡的一种试探吧。
2.泛型自动转换
在C#4.0之前下面的代码是不可以编译通过的
1
2
3
|
IEnumerable<
object
> objs =
new
List<
string
> {
"I'm 0"
,
"I'am 1"
,
"I'am 2"
};
|
但是在c#4.0中这种声明是允许的,不过也仅限于泛型接口,泛型类型的类似做法是不允许的,如下代码是有编译错误的
1
2
3
|
List<
object
> objList =
new
List<
string
> {
"I'am 0"
,
"I'am 1"
,
"I'am 2"
};
|
3.方法参数之可选参数
如下方法声明的语法
1
2
3
|
static
void
DoSomething(
int
notOptionalArg,
string
arg1 =
"default Arg1"
,
string
arg2 =
"default arg2"
) {
Console.WriteLine(
"arg1 = {0},arg2 = {1}"
,arg1,arg2);
}
|
这个方法有三个参数第一个是必选参数,第二个和第三个是可选参数,他们都有一个默认值。这种形式对固定参数的几个方法重载很有用。
如下调用:
1
2
3
4
5
6
7
|
static
void
Main(
string
[] args)
{
DoSomething(1);
DoSomething(1,
"葫芦"
);
DoSomething(1,
"葫芦"
,
"黄瓜"
);
Console.ReadLine();
}
|
也许你会想到,假如我有一个和可选参数方法不选某个参数相同的方法签名的方法时,c#会怎么处理呢,我们看下下面的代码
1
2
3
4
|
static
void
DoSomething(
int
notOpArg,
string
arg)
{
Console.WriteLine(
"arg1 = {0}"
, arg);
}
|
我又重载了一个DoSomething这个方法有两个参数,但是没有可选参数,实验证明调用
DoSomething(1,”arg”)时会优先执行没有可选参数的方法。
4.方法参数之命名参数
命名参数让我们可以在调用方法时指定参数名字来给参数赋值,这种情况下可以忽略参数的顺序。如下方法声明:
1
2
3
|
static
void
DoSomething(
int
height,
int
width,
string
openerName,
string
scroll) {
Console.WriteLine(
"height = {0},width = {1},openerName = {2}, scroll = {3}"
,height,width,openerName,scroll);
}
|
我们可以这样来调用上面声明的方法
1
2
3
4
5
|
static
void
Main(
string
[] args)
{
DoSomething( scroll :
"no"
,height : 10, width : 5, openerName :
"windowname"
);
Console.ReadLine();
}
|
很显然的这是一个语法糖,但是在方法参数很多的情况下很有意义,可以增加代码的可读性。