【问题标题】:Can't use codeblock in control property无法在控件属性中使用代码块
【发布时间】:2019-05-02 19:04:36
【问题描述】:

抽象问题:我试图将文本字段中的用户输入限制为与列的数据库长度相同。所以我想在 html 输入上设置一个 maxlength 属性,并且 maxlength 应该与数据库中允许的最大长度相同。我可以在整个前端对这些常量进行硬编码,但我正在尝试动态设置该值。

问题:telerik RadComboBox 不接受 asp 代码块来设置属性。例外情况如下:

解析器错误

说明:解析服务此请求所需的资源时出错。请查看以下特定的解析错误详细信息并适当修改您的源文件。

解析器错误消息:无法从其字符串表示 '(x => x.Title) %>' 创建类型为 'System.Int32' 的对象'MaxLength' 属性。

我创建了一个新的最小 asp.net 项目来复制问题。 default.aspx 源代码(后面没有 .cs 代码):

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TelerikCodeBlock._Default" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Import namespace="TelerikCodeBlock" %>
<%@ Import namespace="TelerikCodeBlock.DataModel" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

    <telerik:RadComboBox ID="txtboxTitle" runat="server" MaxLength="<% Utility.GetColumnMaxLength<Portfolio>(x => x.Title) %>" >
        </telerik:RadComboBox>

</asp:Content>

Utility 类已最小化为以下内容

namespace TelerikCodeBlock
{
    public class Utility
    {
        public static int GetColumnMaxLength<T>(Expression<Func<T, object>> property)
        {
            // looks at Entity Framework metadata in real project ...
            return 3;
        }
    }
}

数据模型的样子

namespace TelerikCodeBlock.DataModel
{
    public class Portfolio
    {
        public int Id { get; set; }
        public string Title { get; set; }
    }
}

【问题讨论】:

    标签: c# asp.net telerik


    【解决方案1】:

    可能的解决方法:使用ASP.NET Expressions(&lt;%$ ... %&gt; 代码块)构建执行代码的通用表达式,如here 所述。

    添加对Microsoft.CodeDom.Providers.DotNetCompilerPlatform的引用。

    在某处定义以下内容:

    using System;
    using System.CodeDom;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Compilation;
    using System.Web.UI;
    
    namespace TelerikCodeBlock
    {
        [ExpressionPrefix("Code")]
        public class CodeExpressionBuilder : ExpressionBuilder
        {
            public override CodeExpression GetCodeExpression(BoundPropertyEntry entry,
               object parsedData, ExpressionBuilderContext context)
            {
                return new CodeSnippetExpression(entry.Expression);
            }
        }
    }
    

    并在 web.config 中注册

    <configuration>
      <system.web>
        <compilation debug="true" targetFramework="4.5" >
          <expressionBuilders>
            <add expressionPrefix="Code" type="TelerikCodeBlock.CodeExpressionBuilder"/>
          </expressionBuilders>
        </compilation>
    
        ...
    

    现在更改asp控件以使用表达式代码块:

    <telerik:RadComboBox ID="txtboxTitle" runat="server" MaxLength="<%$ Code: Utility.GetColumnMaxLength<Portfolio>(x => x.Title) %>" >
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      相关资源
      最近更新 更多