2009-10-26 78 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...