【问题标题】:Problem with recaptchacontrol inside updatepanelupdatepanel 中的 recaptchacontrol 问题
【发布时间】:2009-10-26 00:54:28
【问题描述】:

我有一个 recaptchavalidator,它位于更新面板中:

        <asp:updatepanel runat=server id=updatepanel1>

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

我相信你们中的一些人可以猜到会发生什么。对于那些以前没有经历过这种情况的人,recaptchacontrol 消失了!如果 recaptchacontrol 返回错误验证,我尝试重定向到同一页面,但这会导致复杂的代码隐藏和丢失 veiwstate。 有一个简单的解决方案吗?我查看了网络上的一些文章,但它们似乎很复杂并且结构不合理。我需要更改更新面板的内容,所以请记住这一点。

感谢您的帮助。

【问题讨论】:

标签: c# asp.net-ajax recaptcha


【解决方案1】:

我只用一个更新面板就可以很好地工作。

 <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>

关键是将您的更新面板设置为围绕您的发布按钮的条件,以便您手动调用更新以从服务器端重新加载 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();

            }

        }

【讨论】:

  • 只是一个评论,我发现recaptcha.reload()需要大写R,即Recaptcha.reload()
【解决方案2】:

这是我尝试过并且正在工作的答案:

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

基本上它涉及创建一个隐藏的 div 并使用 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...  

【讨论】:

    【解决方案3】:

    试试这个。

        <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);
        }
    }
    

    【讨论】:

    • 当我这样做时,我得到:Uncaught TypeError: Cannot set property 'innerHTML' of null recaptcha.js:110 Recaptcha._set_challenge recaptcha.js:110 Recaptcha.finish_reload recaptcha.js:110 (anonymous function)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多