验证码刷新问题?

发布于 2024-12-03 11:09:03 字数 6578 浏览 1 评论 0原文

您好,我的验证码有问题,当用户得到错误的代码时,它不想刷新。

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.IO;

public partial class CAPTCHA_Contact : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        //ImageVerification
        if (!IsPostBack)
        {
            SetVerificationText();
        }
    }

    public void SetVerificationText()
    {
        Random ran = new Random();

        int no = ran.Next();

        Session["Captcha"] = no.ToString();
    }

    protected void CAPTCHAValidate(object source, ServerValidateEventArgs args)
    {
        if (Session["Captcha"] != null)
        {
            if (txtVerify.Text != Session["Captcha"].ToString())
            {
                SetVerificationText();

                args.IsValid = false;

                return;

            }
        }
        else
        {
            SetVerificationText();

            args.IsValid = false;

            return;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid)
        {
            return;
        }

        SetVerificationText();

        //Save the content
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress(EmailTB.Text);
        mail.To.Add("[email protected]");
        mail.CC.Add("[email protected]");
        mail.Subject = "Web Quote";
        mail.IsBodyHtml = true;
        mail.Body = "First Name: " + FNameTB.Text + "<br />";
        mail.Body += "Email: " + EmailTB.Text + "<br />";
        mail.Body += "Telephone: " + TelephoneTB.Text + "<br />";
        mail.Body += "Query: " + QueryDD.Text + "<br />";
        mail.Body += "Comments: " + CommentsTB.Text + "<br />";


        SmtpClient smtp = new SmtpClient();
        smtp.Host = "localhost";
        smtp.Send(mail);

        sucessPH.Visible = true;
    }

    protected void Reset(object s, EventArgs e)
    {
        FNameTB.Text = "";
        QueryDD.Text = "";
        EmailTB.Text = "";
        TelephoneTB.Text = "";
        CommentsTB.Text = "";
    }

    }

ASPX:

<asp:PlaceHolder ID="formPH" runat="server" Visible="true"> 
<form id="form1" runat="server">
<table id="contact" cellspacing="7">
<tr>
<td class="label"></td>
<td><asp:TextBox ID="FNameTB" runat="server" width="350px" title="Your Name" />
<asp:RequiredFieldValidator ID="rfvFName" runat="server" ControlToValidate="FNameTB" ErrorMessage="First Name is required" Display="Dynamic" />
</td>
</tr>
<tr>
<td class="label"></td>
<td><asp:TextBox ID="EmailTB" runat="server" width="350px" title="Your Email" />
<asp:RequiredFieldValidator ID="rfvEmail" runat="server" ControlToValidate="EmailTB" ErrorMessage="Email is required" Display="Dynamic" />
</td>
</tr>
<tr>
<td class="label"></td>
<td><asp:TextBox ID="TelephoneTB" runat="server" width="350px" title="Your Telephone Number" />
<asp:RequiredFieldValidator ID="rfvTelephone" runat="server" ControlToValidate="TelephoneTB" ErrorMessage="Telephone number is required" Display="Dynamic" />
</td>
</tr>
<tr>
<td class="label"></td>
<td><asp:DropDownList ID="QueryDD" runat="server" width="355px" CssClass="QueryDD">
        <asp:ListItem Selected="True" style="color: #999999">Select a Service</asp:ListItem>
        <asp:ListItem>test</asp:ListItem>
        <asp:ListItem>Cleaning</asp:ListItem>
        <asp:ListItem>test</asp:ListItem>
        <asp:ListItem>Bar</asp:ListItem>
        <asp:ListItem>Cleaning</asp:ListItem>
        <asp:ListItem>Cleaning</asp:ListItem>
        <asp:ListItem>Wash Cleaning</asp:ListItem>
        <asp:ListItem>Cleaning</asp:ListItem>
        <asp:ListItem>Supplies</asp:ListItem>
   </asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvLName" runat="server" ControlToValidate="QueryDD" ErrorMessage="Query is required"  Display="Dynamic" />
</td>
</tr>
<tr>

<tr>
<td class="label"></td>
<td><asp:TextBox ID="CommentsTB" title="Comments" runat="server" TextMode="MultiLine" width="350px" />
<asp:RequiredFieldValidator ID="rfvComments" runat="server" ControlToValidate="CommentsTB" ErrorMessage="Comments are required" Display="Dynamic" />
</td>
</tr>
<tr>
<td></td>
<td><asp:Label ID="lblCaptchaCode" runat="server" Text=""></asp:Label><asp:Image ID="imCaptcha" ImageUrl="captcha.ashx" runat="server" width="120px" /> <asp:Button ID="btnNewCode" runat=server Text="GENERATE"></asp:Button></td>
</tr>
<tr>
<td class="label"></td>
<td><asp:TextBox ID="txtVerify" runat="server" width="350px" title="Enter the above code here"> </asp:TextBox>
<asp:RequiredFieldValidator ID="rfvCaptcha" runat="server" ControlToValidate="txtVerify" ErrorMessage="Required" Display="Dynamic" />
</td>
</tr>
<tr>
<td></td>
<td class = "buttons" colspan="2"><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSave_Click"/><asp:Button ID="btnReset" runat="server" Text="Reset" OnClick="Reset"/></td>
</tr>
<tr>
<td></td>
<td><asp:PlaceHolder ID="sucessPH" runat="server" Visible="false"><p class="submission">Thank you for your submission.</p></asp:PlaceHolder></td>
</tr>
<tr>
<td></td>
<td><asp:CustomValidator ID="CustomValidator2" runat="server" ControlToValidate="txtVerify" ErrorMessage="Wrong verification code, please refresh the page"
            OnServerValidate="CAPTCHAValidate"></asp:CustomValidator></td>
</tr>
</table>


</form>
</asp:PlaceHolder> 

我需要能够将验证码存储在标签中并创建一个仅重新加载验证码的按钮。

有什么想法吗?

我已经走到这一步了,只是需要一些帮助。 编辑:添加了aspx代码。

Hi i have issues with my captcha code, it doesnt want to refresh when the user gets the code the wrong.

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.IO;

public partial class CAPTCHA_Contact : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        //ImageVerification
        if (!IsPostBack)
        {
            SetVerificationText();
        }
    }

    public void SetVerificationText()
    {
        Random ran = new Random();

        int no = ran.Next();

        Session["Captcha"] = no.ToString();
    }

    protected void CAPTCHAValidate(object source, ServerValidateEventArgs args)
    {
        if (Session["Captcha"] != null)
        {
            if (txtVerify.Text != Session["Captcha"].ToString())
            {
                SetVerificationText();

                args.IsValid = false;

                return;

            }
        }
        else
        {
            SetVerificationText();

            args.IsValid = false;

            return;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid)
        {
            return;
        }

        SetVerificationText();

        //Save the content
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress(EmailTB.Text);
        mail.To.Add("[email protected]");
        mail.CC.Add("[email protected]");
        mail.Subject = "Web Quote";
        mail.IsBodyHtml = true;
        mail.Body = "First Name: " + FNameTB.Text + "<br />";
        mail.Body += "Email: " + EmailTB.Text + "<br />";
        mail.Body += "Telephone: " + TelephoneTB.Text + "<br />";
        mail.Body += "Query: " + QueryDD.Text + "<br />";
        mail.Body += "Comments: " + CommentsTB.Text + "<br />";


        SmtpClient smtp = new SmtpClient();
        smtp.Host = "localhost";
        smtp.Send(mail);

        sucessPH.Visible = true;
    }

    protected void Reset(object s, EventArgs e)
    {
        FNameTB.Text = "";
        QueryDD.Text = "";
        EmailTB.Text = "";
        TelephoneTB.Text = "";
        CommentsTB.Text = "";
    }

    }

ASPX:

<asp:PlaceHolder ID="formPH" runat="server" Visible="true"> 
<form id="form1" runat="server">
<table id="contact" cellspacing="7">
<tr>
<td class="label"></td>
<td><asp:TextBox ID="FNameTB" runat="server" width="350px" title="Your Name" />
<asp:RequiredFieldValidator ID="rfvFName" runat="server" ControlToValidate="FNameTB" ErrorMessage="First Name is required" Display="Dynamic" />
</td>
</tr>
<tr>
<td class="label"></td>
<td><asp:TextBox ID="EmailTB" runat="server" width="350px" title="Your Email" />
<asp:RequiredFieldValidator ID="rfvEmail" runat="server" ControlToValidate="EmailTB" ErrorMessage="Email is required" Display="Dynamic" />
</td>
</tr>
<tr>
<td class="label"></td>
<td><asp:TextBox ID="TelephoneTB" runat="server" width="350px" title="Your Telephone Number" />
<asp:RequiredFieldValidator ID="rfvTelephone" runat="server" ControlToValidate="TelephoneTB" ErrorMessage="Telephone number is required" Display="Dynamic" />
</td>
</tr>
<tr>
<td class="label"></td>
<td><asp:DropDownList ID="QueryDD" runat="server" width="355px" CssClass="QueryDD">
        <asp:ListItem Selected="True" style="color: #999999">Select a Service</asp:ListItem>
        <asp:ListItem>test</asp:ListItem>
        <asp:ListItem>Cleaning</asp:ListItem>
        <asp:ListItem>test</asp:ListItem>
        <asp:ListItem>Bar</asp:ListItem>
        <asp:ListItem>Cleaning</asp:ListItem>
        <asp:ListItem>Cleaning</asp:ListItem>
        <asp:ListItem>Wash Cleaning</asp:ListItem>
        <asp:ListItem>Cleaning</asp:ListItem>
        <asp:ListItem>Supplies</asp:ListItem>
   </asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvLName" runat="server" ControlToValidate="QueryDD" ErrorMessage="Query is required"  Display="Dynamic" />
</td>
</tr>
<tr>

<tr>
<td class="label"></td>
<td><asp:TextBox ID="CommentsTB" title="Comments" runat="server" TextMode="MultiLine" width="350px" />
<asp:RequiredFieldValidator ID="rfvComments" runat="server" ControlToValidate="CommentsTB" ErrorMessage="Comments are required" Display="Dynamic" />
</td>
</tr>
<tr>
<td></td>
<td><asp:Label ID="lblCaptchaCode" runat="server" Text=""></asp:Label><asp:Image ID="imCaptcha" ImageUrl="captcha.ashx" runat="server" width="120px" /> <asp:Button ID="btnNewCode" runat=server Text="GENERATE"></asp:Button></td>
</tr>
<tr>
<td class="label"></td>
<td><asp:TextBox ID="txtVerify" runat="server" width="350px" title="Enter the above code here"> </asp:TextBox>
<asp:RequiredFieldValidator ID="rfvCaptcha" runat="server" ControlToValidate="txtVerify" ErrorMessage="Required" Display="Dynamic" />
</td>
</tr>
<tr>
<td></td>
<td class = "buttons" colspan="2"><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSave_Click"/><asp:Button ID="btnReset" runat="server" Text="Reset" OnClick="Reset"/></td>
</tr>
<tr>
<td></td>
<td><asp:PlaceHolder ID="sucessPH" runat="server" Visible="false"><p class="submission">Thank you for your submission.</p></asp:PlaceHolder></td>
</tr>
<tr>
<td></td>
<td><asp:CustomValidator ID="CustomValidator2" runat="server" ControlToValidate="txtVerify" ErrorMessage="Wrong verification code, please refresh the page"
            OnServerValidate="CAPTCHAValidate"></asp:CustomValidator></td>
</tr>
</table>


</form>
</asp:PlaceHolder> 

I need to be able to store the captcha in a label and create a button that reloads just the captcha..

Any ideas?

I've came this far, just need some help please.
EDIT: added the aspx code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网名女生简单气质 2024-12-10 11:09:03

您似乎遇到了缓存问题。浏览器不知道图像已更改,因此使用较早存储的版本。

如果您不想完全禁用缓存,可以通过添加虚拟值<来规避此行为/a> (例如当前时间戳)到图像路径。浏览器每次都会重新加载图像,因为它有一个新的 URL。

http://localhost/captcha.ashx?d=1315497031

编辑:为此,只需在 Page_Load 中添加以下行:

imCaptcha.ImageUrl = "captcha.ashx?d=" + DateTime.Now.Ticks;

It seems like you have a caching problem. The browser does not know that the image has changed and therefore uses an earlier stored version.

If you don't want to disable caching completely, you can circumvent this behaviour by adding a dummy value (for example the current timestamp) to the image path. The browser will reload the image then everytime, because it has a new url.

http://localhost/captcha.ashx?d=1315497031

EDIT: To do this just add this line in your Page_Load:

imCaptcha.ImageUrl = "captcha.ashx?d=" + DateTime.Now.Ticks;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文