Asp.net Recaptcha在回发过程中消失
我在这里看到了一些关于在ASP.net UpdatePanels中使用Recaptcha的其他帖子,但还没有找到解决我问题的答案 这是我的ASPX代码:Asp.net Recaptcha在回发过程中消失,asp.net,updatepanel,recaptcha,Asp.net,Updatepanel,Recaptcha,我在这里看到了一些关于在ASP.net UpdatePanels中使用Recaptcha的其他帖子,但还没有找到解决我问题的答案 这是我的ASPX代码: <asp:UpdatePanel ID="updRecaptcha" runat="server" UpdateMode="Always"> <ContentTemplate> <recaptcha:RecaptchaControl ID="btnrecaptcha" runat="server" Theme
<asp:UpdatePanel ID="updRecaptcha" runat="server" UpdateMode="Always">
<ContentTemplate>
<recaptcha:RecaptchaControl ID="btnrecaptcha" runat="server" Theme="clean" PrivateKey"<%$ Resources: General, CaptchaPrivateKey %>" PublicKey="<%$ Resources: General, CaptchaPublicKey %>" />
<asp:Label ID="recaptchaerror" runat="server" style="color: Red;"/>
</ContentTemplate>
</asp:UpdatePanel>
这两条警报语句都会触发,但Recaptcha仍不会加载。如果需要使用javascript重新加载Recaptcha控件,请尝试以下操作:
protected void Button1_Click(object sender, EventArgs e)
{
btnrecaptcha.Validate();
if(IsValid && updRecaptcha.IsValid}
{
//Some logic here
}
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "CaptchaReload", "Recaptcha.reload();", true);
}
忘记脚本管理器吧。你所需要的只是页面中的这个脚本。相应地更改您的标识符:
<script type="text/javascript">
// RECAPTURE CODE FOR RELOADING AFTER INCORRECT ENTRY
if (typeof Sys != 'undefined') {
var requestManager = Sys.WebForms.PageRequestManager.getInstance();
requestManager.add_endRequest(function(sender, args) {
$('<div id="recaptcha_widget_div"/>').appendTo('#recaptcha_widget_div2');
if (typeof Recaptcha != 'undefined') {
Recaptcha.create(recaptcha_key, "recaptcha_widget_div",
{
theme: "red",
callback: Recaptcha.focus_response_field
});
}
});
}
//输入错误后重新加载的重新捕获代码
if(系统类型!=“未定义”){
var requestManager=Sys.WebForms.PageRequestManager.getInstance();
requestManager.add_endRequest(函数(发送方,参数){
$('').appendTo('#recaptcha_widget_div2');
如果(类型为Recaptcha!=“未定义”){
创建(Recaptcha_键,“Recaptcha_小部件_div”,
{
主题:“红色”,
回调:Recaptcha.focus\u response\u字段
});
}
});
}
接受的解决方案对我不起作用。以下是我已经尝试并正在努力的答案: 基本上,它包括创建一个隐藏的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...
protectedvoid btnSubmit\u单击(对象发送方,事件参数e)
{
recomptcha.Validate();
如果(!Page.IsValid | |!recaptcha.IsValid)
{
pbTarget.Visible=true;
ScriptManager.RegisterClientScriptBlock(
雷帕查,
recaptcha.GetType(),
“雷帕查”,
“Recaptcha.\u初始选项(RecaptchaOptions);”
+“如果(RecaptchaOptions&&\“custom\”==RecaptchaOptions.theme)”
+ "{"
+“如果(RecaptchaOptions.custom_主题_小部件)”
+ " {"
+“Recaptcha.widget=Recaptcha.$(RecaptchaOptions.custom_theme_widget);”
+“Recaptcha.challenge_callback();”
+ " }"
+“}否则{”
+“如果(Recaptcha.widget==null | |!document.getElementById(\“Recaptcha\u widget\u div\”)”
+ " {"
+“jQuery(\”\“\”+pbTarget.ClientID+“\”).html(“”);”
+“Recaptcha.widget=Recaptcha.$(\“Recaptcha\u widget\u div\”)
+ " }"
+“Recaptcha.reload();”
+“Recaptcha.challenge_callback();”
+ "}",
真的
);
返回;
}
其他的
{
//正常页面处理在这里。。。
令人惊讶的是,我不需要应用RegisterClientScriptBlock调用就可以工作。
我只是使用:
UpdateMode=“始终”
对于updatepanel和服务器端,我调用:
updatePanel.Update();
这也会阻止验证码显示新的挑战。我的答案基本上与“用户”相同(有效!),但可以帮助任何使用VB的人。 我会在那里发表评论,但我有zippo rep。。。 (希望这不是一个太多的违规行为,并希望它能帮助别人) 我将这部分内容移植到VB.NET:
pbTarget.Visible = True
Dim _sb As New StringBuilder
With _sb
.Append("Recaptcha._init_options(RecaptchaOptions);")
.Append("if ( RecaptchaOptions && ""custom"" == RecaptchaOptions.theme )")
.Append("{")
.Append(" if ( RecaptchaOptions.custom_theme_widget )")
.Append(" {")
.Append(" Recaptcha.widget = Recaptcha.$(RecaptchaOptions.custom_theme_widget);")
.Append(" Recaptcha.challenge_callback();")
.Append(" }")
.Append("} else {")
.Append(" if ( Recaptcha.widget == null || !document.getElementById(""recaptcha_widget_div"") )")
.Append(" {")
.Append(" jQuery(""#")
.Append(pbTarget.ClientID)
.Append(""").html('<div id=""recaptcha_widget_div"" style=""display:none""></div>');")
.Append(" Recaptcha.widget = Recaptcha.$(""recaptcha_widget_div"");")
.Append(" }")
.Append(" Recaptcha.reload();")
.Append(" Recaptcha.challenge_callback();")
.Append("}")
End With
ScriptManager.RegisterClientScriptBlock(recaptcha, recaptcha.[GetType](), "recaptcha", _sb.ToString, True)
pbTarget.Visible=True
将某人定为新的架线工
与某人
.Append(“Recaptcha.\u init\u options(recaptchaooptions);”)
.Append(“如果(RecaptchaOptions&&“custom”==RecaptchaOptions.theme)”)
.Append(“{”)
.Append(“if(RecaptchaOptions.custom_theme_widget)”)
.Append(“{”)
.Append(“Recaptcha.widget=Recaptcha.$(RecaptchaOptions.custom_theme_widget);”)
.Append(“Recaptcha.challenge_callback();”)
.Append(“}”)
.Append(“}else{”)
.Append(“if(Recaptcha.widget==null | | |!document.getElementById”(“Recaptcha_widget_div”)))
.Append(“{”)
.Append(“jQuery(“#”)
.Append(pbTarget.ClientID)
.Append(“”).html(“”);“”)
.Append(“Recaptcha.widget=Recaptcha.$(“Recaptcha_widget_div”);)
.Append(“}”)
.Append(“Recaptcha.reload();”)
.Append(“Recaptcha.challenge_callback();”)
.Append(“}”)
以
ScriptManager.RegisterClientScriptBlock(recaptcha,recaptcha.[GetType](),“recaptcha”,\u sb.ToString,True)
如果您使用的是Recaptcha 2.0,这是您的Javascript
<script type="text/javascript">
function CaptchaReload() {
Recaptcha.create("yourpublicKey", 'yourRecaptchadiv', {
theme: 'white',
callback: grecaptcha.reset()
});
}
我能够使用2.1.0.0版本的reCAPTCHA library for.NET,通过webforms、脚本管理器和更新面板使其正常工作 首先,在更新面板外的.aspx页面中,我正在加载Google reCAPTCHA库:
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit"
async defer></script>
你有一个指向它的实时演示的链接吗?我怀疑在发回后没有重新初始化javascript。不幸的是,没有一个我可以共享的。我在Google Chrome的控制台选项卡中看到它显示了以下错误:无法设置属性“innerHTML”的值。我正在做类似的事情:ScriptManager.RegisterClientScriptBlock(Page,Page.GetType(),“InvalidRecaptcha”,“alert('Recaptcha Failed');Recaptcha.reload();alert('Recaptcha reloated');”,true);两个警报语句都会触发,但Recaptcha仍然没有加载。我不得不放弃Recaptcha,使用我自己的“证明你是人类”问题。我仍然想知道如何解决这个问题。谢谢!!对我来说非常有效。:)在使用UpdatePanel时对我来说非常有效。我将其嵌入到包装表单的ascx控件中,包括UpdatePanel中的recaptcha。我得到的控制台充满了错误“recaptcha not defined”(这是带有大写字母“R”的recaptcha)按照您的说明,我收到了以下错误消息:“当UpdateMode设置为Conditional时,只能对ID为'sifCaptchaControl'的UpdatePanel调用Update方法。”您不能将UpdatePanel的UpdateMode设置为“Always”,然后手动调用.Update()功能。这是另一个。谢谢Vijay。你的回答让我朝着正确的方向前进
<div class="g-recaptcha" data-sitekey="yourPublicKey"></div>
protected void txtEmail_TextChanged(object sender, EventArgs e)
{
if (txtEmail.Text != string.Empty)
{
ValidateEmail();
ScriptManager.RegisterStartupScript(this, this.GetType(), "CaptchaReload", "$.getScript(\"https://www.google.com/recaptcha/api.js\", function () {});", true);
}
}
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit"
async defer></script>
<div runat="server" id="pbTarget" visible="false"></div>
<cc1:Recaptcha ID="recaptcha" runat="server" Theme="Clean" />
Private Sub cmdSubmit_Click(sender As Object, e As EventArgs) Handles cmdSubmit.Click
pbTarget.Visible = True
ScriptManager.RegisterStartupScript(
UpdatePanel1,
UpdatePanel1.GetType(),
"loadCaptcha",
"grecaptcha.render('" & pbTarget.ClientID & "', {'sitekey': 'YourKey' });",
End Sub