代码改变世界

asp.net 单值绑定,使用<%$ %> 方式

2008-03-24 09:36  齐天大圣  阅读(228)  评论(0编辑  收藏  举报
  • <%#$ XXX %>  可以引用webConfig.xml中预定义的字段或者已注册的类的表达式
  • 示例:
    Default.aspx

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        
    <title>Untitled Page</title>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
        
    <asp:Label ID="appSetting1" runat="server" Text='<%$ AppSettings:key1 %>' />
        
    <asp:Label ID="expressionBuilder1" runat="server" Text='<%$ Code:DateTime.Now %>' />
        
    </div>
        
    </form>
    </body>
    </html>

    web.config
        <appSettings>
        
    <add key="key1" value="Hello World!"/>    
        
    </appSettings>

    <compilation debug="true">
    <expressionBuilders>
    <add expressionPrefix="Code" type="expressionBuilder"/>
    </expressionBuilders>
    </compilation>

    appCode中的expressionBuilder类

     1using System;
     2using System.Data;
     3using System.Configuration;
     4using System.Linq;
     5using System.Web;
     6using System.Web.Security;
     7using System.Web.UI;
     8using System.Web.Compilation;
     9using System.Web.UI.HtmlControls;
    10using System.Web.UI.WebControls;
    11using System.Web.UI.WebControls.WebParts;
    12using System.Xml.Linq;
    13using System.CodeDom;
    14/// <summary>
    15/// Summary description for expressionBuilder
    16/// </summary>
    17/// 

    18[ExpressionPrefix("Code")]
    19public class expressionBuilder: ExpressionBuilder
    20{
    21    public expressionBuilder()
    22    {
    23        //
    24        // TODO: Add constructor logic here
    25        //
    26        
    27    }

    28    public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
    29    {
    30        return new CodeSnippetExpression(entry.Expression);
    31    }

    32}

    33