[英]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.