I have two textboxs and one timer on my contet page.
In timer onTick event I am changing the text of Textbox2 to System.DateTime.Now.Second.
protected void Timer1_Tick(object sender, EventArgs e)
{
TextBox2.Text = System.DateTime.Now.Second.ToString();
Timer1.Enabled = false;
}
Initially timer is enabled so TextBox2 will fill initially.Then timer will Stop.
Now I want to enable timer,when user start typing in TextBox1. So I wrote following code..
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Attributes.Add("onkeypress", "myfunction()");
}
function myfunction() {
var timer = Sys.Application.findComponent("<%= Timer1.ClientID %>");
//returns the timer's interval in milliseconds:
var waitTime = timer.get_interval;
//sets the timer's interval to 5000 milliseconds (or 5 seconds):
timer.set_interval(3000);
//returns whether or not the timer is enabled:
var isTimerEnabled = timer.get_enabled();
//disables the timer:
timer.set_enabled(true);
//starts the timer:
timer._startTimer();
//stops the timer:
//timer._stopTimer();
}
Now I after 3 seconds page will do postback but Timer1_Tick will not call.
Any one knows why this is happening ?
Loading
Suthish NairPosted Apr 20, 2011, 3:27 PM
You already disabling the Timer from code behind, so it will not work next time. You need enable it from code behind itself.
try the attached sample..
Suthish NairPosted Apr 21, 2011, 1:54 AM
Roy rPosted Apr 21, 2011, 1:43 AM
We are enabling and start timer from javascript then why we need to enable it from code behind again..??
Roy rPosted Apr 21, 2011, 1:43 AM
We are enabling and start timer from javascript then why we need to enable it from code behind again..??
Roy rPosted Apr 21, 2011, 1:34 AM
Its Exactly looks like what I want..
I wanted to stop timer after timer's tick event fire so I was disabling it
Bdw I have one question in the sample that u have given..
up to I know TextBox1_TextChanged event will fire after we leave text box..But right now it is firing on every key press..what is the reason for dat ? Please tell me..
And why u have added below line in Trigger ?
Please tell me..
Ankit NandekarPosted Apr 19, 2011, 4:40 AM
Timer1.Enabled = false;
From Tick event it will work.
Roy rPosted Apr 19, 2011, 1:30 AM
.CS Page