我正在尝试使用正则表达式验证器在文本区域中设置最大长度限制(如果有更好的方法,请告诉我)。这是一个ASP.Net webforms用户控件。我目前的代码是
<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
在代码隐藏文件中定义为
public int MaxLength { get; set; }
我设置的值恰好显示在textBoxLengthLabel
中,例如Maximum 500 characters
。
但是,它在字面上用于验证器的属性。客户端的标记是
<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>
如何在属性中使用代码隐藏文件中的属性?
答案 0 :(得分:0)
在页面加载或提交后面的代码中,您只需添加属性
即可textBox.Attributes.Add("maxlength", MaxLength.ToString());