Recaptcha in Updatepanel disappears during PostBac

2019-06-14 11:47发布

I am using Google ReCaptcha V2 and it is inside an updatepanel. IF validation failed ReCaptcha disappears on postback. I read similar topics but I have not yet found an answer that solves my problem.

Please help!

My ASPX code :

<%@ Register Assembly="GoogleReCaptcha" Namespace="GoogleReCaptcha" TagPrefix="cc1" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
 <form id="formRegister" runat="server">
    <asp:ScriptManager ID="ScriptManagerRegister" EnablePartialRendering="true" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanelRegister" hildrenAsTriggers="false" UpdateMode="Conditional" runat="server">
        <ContentTemplate>

               <asp:Panel ID="Panel1" runat="server"></asp:Panel>
               <asp:Button ID="ButtonRegister" runat="server" Text="Registrera" CssClass="btn btn-primary btn-md" /><br />
               <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

            </asp:Panel>
        </ContentTemplate>
    </asp:UpdatePanel>
 </form>
</asp:Content>

My code behind C#

GoogleReCaptcha.GoogleReCaptcha ctrlGoogleReCaptcha = new GoogleReCaptcha.GoogleReCaptcha();

    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        ctrlGoogleReCaptcha.PublicKey = "My Public Key";
        ctrlGoogleReCaptcha.PrivateKey = "My Private Key";
        this.Panel1.Controls.Add(ctrlGoogleReCaptcha);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ButtonRegister.Click += new EventHandler(ButtonRegister_Click);
    }

    protected void ButtonRegister_Click(object sender, EventArgs e)
    {
        if (ctrlGoogleReCaptcha.Validate())
        {
            //submit form
            Label1.Text = "Success";
        }
        else
        {
            Label1.Text = "Captcha Failed!! Please try again!!";
        }
    }

1条回答
Melony?
2楼-- · 2019-06-14 12:46

use this script after body

<body>
<script language="javascript" type="text/javascript"> 
    function pageLoad() 
    { 

        $('.g-recaptcha').each(function (index, obj) {
            grecaptcha.render(obj, { 'sitekey': 'yoursitekey' });
        });
    } 
</script>
查看更多
登录 后发表回答