2009-10-26 76 views
2

recaptchacontrol我有一個recaptchavalidator,這是一個UpdatePanel內:問題裏面的UpdatePanel

 <asp:updatepanel runat=server id=updatepanel1> 

    <cc1:recaptchacontrol runat=server publickey=.. privatekey=.. id=recaptchavalidator1/> 
<asp:button runat=server id=button1/> 
</updatepanel> 

我確信你們中的一些可以猜到發生了什麼。對於那些以前沒有經歷過這些的人來說,recaptchacontrol會消失!如果recaptchacontrol返回錯誤驗證,我嘗試重定向到同一頁面,但這導致了複雜的代碼隱藏,並導致了權限的喪失。 有沒有簡單的解決方案呢?我瀏覽過網絡上的一些文章,但它們看起來很複雜,結構也不完整。我需要更改updatepanel的內容,因此請記住這一點。

謝謝你的幫助。

+0

這是對我工作。可能不是最好的解決方案,但值得一試:http://lakhlaniprashant.blogspot.com/2009/05/recaptchanet-control-in-updatepanel.html – User 2012-12-06 04:09:30

+0

劃痕,最後評論..它不是一個完整的解決方案。 – User 2012-12-28 03:49:44

回答

0

試試這個。

<asp:UpdatePanel ID="ContactUpdatePanel" runat="server"> 
     <ContentTemplate> 
      <p> 
       <label>Name:</label> 
       <asp:TextBox ID="txtName" runat="server" 
         CssClass="textbox"> 
       </asp:TextBox> 
      </p> 
      <p> 
       <label>Address</label> 
       <asp:TextBox ID="txtAddress" runat="server" 
         CssClass="textbox" 
         Height="50px" 
         TextMode="MultiLine"> 
       </asp:TextBox> 
      </p> 
      <p> 
       <recaptcha:RecaptchaControl ID="recaptcha" runat="server" 
           PublicKey="public key" 
           PrivateKey="private key" 
           Theme="white" /> 
      </p> 
      <p> 
       <asp:UpdatePanel ID="UpdatePanel2" runat="server" 
         ChildrenAsTriggers="false" 
         UpdateMode="Conditional"> 
        <ContentTemplate> 
         <asp:Label ID="ErrorLabel" runat="server" 
           EnableViewState="false" 
           ForeColor="Red" /> 
        </ContentTemplate> 
       </asp:UpdatePanel> 
       <p> 
       </p> 
       <p> 
        <asp:Button ID="SubmitButton" runat="server" 
         onclick="SubmitButton_Click" Text="Submit" /> 
      </p> 
     </ContentTemplate> 
    </asp:UpdatePanel> 

代碼隱藏

protected void SubmitButton_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     this.recaptcha.Validate(); 
     if (recaptcha.IsValid) 
     { 
      //valid form. post it 
     } 
     else 
     { 
      ErrorLabel.Text = "Invalid Captcha. Please re-enter the words."; 
      ScriptManager.RegisterClientScriptBlock(
       this.Page, 
       this.Page.GetType(), 
       "mykey", 
       "Recaptcha.reload();", 
       true); 
      UpdatePanel2.Update(); 
     } 
    } 
    catch (Exception exception) 
    { 
     Elmah.ErrorSignal.FromCurrentContext().Raise(exception); 
    } 
} 
+0

當我這樣做,我得到:'遺漏的類型錯誤:110個 Recaptcha._set_challenge recaptcha.js:110個 Recaptcha.finish_reload recaptcha.js:110 (匿名函數)' – User 2012-12-28 02:16:21

4

我這隻有一個UpdatePanel中很好地工作。

<recaptcha:RecaptchaControl Theme="white" ID="recaptcha" runat="server" PrivateKey="your_pub_key " 
                PublicKey="your_pub_key" /> 

    <asp:ScriptManager ID="ScriptManager1" runat="server"> 
       </asp:ScriptManager> 
       <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
       <ContentTemplate> 

    <asp:Label Visible="false" ID="RecaptchaResult" runat="server" />    
    <asp:Button ID="RecaptchaButton" runat="server" Text="Submit" onclick="btnSubmit_Click" /> 

    </ContentTemplate> 
    </asp:UpdatePanel> 

,關鍵是讓你的UpdatePanel設置條件在你的帖子按鈕,讓你手動調用更新重新加載從服務器端的ReCaptcha控制。然後,在請求reload()後,在面板上調用.update()。

protected void btnSubmit_Click(object sender, EventArgs e) 
     { 
      recaptcha.Validate(); 

      if (recaptcha.IsValid) 
      { 
       RecaptchaResult.Text = "Success"; 

       RecaptchaResult.Text = "You got it!"; 
       RecaptchaResult.ForeColor = System.Drawing.Color.Green; 
       RecaptchaResult.Visible = true; 
       ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "recaptcha", "recaptcha.reload();", true); 
       UpdatePanel1.Update(); 
      } 
      else 
      { 
       RecaptchaResult.Text = this.recaptcha.ErrorMessage; 
       RecaptchaResult.ForeColor = System.Drawing.Color.Red; 
       RecaptchaResult.Visible = true; 
       ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "recaptcha", "recaptcha.reload();", true); 
       UpdatePanel1.Update(); 

      } 

     } 
+2

空recaptcha.js不能設置屬性 '的innerHTML'只是一個評論,我發現''recaptcha.reload()'需要與一個大寫R即'Recaptcha.reload()'。 – Kosmosniks 2012-12-05 21:17:07

+1

該解決方案適用於我。謝謝 – 2014-03-14 18:54:51

2

這裏是我曾嘗試和工作答案:

ASP.Net, disappearing Recaptcha, UpdatePanels and Partial PostBacks: Fixed once and for all

基本上它涉及到創建一個隱藏的股利和使用jquery重新呈現HTML。此外,博客文章還提供了典型解決方案的細微分類(例如,使用帶簡單重新加載的RegisterClientScriptBlock)以及它們爲何失敗。背後

<div runat="server" id="pbTarget" visible="false"></div> 
<recaptcha:RecaptchaControl ID="recaptcha" runat="server" Theme="clean" /> 

代碼:

protected void btnSubmit_Click(object sender, EventArgs e) 
{ 
    recaptcha.Validate(); 
    if (!Page.IsValid || !recaptcha.IsValid) 
    { 
    pbTarget.Visible = true; 
    ScriptManager.RegisterClientScriptBlock( 
     recaptcha, 
     recaptcha.GetType(), 
     "recaptcha", 
     "Recaptcha._init_options(RecaptchaOptions);" 
     + "if (RecaptchaOptions && \"custom\" == RecaptchaOptions.theme)" 
     + "{" 
     + " if (RecaptchaOptions.custom_theme_widget)" 
     + " {" 
     + " Recaptcha.widget = Recaptcha.$(RecaptchaOptions.custom_theme_widget);" 
     + " Recaptcha.challenge_callback();" 
     + " }" 
     + "} else {" 
     + " if (Recaptcha.widget == null || !document.getElementById(\"recaptcha_widget_div\"))" 
     + " {" 
     + " jQuery(\"#" + pbTarget.ClientID + "\").html('<div id=\"recaptcha_widget_div\" style=\"display:none\"></div>');" 
     + " Recaptcha.widget = Recaptcha.$(\"recaptcha_widget_div\");" 
     + " }" 
     + " Recaptcha.reload();" 
     + " Recaptcha.challenge_callback();" 
     + "}", 
     true 
    ); 

    return; 
    } 
    else 
    { 
    //normal page processing here...