Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从codebehind设置asp:CustomValidator文本_C#_Asp.net_Customvalidator_Requiredfieldvalidator - Fatal编程技术网

C# 从codebehind设置asp:CustomValidator文本

C# 从codebehind设置asp:CustomValidator文本,c#,asp.net,customvalidator,requiredfieldvalidator,C#,Asp.net,Customvalidator,Requiredfieldvalidator,我有用户ID和密码字段,这两个字段都可以与RequiredFieldValidator一起使用。就在下面,我放置了一个CustomValidator,我正试图在密码不匹配时从codebehind进行类似的验证 <asp:RequiredFieldValidator runat="server" ID="PWRequired" ControlToValidate="PasswordTextbox" Display="None" ErrorMessage=

我有用户ID和密码字段,这两个字段都可以与RequiredFieldValidator一起使用。就在下面,我放置了一个CustomValidator,我正试图在密码不匹配时从codebehind进行类似的验证

<asp:RequiredFieldValidator runat="server" ID="PWRequired"   
    ControlToValidate="PasswordTextbox"  
    Display="None"  
    ErrorMessage="<b>Required Field Missing</b><br />A password is required." />
<asp:CustomValidator runat="server" ID="PWMatch"
    ControlToValidate="PasswordTextbox"  
    Display="None"
    ErrorMessage="<b>Password is Invalid</b><br />Please enter a valid password." />
<AjaxControl:ValidatorCalloutExtender 
    runat="Server"
    ID="PWValidationExtender"
    TargetControlID="PWRequired" 
    Width="185px"
    WarningIconImageUrl="/Images/warning.gif" />
<asp:TextBox ID="PasswordTextbox" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>

(希望格式正确。我从来没有用过“4空格”这个词

然后在代码隐藏中,当PWs不匹配时,我尝试以下方法:

CustomValidator myValidator = (CustomValidator)this.FindControl("PWMatch");
myValidator.Text = "<b>Password is Invalid</b><br />Please enter a valid password."; 
ValidatorCalloutExtender myValExtender = (ValidatorCalloutExtender)this.FindControl("PWValidationExtender");
myValExtender.ID = "PWValidationExtender";
myValExtender.TargetControlID="PWMatch";
myValExtender.WarningIconImageUrl = "/Images/warning.gif";
myValExtender.Width = new Unit(250);
CustomValidator myValidator=(CustomValidator)this.FindControl(“PWMatch”);
myValidator.Text=“密码无效
请输入有效密码。”; ValidatorCalloutExtender myValExtender=(ValidatorCalloutExtender)this.FindControl(“PWValidationXtender”); myValExtender.ID=“PWValidationXtender”; myValExtender.TargetControlID=“PWMatch”; myValExtender.WarningIconImageUrl=“/Images/warning.gif”; myValExtender.Width=新单元(250);
我希望我错过的是一些简单而愚蠢的事情。我花了一整天的时间试图弄明白这一点

提前感谢你的帮助

=========================================================================


谢谢freefaller。我试过了,得到的是“没有乐趣”。所以,我从一个稍微不同的方向来做

我已改为CompareValidator,如下所示:(再次希望标记有效)



它给了我一条消息,但总是计算为“false”,因为GetPassword方法没有被调用。如果我继续努力,我想我可以让它工作,但是如果你看到一个快速的解决方案,我会洗耳恭听。再次感谢!!

而不是
。Text
使用
。ErrorMessage

所以改变

myValidator.Text = "<b>Password is Invalid</b><br />Please enter a valid password."; 
然后在代码隐藏中,需要有处理程序

protected void cusPassword_ServerValidate(object sender, ServerValidateEventArgs args)
{
  args.IsValid = (txtPassword.Text == GetPassword());
}

谢谢freefaller,我试过了,但没有得到快乐。“所以,我是从一个稍微不同的方向来的。我在上面的原始帖子中添加了新的方法。@DJGrey-您的自定义验证器上有任何处理程序吗?服务器端或客户端?freefaller-很抱歉我的无知。这里是新手程序员。您所说的“处理程序”是什么意思我怀疑您的意思是捕获并显示验证器返回的内容未调用CodeBehind中的方法。我怀疑如果我可以从该方法中获取字符串返回值,我的CompareEvaluator将正常工作。这是一种有效的方法吗?处理程序是在系统要验证时调用的一段代码。在调用
CustomValidator
时需要它,因为它不知道您使用了什么另外,除非您处理的是基于模板的控件(如
Repeater
GridView
),否则您不能在asp控件的属性上使用代码块(即,您不能使用
)。这就是
GetPassword()
未被调用-如果查看发送到浏览器的HTML,您可能会发现
GetPassword()的静态字符串
在thereLOL!!我想做的是:我有服务器端用户ID/密码验证,但从未对失败时返回的错误消息感到满意。这太难看了。昨天,我偶然发现ValidationXtender弹出了一个吸引人的气球。它与RequiredFieldValidator配合得很好,所以我想这么做无效密码、无效用户ID和有效但尚未批准登录的用户也是如此。当出现这些错误情况时,我希望我可以像RequiredFieldValidator一样弹出一个小气球。这有帮助吗?
myValidator.ErrorMessage = "<b>Password is Invalid</b><br />Please enter a valid password."; 
Password: <asp:TextBox runat="server" id="txtPassword"/>
<asp:RequiredFieldValidator runat="server" id="reqPassword"
   ControlToValidate="txtPassword" ErrorMessage="Provide Password" />
<asp:CustomValidator runat="server" id="cusPassword"
   ControlToValidator="txtPassword" ErrorMessage="Incorrect Password"
   OnServerValidate="cusPassword_ServerValidate"/>
protected void cusPassword_ServerValidate(object sender, ServerValidateEventArgs args)
{
  args.IsValid = (txtPassword.Text == GetPassword());
}