创建自定义的绑定表达式 简单应用
创建自定义的绑定表达式 简单应用
在dotnet中的绑定表达式中,#算是大家用得最多的,另外还有几个专用的绑定表达式比如,appSettings,ConnectionStrings,Resources等,用起来都很方便.dotnet里提供了一个ExpressionBuilder的类,继承该类可实现自定义的绑定表达式.以下是定义一个绑定颜色的绑定表达式.该颜色的值保存在profile中,对于一些服务器控件,比如textbox的颜色绑定,如果直接绑定个字符串,那么会出现运行错误的,应为textobx的BackColor只认Color类型的值,而如果在profile中存放个color类型的值,那么写到数据库里还得来个转换,操作起来麻烦.于是才有了弄个颜色绑定表达式的想法.(也许还有更好的解决方案).具体实现如下:
1.定义个绑定表达式的类型,继承于ExpressionBuilder
![](/Images/OutliningIndicators/ContractedBlock.gif)
ColorExpressionBuilder
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
using System.Web.Compilation;
11
using System.CodeDom;
12
using System.Drawing;
13![](/Images/OutliningIndicators/None.gif)
14
namespace CustomExpressionBuilder
15![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
16![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
17
/// 用于绑定颜色的自定义绑定表达式,继承于ExpressionBuilder
18
/// </summary>
19
[ExpressionEditor(typeof(ColorExpressionBuilder))]
20
[ExpressionPrefix("Color")]
21
public class ColorExpressionBuilder : ExpressionBuilder
22![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
24
/// 转换输入的绑定表达式
25
/// </summary>
26
/// <param name="expression"></param>
27
/// <param name="propertyType"></param>
28
/// <param name="context"></param>
29
/// <returns></returns>
30
public override object ParseExpression(string expression, Type propertyType, ExpressionBuilderContext context)
31![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
return expression;
33
}
34![](/Images/OutliningIndicators/InBlock.gif)
35![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
36
/// 表达式的核心方法,用于计算表达式的值
37
/// </summary>
38
/// <param name="entry"></param>
39
/// <param name="parsedData">ParseExpression转换后的数据</param>
40
/// <param name="context"></param>
41
/// <returns>返回代码表达式</returns>
42
public override System.CodeDom.CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
43![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
44
// 以下内容用codedom构建一个方法表达式,调用GetColorValue
45
// 参数值
46
CodePrimitiveExpression prim = new CodePrimitiveExpression(parsedData);
47
// 参数
48![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
CodeExpression[] args = new CodeExpression[]
{ prim };
49
// 引用对象
50
CodeTypeReferenceExpression refType = new CodeTypeReferenceExpression(GetType());
51
// 返回方法表达式
52
return new CodeMethodInvokeExpression(refType, "GetColorValue", args);
53![](/Images/OutliningIndicators/InBlock.gif)
54
}
55![](/Images/OutliningIndicators/InBlock.gif)
56![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
57
/// 从Profile中获取自定义的颜色的值,在计算表达式的值时用
58
/// </summary>
59
/// <param name="colorPropertyName"></param>
60
/// <returns></returns>
61
public static Color GetColorValue(string colorPropertyName)
62![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
63
string colorName = (string)HttpContext.Current.Profile.GetPropertyValue(colorPropertyName);
64
65
return Color.FromName(colorName);
66
}
67![](/Images/OutliningIndicators/InBlock.gif)
68![](/Images/OutliningIndicators/ContractedSubBlock.gif)
以下用于非编译页面#region 以下用于非编译页面
69![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
70
/// 计算绑定表达式的值,该方法用于非编译页面
71
/// </summary>
72
/// <param name="target"></param>
73
/// <param name="entry"></param>
74
/// <param name="parsedData"></param>
75
/// <param name="context"></param>
76
/// <returns></returns>
77
public override object EvaluateExpression(object target, BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
78![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
79
string propertyName = (string)target;
80
return GetColorValue(propertyName);
81
}
82![](/Images/OutliningIndicators/InBlock.gif)
83![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
84
/// 支持非编译页面的绑定调用
85
/// </summary>
86
public override bool SupportsEvaluate
87![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
88
get
89![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
90
return true;
91
}
92
}
93
#endregion
94
}
95
}
96
2.配置web.config
![](/Images/OutliningIndicators/ContractedBlock.gif)
注册表达式
1
<!--注册表达式的前缀-->
2
<compilation debug="true">
3
<expressionBuilders>
4
<add expressionPrefix="Color" type="CustomExpressionBuilder.ColorExpressionBuilder"/>
5
</expressionBuilders>
6
</compilation>
7
<!--添加颜色属性-->
8
<anonymousIdentification enabled="true"/>
9
<profile>
10
<properties>
11
<add allowAnonymous="true" name="BackgroundColor" defaultValue="Yellow" type="string"/>
12
</properties>
13
</profile>
3.页面调用绑定表达式
![](/Images/OutliningIndicators/ContractedBlock.gif)
调用
1
<asp:TextBox ID="txtContent" runat="server" BackColor="<%$ Color:BackgroundColor %>" />
这里用到一些codedom的内容,具体MSDN有.另外属性不一定存放在profile中,实现方式可多种.
比较简单的小应用,学习中....
1.定义个绑定表达式的类型,继承于ExpressionBuilder
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/None.gif)
14
![](/Images/OutliningIndicators/None.gif)
15
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
23
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
67
![](/Images/OutliningIndicators/InBlock.gif)
68
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
69
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/InBlock.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/InBlock.gif)
81
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
86
![](/Images/OutliningIndicators/InBlock.gif)
87
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
88
![](/Images/OutliningIndicators/InBlock.gif)
89
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
90
![](/Images/OutliningIndicators/InBlock.gif)
91
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
92
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
93
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
94
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
95
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
96
![](/Images/OutliningIndicators/None.gif)
2.配置web.config
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/None.gif)
3.页面调用绑定表达式
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![](/Images/OutliningIndicators/None.gif)
这里用到一些codedom的内容,具体MSDN有.另外属性不一定存放在profile中,实现方式可多种.
比较简单的小应用,学习中....