在UpdatePanel中没有更新Asp.Net NoCaptcha

时间:2015-06-13 14:22:16

标签: asp.net

如果我不添加UpdatePanel,NoCaptcha图像工作正常,但如果我添加UpdatePanel,它不会在回发时更新。这是我的代码:

 <asp:UpdatePanel ID="UpdatePanelTriggers" runat="server" UpdateMode="Conditional">
        <ContentTemplate>

            --NoCaptcha
            <div id="captcha" runat="server" class="login_re_captcha_hidden">
                <div class="g-recaptcha" data-sitekey="Key">
                </div>
            </div>

        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="btnLogin" />
        </Triggers>
    </asp:UpdatePanel>
    <asp:UpdateProgress ID="uprCampaigns" runat="server" DisplayAfter="0" AssociatedUpdatePanelID="UpdatePanelTriggers">
        <ProgressTemplate>
            <div class="loadingIconBackground">
                <div style="height: 85px; top: 0px; width: 100%">
                </div>
                <div style="min-height: 100%; background-color: White">
                </div>
            </div>
            <div class="loadingIconDiv">
                <img src="images/PleaseWait_Small.gif" />
            </div>
        </ProgressTemplate>
    </asp:UpdateProgress>

这是我的服务器端代码。我使用此代码验证用户的答案:

private bool ValidateUserReCaptchaResponse()
        {
            bool validated = true;

            if (Request["g-recaptcha-response"] != null && Request["g-recaptcha-response"] != "")
            {
                string sCatchaResponse = Request["g-recaptcha-response"];
                string sSecret = "key"; //put in webconfig
                string sIPAddress = Request.ServerVariables["REMOTE_ADDR"].ToString();
                sIPAddress = "x.2x.x.18";

                // Next create a WebClient instance to call the web api and get the result... 
                System.Net.WebClient wc = new System.Net.WebClient();
                string sRequest = String.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}&remoteip={2}", sSecret, sCatchaResponse, sIPAddress);
                string sResponse = wc.DownloadString(sRequest);

                //add  <system.net>
                //<defaultProxy useDefaultCredentials="true" />
                //</system.net> for DownloadString(sRequest) in webconfig

                // The result comes back as a JSON object. I've created a simple GoogleResponse object to hold this information 
                // and am using the JavaScriptSerializer to deserialize the response accordingly... 
                System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                GoogleResponse response = serializer.Deserialize<GoogleResponse>(sResponse);

                // Now we can just check if the call succeeded or failed and take the necessary action 
                if (!response.success)
                {
                    // just output an error on the front end here 
                    validated = false;
                }
            }
            else
            {
                // ok if you're here, you basically didn't check that bot question on the form. 
                // Tell the user to do so and resubmit the form
                validated = false;
            }

            return validated;
        }

请帮帮忙?

1 个答案:

答案 0 :(得分:0)

我认为你的意思是当你单击btnLogin时,它不能在PostBack中工作吗? 因为这是您定义的唯一触发器。也许尝试设置EventName。

至少应该在你这样做时起作用:

UpdateMode="Always". 

<强>更新 现在我想我明白会发生什么。如果我是正确的,你还包括一个调用默认卸载的render方法的javascript文件。因此,如果我是正确的,UpdatePanel会更新并仍然需要您致电:

grecaptcha.render();

或指定参数。真的可以看出它们是否是强制性的。默认情况下,它应查找它找到的第一个标记并使用属性集,因此不希望参数是必需的。 请参阅文档https://developers.google.com/recaptcha/docs/display#render_param

要么在初始加载时禁用调用render / onload,所以你可以随时执行:

$(function() {
        grecaptcha.render();
}

或者您添加了使用UpdatePanel调用客户端代码以进行更新的机制。 可以在https://msdn.microsoft.com/en-us/library/bb398976(v=vs.140).aspx

上找到解释

应该有效的示例(但是我在MacBook上按键输入,因此无法验证/测试它)将此添加到页面的标记中:

<script type="text/javascript">
    function PartialPostBackFinished(sender, args) {
         //Do whatever also needs to be triggered, esp when you want it to be applied on panels_updated only to avoid registering events multiple times

         grecaptcha.render()
    }

    if (Sys != undefined) {
        //There's a ScriptManager on the page
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_endRequest(PartialPostBackFinished);
    }
</script>