【问题标题】:ASP.net: "Object reference not set to an instance of an object." for an existing objectASP.net:“对象引用未设置为对象的实例。”对于现有对象
【发布时间】:2013-04-12 20:11:35
【问题描述】:

我在 asp.net 4.0 网络应用程序中使用 MSCaptcha 控件。我已将提供的 .dll.xml 文件放在 Bin 文件夹中,并在页面顶部添加了<%@ Register Assembly="MSCaptcha" Namespace="MSCaptcha" TagPrefix="cc1" %>。 这是我在页面中使用它的方式:

<cc1:CaptchaControl ID="Captcha1" runat="server"
                 CaptchaBackgroundNoise="Low" CaptchaLength="4"
                 CaptchaHeight="60" CaptchaWidth="300"
                 CaptchaLineNoise="None" CaptchaMinTimeout="1"
                 CaptchaMaxTimeout="240" FontColor = "#529E00" />

在后面的代码中我这样使用它:

If Page.FindControl("Captcha1").ToString = "MSCaptcha.CaptchaControl" Then
    Captcha1.ValidateCaptcha(webCaptcha.Text.Trim())

    If Captcha1.UserValidated Then
        addComment()
    Else
        webWrongCaptcha.Text = "Captcha is wrong!"
    End If
Else
    Response.Redirect("contact.aspx?c=e")
End If

但是如果用户在CaptchaMaxTimeout 之后单击按钮,我会遇到此错误:

Line 7:              Captcha1.ValidateCaptcha(webCaptcha.Text.Trim())

我该如何解决?

【问题讨论】:

  • null 是什么:Captcha1webCaptchawebCaptcha.Text
  • 不是webCaptcha 也不是webCaptcha.Text,但不确定Captcha1
  • 因为我尝试了Captcha1.ValidateCaptcha("foo") 并遇到了同样的错误。
  • 针对另一个类似查询stackoverflow.com/questions/17232819/…提供的答案

标签: asp.net


【解决方案1】:

在调用它的属性之前,将您的逻辑包装在一个确定验证码是否为空的语句中。如果是,则通过向客户端发送消息来处理错误,让他们知道他们已经超过了超时并且他们需要重试。

【讨论】:

  • 我认为If Page.FindControl("Captcha1").ToString = "MSCaptcha.CaptchaControl" Then 会这样做,是吗?
  • @Maysam 你不应该使用If CType(Page.FindControl("Captcha1"), CaptchaControl) Is Not Nothing Then之类的东西吗?那个字符串转换的东西看起来可能有点脆弱。
  • 以上选项都不适合我,我不得不在我的CaptchaControl1.ValidateCaptcha 周围写一个Try - Catch
【解决方案2】:

将 EnableViewState="False" 添加到代码中:

【讨论】:

    猜你喜欢
    • 2013-10-28
    • 1970-01-01
    • 2020-04-13
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多