开发者

refresh page after 3 seconds using c#

开发者 https://www.devze.com 2023-04-09 05:22 出处:网络
I want my code to refresh the page once its shown the success label for 3 seconds. How can i do this in c# code?

I want my code to refresh the page once its shown the success label for 3 seconds.

How can i do this in c# code?

i have this following:

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 = "";


    }

    }

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.

Any ideas?


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.

In JavaScript you can reload the page with a delay with something like this:

setTimeout("window.location.reload()", 3000);


Well, if you're using webforms, you could use an update panel, and then use the timer component, just like this guy've done

Tutorial: How to refresh an UpdatePanel control at a timed interval


Right after sucessPH.Visible = true; add following line: ClientScript.RegisterClientScriptBlock(this.GetType(), "refresh", "setTimeout('window.location.href=window.location.href', 3000);", true);


Well.. you are in a web.. So your server can count 3, but you need the client to count 3 and then postback it to the server...

If your server only counts, then the user will not view anything..

Just make your application ajax enable, and use a timer ;)

This is the code for the page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>

            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:Timer ID="Timer1" runat="server" Interval="3000" ontick="Timer1_Tick">
            </asp:Timer>

        </div>
        </form>
    </body>
    </html>

and in the code behind you have this:

  protected void Timer1_Tick(object sender, EventArgs e)
    {
        //do your thing when reach this tick

    }

This page will refresh exactly every 3 seconds.

This is .net 4.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号