Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 验证码刷新问题?_C#_Asp.net_Captcha_Contact Form - Fatal编程技术网

C# 验证码刷新问题?

C# 验证码刷新问题?,c#,asp.net,captcha,contact-form,C#,Asp.net,Captcha,Contact Form,嗨,我的验证码有问题,当用户得到错误的验证码时,它不想刷新 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 {

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

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("test@hotmail.co.uk");
        mail.CC.Add("another_test@hotmail.co.uk");
        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 = "";
    }

    }
使用系统;
使用System.Collections.Generic;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Net.Mail;
使用System.IO;
公共部分类验证码联系人:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
//图像验证
如果(!IsPostBack)
{
SetVerificationText();
}
}
public void setVerificationContext()
{
Random ran=新的Random();
int no=ran.Next();
会话[“验证码”]=编号ToString();
}
受保护的无效CAPTCHAValidate(对象源,ServerValidateEventArgs参数)
{
如果(会话[“验证码”]!=null)
{
if(txtVerify.Text!=会话[“验证码”].ToString())
{
SetVerificationText();
args.IsValid=false;
返回;
}
}
其他的
{
SetVerificationText();
args.IsValid=false;
返回;
}
}
受保护的无效btnSave\u单击(对象发送方,事件参数e)
{
如果(!Page.IsValid)
{
返回;
}
SetVerificationText();
//保存内容
MailMessage mail=新的MailMessage();
mail.From=新邮件地址(EmailTB.Text);
mail.To.Add(“test@hotmail.co.uk");
mail.CC.Add(“另一个_test@hotmail.co.uk");
mail.Subject=“网络报价”;
mail.IsBodyHtml=true;
mail.Body=“名字:”+FNameTB.Text+“
”; mail.Body+=“Email:+EmailTB.Text+”
”; mail.Body+=“电话:”+TelephoneTB.Text+“
”; mail.Body+=“查询:”+QueryDD.Text+“
”; mail.Body+=“Comments:”+CommentsTB.Text+“
”; SmtpClient smtp=新SmtpClient(); smtp.Host=“localhost”; smtp.发送(邮件); successph.Visible=true; } 受保护的无效重置(对象s、事件参数e) { FNameTB.Text=“”; QueryDD.Text=“”; EmailTB.Text=“”; 电话号码tb.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代码。

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

如果不想完全禁用缓存,可以通过(例如当前时间戳)到映像路径来避免这种行为。浏览器每次都会重新加载图像,因为它有一个新的url

http://localhost/captcha.ashx?d=1315497031
编辑:要做到这一点,只需在页面加载中添加这一行即可:

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

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

如果不想完全禁用缓存,可以通过(例如当前时间戳)到映像路径来避免这种行为。浏览器每次都会重新加载图像,因为它有一个新的url

http://localhost/captcha.ashx?d=1315497031
编辑:要做到这一点,只需在页面加载中添加这一行即可:

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

我添加了aspx页面,您能帮我吗?我看不到您的代码中有任何会阻止刷新的主要问题。您确定浏览器没有缓存图像吗?如果是这样,将不会调用泛型处理程序,并且您将再次获得相同的映像,即使您在会话中有一个新值。检查您的浏览器是否真的加载了图片(例如Firebug),这是唯一一个出现问题的IE 9,chrome和FF工作正常…:我添加了aspx页面,你能帮我吗?我看不到你的代码中有任何会阻止刷新的主要问题。您确定浏览器没有缓存图像吗?如果是这样,将不会调用泛型处理程序,并且您将再次获得相同的映像,即使您在会话中有一个新值。检查您的浏览器是否真的加载了图片(例如Firebug),这是唯一一个出现问题的IE 9,chrome和FF工作正常…:显示我是否会停止缓存,抱歉,我不明白。正如我上面解释的,每次显示页面时,只需向处理程序路径添加一个新的编号,浏览器不应存储图像。请参阅我的编辑以了解如何执行此操作。字符串dummy=math.rand()imCaptcha.ImageUrl=“captcha.ashx”;我在上面添加了代码。如果对您有效,请将此答案标记为已接受。如果它仍然不起作用,你也许应该发布你的captcha.ashx代码。这是一个解决缓存问题的聪明方法!非常感谢你!我该如何停止缓存,抱歉,我不明白。正如我上面解释的,每次显示页面时,只需向处理程序路径添加一个新的数字,浏览器不应存储图像。请参阅我的编辑以了解如何执行此操作。字符串dummy=math.rand()imCaptcha.ImageUrl=“captcha.ashx”;我在上面添加了代码。如果对您有效,请将此答案标记为已接受。如果它仍然不起作用,你也许应该发布你的captcha.ashx代码。这是一个解决缓存问题的聪明方法!非常感谢你!