locked
ExpressionBuilder question RRS feed

  • Question

  • User1774885328 posted

    Hi there,

    I am new to expression builder. I followed the example in https://docs.microsoft.com/en-us/dotnet/api/system.web.compilation.expressionbuilder?view=netframework-4.8

    The expression builder works when I run my web app it shows "Hello, world!".

    But in designer it shows "Expression:Hello, world!" instead of just "Hello, world!"

    I have looked in the documentation that I only need to add the ExpressionEditorAttributes in my class which I did.

    This is the VB code I used for the expression builder.

    Imports System
    Imports System.CodeDom
    Imports System.Web.UI
    Imports System.ComponentModel
    Imports System.Web.Compilation
    Imports System.Web.UI.Design
    ' Apply ExpressionEditorAttributes to allow the 
    ' expression to appear in the designer.
    <ExpressionPrefix("MyCustomExpression"),
     ExpressionEditor("MyCustomExpressionEditor")>
    Public Class MyExpressionBuilder
        Inherits ExpressionBuilder
    
        ' Create a method that will return the result 
        ' set for the expression argument.
        Public Shared Function GetEvalData(ByVal expression As String, ByVal target As Type, ByVal entry As String) As Object
            Return expression
        End Function
    
        Public Overrides Function EvaluateExpression(ByVal target As Object, ByVal entry As BoundPropertyEntry, ByVal parsedData As Object, ByVal context As ExpressionBuilderContext) As Object
            Return MyExpressionBuilder.GetEvalData(entry.Expression, target.GetType, entry.Name)
        End Function
    
        Public Overrides Function GetCodeExpression(ByVal entry As BoundPropertyEntry, ByVal parsedData As Object, ByVal context As ExpressionBuilderContext) As CodeExpression
            Dim type1 As Type = entry.DeclaringType
            Dim descriptor1 As PropertyDescriptor = TypeDescriptor.GetProperties(type1)(entry.PropertyInfo.Name)
            Dim expressionArray1() As CodeExpression = New CodeExpression((3) - 1) {}
            expressionArray1(0) = New CodePrimitiveExpression(entry.Expression.Trim)
            expressionArray1(1) = New CodeTypeOfExpression(type1)
            expressionArray1(2) = New CodePrimitiveExpression(entry.Name)
            Return New CodeCastExpression(descriptor1.PropertyType, New CodeMethodInvokeExpression(New CodeTypeReferenceExpression(MyBase.GetType), "GetEvalData", expressionArray1))
        End Function
    
        Public Overrides ReadOnly Property SupportsEvaluate As Boolean
            Get
                Return True
            End Get
        End Property
    End Class

    In my Web.config compilation

    <expressionBuilders>
       <add expressionPrefix="MyExpression" type="MyExpressionBuilder, App_Code"/>
    </expressionBuilders>
    



    Really appreciate if there is anyone who can help with this issue. There's not much resources out there that discusses about this.

    Saturday, September 7, 2019 12:44 PM

Answers

  • User665608656 posted

    Hi keenhon,

    But in designer it shows "Expression:Hello, world!" instead of just "Hello, world!"

    According to your description, the design page itself is not exactly the same as the aspx running page.

    Based on the link you provided, in your front-end code : 

    <asp:Label ID="Label1" runat="server"   
    Text="<%$ MyCustomExpression:Hello, world! %>" />  

    "MyCustomExpression:" is added to the page itself, so on the design page, the screen you see contains Expression, but when the page runs, through the rebuild project, the running page will show you what you need.

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 9, 2019 8:09 AM

All replies

  • User665608656 posted

    Hi keenhon,

    But in designer it shows "Expression:Hello, world!" instead of just "Hello, world!"

    According to your description, the design page itself is not exactly the same as the aspx running page.

    Based on the link you provided, in your front-end code : 

    <asp:Label ID="Label1" runat="server"   
    Text="<%$ MyCustomExpression:Hello, world! %>" />  

    "MyCustomExpression:" is added to the page itself, so on the design page, the screen you see contains Expression, but when the page runs, through the rebuild project, the running page will show you what you need.

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 9, 2019 8:09 AM
  • User1774885328 posted

    Thanks

    Monday, September 9, 2019 8:15 AM