2016-06-15 15 views
1

我以前使用BotDetect在asp WEBFORM上使用驗證碼,但是現在當我嘗試使用NuGet將BotDetect添加到另一個Asp WEBFORM項目時,我遇到了'Object Reference not set to an調試時對象的實例錯誤。」對象引用未設置爲對象的實例。「當使用BotDetect Captcha

我無法加載我的頁面,也沒有得到任何休息發生。它在嘗試執行我的表單頁面的應用程序洞察請求GET時似乎失敗。然後在System.Web.dll中拋出一個毫無異常的異常

但是,這就是說,當我嘗試運行他的調試器時,我仍遇到'未設置爲對象實例的對象引用'錯誤,但它引用BotDetect.Web.UI.WebFormsCaptcha.OnInit(EventArgs e)

我已經刪除了BotDetect包,重新添加了程序集,試圖從工作項目中添加程序集,並試圖從頭開始重新創建項目,以查看我是否也許錯誤配置了一些東西。似乎沒有任何工作。

我目前從NuGet軟件包(版本4.0.1)獲得BotDetectBotDetect.Web.MVC,我已經檢查過以確保我的.net 4.5版本與我的.net 4.5項目一起使用。

我檢查了我的Packages.config,我可以看到:

<package id="Captcha" version="4.0.1" targetFramework="net452" /> 

下面是我的代碼引用BotDetect驗證碼。這是一個MasterPage網頁表單,但Captcha駐留在內容佔位符中,因爲我們在呈現主內容後與它交互。

我只是不確定我還能做些什麼來解決這個問題,有什麼建議嗎?

ASP窗體頁

<asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolder2" runat="server"> 
<fieldset> 
    <legend>ASP.NET WebForm CAPTCHA Validation</legend> 
    <p class="prompt"> 
    <label for="CaptchaCodeTextBox">Retype the characters from the picture:</label></p> 
    <BotDetect:WebFormsCaptcha runat="server" ID="ExampleCaptcha" 
    UserInputControlID="CaptchaCodeTextBox" /> 
    <div class="validationDiv"> 
    <asp:TextBox ID="CaptchaCodeTextBox" runat="server"></asp:TextBox> 
    <asp:Button ID="ValidateCaptchaButton" runat="server" /> 
    <asp:Label ID="CaptchaCorrectLabel" runat="server" CssClass="correct"></asp:Label> 
    <asp:Label ID="CaptchaIncorrectLabel" runat="server" CssClass="incorrect"></asp:Label> 
    </div> 
</fieldset> 

ASP窗體頁後面的代碼

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender 
    ' initial page setup 
    If Not IsPostBack Then 
     ' set control text 
     ValidateCaptchaButton.Text = "Validate" 
     CaptchaCorrectLabel.Text = "Correct!" 
     CaptchaIncorrectLabel.Text = "Incorrect!" 

     ' these messages are shown only after validation 
     CaptchaCorrectLabel.Visible = False 
     CaptchaIncorrectLabel.Visible = False 
    End If 

    If IsPostBack Then 
     ' validate the Captcha to check we're not dealing with a bot 
     Dim isHuman As Boolean 
     isHuman = ExampleCaptcha.Validate() 
     If isHuman Then 
      CaptchaCorrectLabel.Visible = True 
      CaptchaIncorrectLabel.Visible = False 
     Else 
      CaptchaCorrectLabel.Visible = False 
      CaptchaIncorrectLabel.Visible = True 
     End If 
    End If 
End Sub 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 

     Dim HF As HiddenField = DirectCast(Page.Master.FindControl("HF1"), HiddenField) 

     If Not IsPostBack Then 
      SetInitialRow() 
     ElseIf HF.Value = "AddNewRow" Then 
      AddNewRowToGrid() 
     Else 
      btnSubmit_Click() 
     End If 
Done: 
    End Sub 
+0

不要喲明白,異常意味着什麼呢? –

+1

參照變量和函數不可用,是的。但是,對於這個錯誤,我不確定。正如我上面所說的,它不會加載項目,所以我認爲它與某個程序集中沒有設置的引用有關。在檢查程序集BotDetect時,我可以看到BotDetect.Web.UI.WebFormsCaptcha.OnInit(EventArgs e)確實存在。這顯然是頁面加載之前的事情,儘管我知道這一點。 – expenguin

回答

1

所以,因爲我已經談過BotDetect的開發者和事實證明,這實際上是他們當前版本的一個錯誤。

您目前無法使用BotDetect(v4.0.1)在內容頁面(帶有母版頁的Web表單)中設置UserInputControlID屬性。在下一個版本中會有一個補丁。

與此同時,根據我發佈的代碼,您可以在代碼隱藏(VB)中以編程方式設置它。

後面的代碼(在page_prerender)

ExampleCaptcha.UserInputID = CaptchaCodeTextBox.ClientID

您可能還需要更換

isHuman = ExampleCaptcha.Validate() 

隨着

isHuman = ExampleCaptcha.Validate(CaptchaCodeTextBox.Text.Trim()) 

內容的Web表格

<BotDetect:WebFormsCaptcha runat="server" ID="ExampleCaptcha" /> 
相關問題