如何在aspx网站上点击按钮启动计时器事件

时间:2014-03-10 10:13:21

标签: c# asp.net timer

我正在使用aspx网站,我正在使用计时器事件。 我只想在我点击一个特定按钮时运行计时器。现在计时器正在页面加载上运行。在Winforms中我们使用timer.Tick()事件来完成它。但它不支持网站。 任何人都可以帮我解决这个问题。 在此先感谢....

我的代码在这里......

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.ComponentModel;


    public partial class Expert : System.Web.UI.Page
    {
     public static BackgroundWorker worker = new BackgroundWorker();

      protected void Page_Load(object sender, EventArgs e)
          {
           int id;
           id = Int32.Parse(Request.QueryString["id"]);
           SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LPSConnection"].ConnectionString.ToString());
        con.Open();
        SqlDataReader rd;
        rd = new SqlCommand("Select * from ExpertChat where id=" + id, con).ExecuteReader();
        rd.Read();
        Image1.ImageUrl = rd["image1"].ToString();

    Label8.Text = rd["image1"].ToString();
    //Image3.ImageUrl = rd["image1"].ToString();
        Label1.Text = rd["SName"].ToString();
        Label2.Text = rd["Skills"].ToString();
        Label5.Text = rd["rate"].ToString();
        Label3.Text = rd["ReviewCount"].ToString();
        Label4.Text = rd["Title"].ToString();
        Label6.Text = rd["ReviewCount"].ToString();
        Label7.Text = rd["Title"].ToString();
        Label9.Text = rd["Qualification"].ToString();
        Label10.Text = rd["MyServices"].ToString();
        Label11.Text = rd["other"].ToString();
        Label14.Text = rd["IsLoggedIn"].ToString();

    int x = Int32.Parse(rd["IsLoggedIn"].ToString());
        if (x == 1)
        {
            Image2.ImageUrl = "~/online.png";
        }

        else
        {
            Image2.ImageUrl = "~/offline.png";
        }
        rd.Close();
        if (Session["User"] == "User")
        {
            SqlDataReader rd1 = new SqlCommand("Select funds from signup where email='" + Session["email"].ToString() + "'", con).ExecuteReader();
            rd1.Read();
            Label13.Text = rd1["funds"].ToString();
        }
        worker.DoWork += new DoWorkEventHandler(DoWork);
        worker.WorkerReportsProgress = true;
        worker.WorkerSupportsCancellation = true;

        con.Close();

    }

protected void Button2_Click(object sender, EventArgs e)
{

}

protected void SendButton_Click(object sender, EventArgs e)
{
    string messageMask = "{0} @ {1} : {2}";
    string message = string.Format(messageMask, "yash", DateTime.Now.ToShortTimeString(), NewMessageTextBox.Text);
    TextBox1.Text = TextBox1.Text + Environment.NewLine + message;
    // Calling the DoWork Method Asynchronously
    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "myScript", "document.getElementById('" + NewMessageTextBox.ClientID + "').value = '';", true);
    ScriptManager.RegisterStartupScript(this, this.GetType(), "TextBox1slide", "buttonClicked();", true);

    if (worker.IsBusy != true)
    {

        worker.RunWorkerAsync(new string[] { message, Label3.Text });


    }


}



private static void DoWork(object sender, DoWorkEventArgs e)
{
    string[] args = e.Argument as string[];
    string value = args[0];
    string id = args[1];
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LPSConnection"].ConnectionString.ToString());
    con.Open();
    SqlCommand cmd;
    cmd = new SqlCommand("Update Chat set Data=@message,Updated1=1 where id=" + Int32.Parse(id), con);
    cmd.Parameters.AddWithValue("@message", value);

    cmd.ExecuteNonQuery();
    con.Close();
}

protected void ChatTextTimer_Tick(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LPSConnection"].ConnectionString.ToString());
    con.Open();

    //if (second1.Visible)
    //{
    //    SqlDataReader rd7 = new SqlCommand("Select IsApproved from Chat where id=" + Int32.Parse(Label2.Text) + "'", con).ExecuteReader();
    //    rd7.Read();
    //    string str3 = rd7["IsApproved"].ToString();
    //    rd7.Close();
    //    if (str3 == "Approved")
    //    {
    //        second1.Visible = false;
    //    }
    //    else if (str3 == "Canceled")
    //    {
    //        second1.Visible = false;
    //        second3.Visible = true;


    //    }

    //}
    //else
    //{
    int x1 = 0;
    SqlDataReader rd;
    rd = new SqlCommand("Select UserInitial,Updated from Chat where id =" + Int32.Parse(Label3.Text), con).ExecuteReader();
    rd.Read();
    string str;
    int i;
    i = Int32.Parse(rd["Updated"].ToString());
    if (i == 1)
    {
        str = rd["UserInitial"].ToString();
        rd.Close();

        x1 = x1 + 1;
        TextBox1.Text = TextBox1.Text + Environment.NewLine + str;
        SqlCommand
        cmd = new SqlCommand("Update Chat set Updated=0 where id=" + Int32.Parse(Label3.Text), con);
        cmd.ExecuteNonQuery();

    }
    else
    {
        rd.Close();
    }
    con.Close();
    ScriptManager.RegisterStartupScript(this, this.GetType(), "TextBox1slide", "buttonClicked();", true);

}

protected void TextBox1_TextChanged(object sender, EventArgs e)
{

}



protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{

    if (Session["User"] != "User")
    {
        Response.Redirect("signin.aspx");
    }
    else
    {
        if (Int32.Parse(Label13.Text) < 20)
        {
            Response.Redirect("Payment.aspx");
        }
        else
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LPSConnection"].ConnectionString.ToString());
            con.Open();
            SqlDataReader rd = new SqlCommand("Select Booked,email from ExpertChat where id=" + Request.QueryString["id"].ToString(), con).ExecuteReader();
            rd.Read();
            int x = Int32.Parse(rd["Booked"].ToString());
            string str = rd["email"].ToString();
            rd.Close();
            if (x == 0)
            {
                //second1.Visible = true;

                SqlDataReader mRead1, mRead3, mRead4;
                mRead1 = new SqlCommand("insert into chat (ExpertName,UserName,rate,Data,Date,UserInitial) Values ('" + str + "','" + Session["SName"] + "','" + Label5.Text + "','Hello','" + DateTime.Now.ToShortDateString() + "','yash')", con).ExecuteReader();
                mRead1.Close();
                mRead3 = new SqlCommand("Update ExpertChat Set Booked=1 where SName='" + Label1.Text + "'", con).ExecuteReader();
                mRead3.Close();
                mRead4 = new SqlCommand("Select top 1 id from Chat  where ExpertName='" + str + "' Order by id desc", con).ExecuteReader();
                mRead4.Read();
                int y = Int32.Parse(mRead4["id"].ToString());
                mRead4.Close();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "TextBox1slide", "button1Clicked();", true);
                ChatTextTimer.Enabled = true;

                second3.Visible = false;
                    second1.Visible = false;
                    second2.Visible = false;
                    int id;
                    id = y;
                    Label3.Text = id.ToString();
                    SqlDataReader rd1 = new SqlCommand("Select ExpertName from Chat where id=" + y, con).ExecuteReader();
                    rd1.Read();
                    string str23 = rd1["ExpertName"].ToString();
                    rd1.Close();
                    SqlDataReader rd3;
                    rd3 = new SqlCommand("Select * from ExpertChat where email='" + str23 + "'", con).ExecuteReader();
                    rd3.Read();
                    string str2;
                    str2 = rd3["email"].ToString();
                    Label1.Text = rd3["rate"].ToString();
                    Image3.ImageUrl = rd3["image1"].ToString();
                    Label12.Text = rd3["rate"].ToString();
                    rd3.Close();
                    con.Close();



            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "TextBox1slide", "buttonClicked();", true);
                //second2.Visible = true;
            }


        }
    }




}

      }

我想在ImageButton1_Click事件被解雇时运行它....

1 个答案:

答案 0 :(得分:1)

如果您正在使用Visual Studio 2010(如果我的版本错误,请更正我)您应该可以访问Intellisense。您可以使用它来查看所有可用的方法。

Timer.Start()

http://msdn.microsoft.com/en-us/library/system.timers.timer.start(v=vs.110).aspx