UpdateBnel中的Recaptcha在PostBack期间消失

时间:2016-04-28 08:24:03

标签: c# asp.net updatepanel postback recaptcha

我使用的是Google ReCaptcha V2,它位于更新面板内。 IF验证失败ReCaptcha在回发时消失。 我读过类似的主题,但我还没有找到解决问题的答案。

请帮忙!

我的ASPX代码:

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

我在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 个答案:

答案 0 :(得分:0)

在正文后使用此脚本

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

        $('.g-recaptcha').each(function (index, obj) {
            grecaptcha.render(obj, { 'sitekey': 'yoursitekey' });
        });
    } 
</script>