Reputation: 3398
I need to call btn_submit_Click(object sender, EventArgs e)
from another method protected void Timer1_Tick(object sender, EventArgs e)
which normally calls by a button click.
Now here in Timer1_Tick
compares a time and if current time exceeds, i need to call btn_submit_Click(object sender, EventArgs e)
automatically.
protected void Timer1_Tick(object sender, EventArgs e)
{
DateTime et = DateTime.Parse(Session["endtime"].ToString());
if (DateTime.Now.TimeOfDay >= et.TimeOfDay)
{
// btn_submit_Click();
Response.Redirect("Welcome.aspx");
}
else
{
Label1.Text = DateTime.Now.ToLongTimeString();
}
}
Please suggest me a way to do this.
Upvotes: 0
Views: 2855
Reputation: 26396
Jon Skeet mentioned the right approach. Refactor the code in your btn_submit_click
into a central method that can be called by both Button and Timer. But you can still do submit_click(sender, e)
protected void Timer1_Tick(object sender, EventArgs e)
{
....
btn_submit_Click(sender, e);
...
}
Upvotes: 2
Reputation: 1503409
Personally I would take a slightly different approach. You're not actually trying to say that a button was clicked - you're just interested in the same side effects as a button click. So extract a third method which only has the relevant parameters (there may not be any) and call that method from both btn_submit_Click
and Timer1_Tick
. That way you don't have to come up with a sender
and EventArgs
for a button click which didn't happen. So for example:
protected void btn_submit_Click(object sender, EventArgs e)
{
// Maybe validation?
Submit();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
DateTime et = DateTime.Parse(Session["endtime"].ToString());
if (DateTime.Now.TimeOfDay >= et.TimeOfDay)
{
Submit();
Response.Redirect("Welcome.aspx");
}
else
{
Label1.Text = DateTime.Now.ToLongTimeString();
}
}
private void Submit()
{
// Common code to execute on either the timer tick or button click
}
Upvotes: 4