本文介绍了在运行时设置 RegularExpressionValidator ValidationExpression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在运行时在我的 aspx 控件中设置了一个 RegularExpressionValidator,如下所示
I am setting a RegularExpressionValidator at runtime in my aspx control as per below
<asp:RegularExpressionValidator ID="revValue" runat="server" ControlToValidate="txtZipCode"
ValidationExpression='<%=this.SettingManager.GetSettingValue("ZipCodeValidationExpression")%>'
ErrorMessage="Invalid Zip Code." Display="Dynamic" />
在页面上,如果我输入了无效的邮政编码,我会收到无效的邮政编码"消息,但是,如果我随后输入了有效的邮政编码,则没有任何反应,并且消息仍然是无效的邮政编码".
On the page, if I enter an invalid zipcode I do get the message "Invalid Zip Code", however, if I then enter a valid zip code nothing happens and the message remains "Invalid Zip Code".
如果我按照下面手动设置表达式
If I manually set the expression as per below
<asp:RegularExpressionValidator ID="revValue" runat="server" ControlToValidate="txtZipCode"
ValidationExpression="^(d{5}-d{4}|d{5}|d{9})$|^([a-zA-Z]d[a-zA-Z] d[a-zA-Z]d)$"
ErrorMessage="Invalid Zip Code." Display="Dynamic" />
它工作正常.我错过了什么?
It works fine. What am I missing?
推荐答案
代码隐藏"中的示例代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class Default3 : System.Web.UI.Page
{
public static string GetErrorMessage()
{
return "Your Error Message";
}
public static string GetValidationExpression()
{
return @"d+";
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
qw.ErrorMessage = GetErrorMessage();
qw.ValidationExpression = GetValidationExpression();
}
}
}
ASPX 页面中的示例代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txt1" runat="server">
</asp:TextBox>
<asp:RegularExpressionValidator ID="qw" runat="server" ControlToValidate="txt1" Display="Dynamic"></asp:RegularExpressionValidator>
<asp:Button ID="ed" runat="server" Text="ed" />
</form>
</body>
</html>
这篇关于在运行时设置 RegularExpressionValidator ValidationExpression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!