使用C#3秒后刷新页面 [英] refresh page after 3 seconds using c#
问题描述
我希望我的代码在显示成功标签3秒钟后刷新页面。
I want my code to refresh the page once its shown the success label for 3 seconds.
如何在c#代码中执行此操作?
How can i do this in c# code?
我有以下内容:
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();
}
imCaptcha.ImageUrl = "captcha.ashx?d=" + DateTime.Now.Ticks;
}
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("name@company.co.uk");
mail.Bcc.Add("test@test.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 = "";
txtVerify.Text = "";
}
}
因此,在 cessPH.Visible = true;
之后,我需要数3秒,然后刷新页面。然后,这将清除表单数据,并且用户还将有3秒钟的时间查看该消息,以表明该消息已成功完成。
So after sucessPH.Visible = true;
i need to count 3 seconds and then refresh the page. This will then clear the form data and the user will also get 3 seconds to see the message to say the message was successfull.
有什么想法吗? $ b
Any ideas?
推荐答案
由于这是客户端问题,而不是服务器方面的问题,因此需要使用JavaScript来实现。请记住,到页面发送到客户端时,服务器端代码的所有 all 都已完成并释放。在这种情况下,尝试使用服务器端代码执行此操作会导致很多不必要的复杂性。
Since this is a client-side concern and not a server-side concern, you'll want to do this with JavaScript. Keep in mind that by the time the page is sent to the client all of the server-side code has completed and disposed. Trying to do this with server-side code will result in a lot of unnecessary complexity in this case.
在JavaScript中,您可以像这样延迟地重新加载页面:
In JavaScript you can reload the page with a delay with something like this:
setTimeout("window.location.reload()", 3000);
这篇关于使用C#3秒后刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!