创建自定义的绑定表达式 简单应用
创建自定义的绑定表达式 简单应用
在dotnet中的绑定表达式中,#算是大家用得最多的,另外还有几个专用的绑定表达式比如,appSettings,ConnectionStrings,Resources等,用起来都很方便.dotnet里提供了一个ExpressionBuilder的类,继承该类可实现自定义的绑定表达式.以下是定义一个绑定颜色的绑定表达式.该颜色的值保存在profile中,对于一些服务器控件,比如textbox的颜色绑定,如果直接绑定个字符串,那么会出现运行错误的,应为textobx的BackColor只认Color类型的值,而如果在profile中存放个color类型的值,那么写到数据库里还得来个转换,操作起来麻烦.于是才有了弄个颜色绑定表达式的想法.(也许还有更好的解决方案).具体实现如下:
1.定义个绑定表达式的类型,继承于ExpressionBuilder
2.配置web.config
3.页面调用绑定表达式
这里用到一些codedom的内容,具体MSDN有.另外属性不一定存放在profile中,实现方式可多种.
比较简单的小应用,学习中....
1.定义个绑定表达式的类型,继承于ExpressionBuilder
ColorExpressionBuilder
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Web.Compilation;
11using System.CodeDom;
12using System.Drawing;
13
14namespace CustomExpressionBuilder
15{
16 /**//// <summary>
17 /// 用于绑定颜色的自定义绑定表达式,继承于ExpressionBuilder
18 /// </summary>
19 [ExpressionEditor(typeof(ColorExpressionBuilder))]
20 [ExpressionPrefix("Color")]
21 public class ColorExpressionBuilder : ExpressionBuilder
22 {
23 /**//// <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 {
32 return expression;
33 }
34
35 /**//// <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 {
44 // 以下内容用codedom构建一个方法表达式,调用GetColorValue
45 // 参数值
46 CodePrimitiveExpression prim = new CodePrimitiveExpression(parsedData);
47 // 参数
48 CodeExpression[] args = new CodeExpression[] { prim };
49 // 引用对象
50 CodeTypeReferenceExpression refType = new CodeTypeReferenceExpression(GetType());
51 // 返回方法表达式
52 return new CodeMethodInvokeExpression(refType, "GetColorValue", args);
53
54 }
55
56 /**//// <summary>
57 /// 从Profile中获取自定义的颜色的值,在计算表达式的值时用
58 /// </summary>
59 /// <param name="colorPropertyName"></param>
60 /// <returns></returns>
61 public static Color GetColorValue(string colorPropertyName)
62 {
63 string colorName = (string)HttpContext.Current.Profile.GetPropertyValue(colorPropertyName);
64
65 return Color.FromName(colorName);
66 }
67
68 以下用于非编译页面#region 以下用于非编译页面
69 /**//// <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 {
79 string propertyName = (string)target;
80 return GetColorValue(propertyName);
81 }
82
83 /**//// <summary>
84 /// 支持非编译页面的绑定调用
85 /// </summary>
86 public override bool SupportsEvaluate
87 {
88 get
89 {
90 return true;
91 }
92 }
93 #endregion
94 }
95}
96
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Web.Compilation;
11using System.CodeDom;
12using System.Drawing;
13
14namespace CustomExpressionBuilder
15{
16 /**//// <summary>
17 /// 用于绑定颜色的自定义绑定表达式,继承于ExpressionBuilder
18 /// </summary>
19 [ExpressionEditor(typeof(ColorExpressionBuilder))]
20 [ExpressionPrefix("Color")]
21 public class ColorExpressionBuilder : ExpressionBuilder
22 {
23 /**//// <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 {
32 return expression;
33 }
34
35 /**//// <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 {
44 // 以下内容用codedom构建一个方法表达式,调用GetColorValue
45 // 参数值
46 CodePrimitiveExpression prim = new CodePrimitiveExpression(parsedData);
47 // 参数
48 CodeExpression[] args = new CodeExpression[] { prim };
49 // 引用对象
50 CodeTypeReferenceExpression refType = new CodeTypeReferenceExpression(GetType());
51 // 返回方法表达式
52 return new CodeMethodInvokeExpression(refType, "GetColorValue", args);
53
54 }
55
56 /**//// <summary>
57 /// 从Profile中获取自定义的颜色的值,在计算表达式的值时用
58 /// </summary>
59 /// <param name="colorPropertyName"></param>
60 /// <returns></returns>
61 public static Color GetColorValue(string colorPropertyName)
62 {
63 string colorName = (string)HttpContext.Current.Profile.GetPropertyValue(colorPropertyName);
64
65 return Color.FromName(colorName);
66 }
67
68 以下用于非编译页面#region 以下用于非编译页面
69 /**//// <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 {
79 string propertyName = (string)target;
80 return GetColorValue(propertyName);
81 }
82
83 /**//// <summary>
84 /// 支持非编译页面的绑定调用
85 /// </summary>
86 public override bool SupportsEvaluate
87 {
88 get
89 {
90 return true;
91 }
92 }
93 #endregion
94 }
95}
96
2.配置web.config
注册表达式
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>
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.页面调用绑定表达式
调用
1<asp:TextBox ID="txtContent" runat="server" BackColor="<%$ Color:BackgroundColor %>" />
1<asp:TextBox ID="txtContent" runat="server" BackColor="<%$ Color:BackgroundColor %>" />
这里用到一些codedom的内容,具体MSDN有.另外属性不一定存放在profile中,实现方式可多种.
比较简单的小应用,学习中....