我目前在注册表中使用 MS Captcha。如果表格在一分钟内提交,效果就很好。但有时,在填写表单后,用户然后搜索要上传的文档,当他们最终提交表单时,他们会收到如下服务器错误:
[NullReferenceException:未将对象引用设置为实例
对象。] MSCaptcha.CaptchaControl.ValidateCaptcha(String userEntry)
+438
单击提交按钮后,我调用ValidateCaptcha
如下:
Captcha1.ValidateCaptcha(txtCaptcha.Text.Trim());
有人可以帮我处理这个异常吗?提前致谢。
可以看出,如果您没有设置 ErrorInputTooFast 和 ErrorInputTooSlow 消息,则当 CaptchaMaxTimeout 时间段过去时,代码会抛出“NullReferenceException”。
我还设置了以下属性,以便在没有 NullReferenceException 的情况下正常工作
ErrorInputTooFast="图像文本输入得太快。"
ErrorInputTooSlow="图像文本输入速度太慢。"
我的实现如下,我将 CaptchaMaxTimeout 设置为 20 秒,看看这个问题是否仍然存在。
<uc:CaptchaControl ID="CaptchaUserControl" runat="server" Height="50px" ValidationGroup="PageValidationGroup"
CustomValidatorErrorMessage="The text you entered did not match up with the image provided"
Width="180px" CaptchaLength="5" FontColor="#000000" BackColor="#e6db55" NoiseColor="#26557f"
CaptchaLineNoise="None" CaptchaFontWarping="Low" ImageTag="border='1'" CaptchaBackgroundNoise="Medium"
ErrorInputTooFast="Image text was typed too quickly. " ErrorInputTooSlow="Image text was typed too slowly."
CaptchaMaxTimeout="20" CaptchaMinTimeout="2" EnableViewState="False" />
<asp:TextBox ID="CapthaTextBox" runat="server" MaxLength="10" Width="180px" AutoCompleteType="Disabled"/>
代码隐藏
private void AppendValidationErrorMessage( string message)
{
var cv = new CustomValidator();
cv.IsValid = false;
cv.ErrorMessage = message;
cv.ValidationGroup = "PageValidationGroup";
this.Page.Validators.Add(cv);
}
protected void SubmitButtonClick(object sender, EventArgs e)
{
try
{
this.CaptchaUserControl.ValidateCaptcha(CapthaTextBox.Text.GetTrimValue());
if (!this.CaptchaUserControl.UserValidated)
{
this.AppendValidationErrorMessage(this.CaptchaUserControl.CustomValidatorErrorMessage);
}
}
catch (Exception)
{
this.AppendValidationErrorMessage(
"Captcha expired please please reload the page.Note: please copy the data before refreshing data");
}
this.CapthaTextBox.Text = string.Empty;
if (this.Page.IsValid) //&& this.CaptchaUserControl.UserValidated
{
//do something
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)