简体   繁体   English

如何使用Razor / Webforms语法访问属性内的属性

[英]How can I access a property within an attribute using Razor/Webforms syntax

I'm trying to set a maximum length limit in a text area using a regex validator (if there is a better way to do this, please let me know). 我正在尝试使用正则表达式验证器在文本区域中设置最大长度限制(如果有更好的方法,请告诉我)。 This is an ASP.Net webforms user control. 这是一个ASP.Net Webforms用户控件。 The current code I have is 我当前的代码是

<asp:Panel ID="labeledTextBox" runat="server">
    <asp:Label ID="label" CssClass="label" ClientIDMode="Static" runat="server"/>
    <asp:TextBox ID="textBox" CssClass="labeledTextBox" TextMode="MultiLine" ClientIDMode="Static" runat="server"/>
    <asp:Label ID="textBoxLengthLabel" CssClass="textBoxLengthLabel" ClientIDMode="Static" runat="server">Maximum <%= MaxLength %> characters</asp:Label>
    <asp:RegularExpressionValidator ID="textboxLengthValidator" Display="Dynamic" ControlToValidate="textBox" ValidationExpression="^[\s\S]{0,<%= MaxLength %>}$" ErrorMessage="Test Message" ClientIDMode="Static" runat="server" />
</asp:Panel>

MaxLength is defined in the codebehind file as MaxLength在代码隐藏文件中定义为

public int MaxLength { get; set; }

The value I set shows up in the textBoxLengthLabel properly as, for example, Maximum 500 characters . 我设置的值正确显示在textBoxLengthLabel中,例如, Maximum 500 characters

However, it is taken literally in the attribute for the validator. 但是,它实际上是在验证器的属性中使用的。 The markup on the client side is 客户端的标记是

<SPAN id=textboxLengthValidator
      style="COLOR: red; DISPLAY: inline"
      controltovalidate="textBox"
      errormessage="Test Message"
      validationexpression="^[\s\S]{0,<%= MaxLength %>}$"
      isvalid="false"
      display="Dynamic">Test Message</SPAN>

How can I use the property in the code-behind file in the attribute? 如何在属性的代码隐藏文件中使用该属性?

在页面加载或提交后的代码中,您只需添加属性

textBox.Attributes.Add("maxlength",  MaxLength.ToString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 c# razor 语法从下拉列表的选定项中访问数据属性 - How to access the data attribute from a dropdownlists' selected item using c# razor syntax 我如何使用javascript var作为键来混合剃刀语法和javascript语法以更改哈希表值 - How can i mix razor syntax and javascript syntax to change a hashtable value using a javascript var as key 如何使用模型中的自定义属性来更改剃刀视图模型中的输入元素名称属性值? - How can I change the input element name attribute value in a razor view model using a custom attribute in a model? 如何在c#中访问属性属性中的类变量? - How can I access to class variable in property attribute in c#? 如何访问初始化程序中设置的对象属性? - How can I access an object property set within an initializer? 在JavaScript条件陈述中使用Razor语法 - Using Razor Syntax within JavaScript Conditional Statment 我如何使用剃须刀评估是否为空 - How can I evaluate if empty using razor 如何在模板化剃刀代理中包含更多逻辑? - How can I include more logic within a templated razor delegate? 如何将 Object 属性作为参数传递给 Razor 组件 - How Can I Pass an Object Property to a Razor Component as a Parameter 我如何访问此属性? - How can I access this property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM