我目前在注册表中使用MS Captcha。如果表单在一分钟内提交,它的工作完美。但有时,在填写表单后,用户会搜索要上传的文档,当他们最终提交表单时,会收到服务器错误,如下所示:
[NullReferenceException:对象引用未设置为的实例 object。] MSCaptcha.CaptchaControl.ValidateCaptcha(String userEntry) 438
点击提交按钮,我拨打ValidateCaptcha
,如下所示:
Captcha1.ValidateCaptcha(txtCaptcha.Text.Trim());
有人可以帮我处理这个例外吗?提前谢谢。
答案 0 :(得分:1)
已经看到,如果你没有设置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
}
}