2

updatepanel 内に 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 が失われました。これに対する簡単な解決策はありますか?Web でいくつかの記事を調べましたが、それらは複雑で構造化されていないようです。updatepanel の内容を変更する必要があるので、これを念頭に置いてください。

ご協力ありがとうございました。

4

3 に答える 3

4

これは、1 つの updatepanel だけでうまく動作するようになりました。

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

            }

        }
于 2012-05-25T18:50:57.383 に答える
2

これが私が試して働いている答えです:

ASP.Net、Recaptcha、UpdatePanels、および Partial PostBacks の消失: 完全に修正

基本的には、非表示の 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...  
于 2012-12-28T05:38:01.463 に答える
0

これを試して。

    <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);
    }
}
于 2009-12-16T15:30:40.080 に答える