![]() |
Web Development »
ASP.NET Controls »
General
Intermediate
License: The GNU General Public License (GPL)
Creating Serverside Clock control Using Asp.net Ajax and C#By blue_arcDemonstration of Timer Control and updata panel to fire timed server events |
C# (C# 1.0, C# 2.0, C# 3.0), .NET, WebForms, Dev
|
||||||||
|
Advanced Search |
|
|
|
||||||||||||||||
In this article we will learn how to use Timer and Updatepanel controls of Asp.net, inside Custom Control, to create other wraper controls.
To use this you need to have atleast .net framework 2.0 and Asp.net AJAX Extention Controls.
Using Timer we can repeat any set of commands on timely basis. Using Update panel we can process our request in asynchronous mode. So to demonstrate this I will show you a simple Server Side Clock Control.. Where I will set a set of instruction on Tick event of Timer then will wrap it in Side a Updatepanel. And at every 1 second clock will refresh it self.
It is assuming that the reader of this article is aware about Asp.net Ajax.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Ravs
{
// Declare a clock class
[ToolboxData("<{0}:Clock runat="server">{0}:Clock>")]
public class Clock : WebControl
{
//Create one TimerControl
Timer timer = new Timer();
// Create one label control for click value
Label clockLabel = new Label();
// Declare one Updatepanel
UpdatePanel updatePanel = new UpdatePanel();
// Now override the Load event of Current Web Control
protected override void OnLoad(EventArgs e)
{
// Create Ids for Control
timer.ID = ID + "_tiker";
clockLabel.ID = ID + "_l";
// get the contentTemplate Control Instance
Control controlContainer = updatePanel.ContentTemplateContainer;
// add Label and timer control in Update Panel
controlContainer.Controls.Add(clockLabel);
controlContainer.Controls.Add(timer);
// Add control Trigger in update panel on Tick Event
updatePanel.Triggers.Add(new AsyncPostBackTrigger() { ControlID = timer.ID, EventName = "Tick" });
updatePanel.ChildrenAsTriggers = true;
// Set default clock time in label
clockLabel.Text = DateTime.Now.ToString();
// Set Interval
timer.Interval = 1000;
// Add handler to timer
timer.Tick += new EventHandler(timer_Tick);
updatePanel.RenderMode = UpdatePanelRenderMode.Block;
//Add update panel to the base control collection.
base.Controls.Add(updatePanel);
}
protected override void Render(HtmlTextWriter output)
{
base.Render(output);
}
void timer_Tick(object sender, EventArgs e)
{
// Set current date time in label to move current at each Tick Event
clockLabel.Text = DateTime.Now.ToString();
}
}
}
Up till now what we have seen. Few text and few lines of code fairly commented. So can you as a reader are able to understand what i want to say. I think no. So now I have to come out of my laziness and should discuss this with you in detail. So lets take each bit of code and examine, what exactly it do.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
[ToolboxData("<{0}:Clock runat="server">{0}:Clock>")]
public class Clock : WebControl
//Create one TimerControl
Timer timer = new Timer();
// Create one label control for click value
Label clockLabel = new Label();
// Declare one Updatepanel
UpdatePanel updatePanel = new UpdatePanel();
// Now override the Load event of Current Web Control
protected override void OnLoad(EventArgs e)
{
// Create Ids for Control
timer.ID = ID + "_tiker";
clockLabel.ID = ID + "_l";
Below you can see I tried to get the ContentTemplateContainer in a control. Which is a instance of Internal Control with implementation of Itemplate. Where we will now add our controls.
// get the contentTemplate Control Instance
Control controlContainer = updatePanel.ContentTemplateContainer;
// add Label and timer control in Update Panel
controlContainer.Controls.Add(clockLabel);
controlContainer.Controls.Add(timer);
So now as we have added or all the control (Timer and Label)in the updatepanel's contentpanel.
So let's move to the main part. Now we will add timer control's reference in update panel’s control trigger with Event name "Tick", so that when ever Timer will raise it's Tick event Update panel will catch it and make is as an Asynchronous request.
Now we will set the default data in to our label. which is current DateTime, and we will set timer Interval property, (I am using static 1000, you can extend if you wish) so that Timer will know when it has to fire it's tick event.
Now the last thig we have to do is create a event for timer, so here it is.
timer.Tick += new EventHandler(timer_Tick);
, afte that add updatepanel into base control collection.
// Add control Trigger in update panel on Tick Event
updatePanel.Triggers.Add(new AsyncPostBackTrigger() { ControlID = timer.ID, EventName = "Tick" });
updatePanel.ChildrenAsTriggers = true;
// Set default clock time in label
clockLabel.Text = DateTime.Now.ToString();
// Set Interval
timer.Interval = 1000;
// Add handler to timer
timer.Tick += new EventHandler(timer_Tick);
updatePanel.RenderMode = UpdatePanelRenderMode.Block;
//Add update panel to the base control collection.
base.Controls.Add(updatePanel);
}
Compile your control class and add it in Toolbox, and then drag it on the page it will show you running clock with out refreshing the page. I hope you will learn something from this, and may be you will come up with something more powerful.
While developing this code i got few more good ideas using Timer and update panel, I will try my best to come back to you guys with something new as soon as possible.
| You must Sign In to use this message board. | ||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 2 Jun 2008 Editor: |
Copyright 2008 by blue_arc Everything else Copyright © CodeProject, 1999-2009 Web20 | Advertise on the Code Project |