[Interview Question] What is the Timer control in ASP.NET Ajax?

The Ajax Timer control is a server control that embeds a JavaScript component into the Web page. Then the JavaScript component initiates the postback from the browser when the interval that is defined in the Time Interval property has elapsed.

Time control always used with ScriptManager class that means an instance of ScriptManager class me be include in web page before add Timer control.
When Timer control initiates the postback, it rails the Tick event on the server. You can use it by creating an event handler on server to perform action periodically.

Scenarios:

Case1: If you want to refresh your web page or a part of web page at a timed interval then we can use Ajax Timer Control.

Case2: In case of refresh only a part of web page Ajax Timer Control use with ScriptManager and UpdatePanel control.

Case 1. Refresh Complete Page

Step 1: Add ScriptManager Control in your design page. You will find it at ToolBox –> Ajax Extensions Tab.

  1. <asp:ScriptManager ID="ScriptManager1" runat="server">
  2.     </asp:ScriptManager>

Step 2. Add timer control in your design page. You will find Time control in your ToolBox –> Ajax Extensions Tab.

  1. <asp:Timer ID="Timer1" runat="server"
  2.             Interval="10000" ontick="Timer1_Tick">
  3. </asp:Timer>

Interval=”10000″

Here we have setup Interval property of Timer is “10000” means page will be refreshed in every 10 seconds because Interval property is defined in milliseconds.

ontick=”Timer1_Tick”

This event we will use to add business logic which will refreshed at every 10 seconds.

Step 3. Add your Business Logic which you want to update after a time interval at Timer Tick event in Server page

  1. protected void Timer1_Tick(object sender, EventArgs e)
  2.         {
  3. // Place your business logic here which will
  4. // refresh after the given time interval
  5.  
  6. }

Case 2. Refresh a part of web page

This is the most common scenario in which we use Timer control.

We can easily understand this case with the help of following example.

If we want to print the Current date time on a label in page and update it with real time change then we can use Timer control to achieve this.

In this scenario Timer control will be used with ScriptManager control, the UpdatePanel control.

Step 1: Add ScriptManager Control in your design page. You will find it at ToolBox –> Ajax Extensions Tab.

  1. <asp:ScriptManager ID="ScriptManager11" runat="server">
  2.     </asp:ScriptManager>

Step 2: Add UpdatePanel Control in your design page. You will find it at ToolBox –> Ajax Extensions Tab.

  1. <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  2.     </asp:UpdatePanel>

Step 3: Add Timer Control within UpdatePanel.

  1. <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  2.         <ContentTemplate>
  3.             <asp:Timer ID="Timer1" runat="server" Interval="10000"
  4.                    ontick="Timer1_Tick">
  5.             </asp:Timer>
  6.         </ContentTemplate>
  7. </asp:UpdatePanel>

I have already explained Interval property and ontick event above in details.

Step 4: Add Label within UpdatePanel.

  1. <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  2.         <ContentTemplate>
  3.             <asp:Timer ID="Timer1" runat="server"
  4.                       Interval="10000" ontick="Timer1_Tick">
  5.             </asp:Timer>
  6.             <asp:Label ID="MyLabel" runat="server"
  7.                       Text="This Panel Still not refreshed.">
  8.             </asp:Label>
  9.         </ContentTemplate>
  10.     </asp:UpdatePanel>

Here we have set the default text of label is “This Panel Still not refreshed”. So that we can easily verify when panel start to refresh at every 10 seconds.

Step 5: Add your Business Logic which you want to update after a time interval at Timer Tick event in Server page

  1. protected void Timer1_Tick(object sender, EventArgs e)
  2.         {
  3. // Place your business logic here which will refresh after the given time interval
  4. MyLabel.Text = "Panel refreshed at: " + DateTime.Now.ToLongTimeString();
  5. }

Now save your project and press ctrl+f5 to run it and wait at least 10 second to see the panel to refresh. And you will see that the text of label changed at every 10 seconds without reload the complete page.

[Interview Question] What is a ScriptManager in ASP.NET AJAX?

What is a ScriptManager in ASP.NET AJAX?

In order to use AJAX functionality on a web page it is required to add a ScriptManager control to the page. ScriptManager control register Microsoft Ajax Library scripts to the web page so we should use the one ScriptManager per page. ScriptManager control enables script to use the type system extensions and to support features like partial-page rendering. Continue reading [Interview Question] What is a ScriptManager in ASP.NET AJAX?

[Interview Quiz] What is the limitations of the Ajax in ASP.Net

The Answer of this question also covers the following:
– What are the disadvantages of the Ajax?
– What are the cons of the using Ajax in ASP.Net web application?
– Can Ajax break the security?
– In What circumstance, Ajax will not work? Continue reading [Interview Quiz] What is the limitations of the Ajax in ASP.Net

[Interview quiz] What is the Ajax UpdatePanel

what is the Update panel in asp.net?

Just the Answer

The ASP.NET UpdatePanel control enables you to add AJAX functionality in to the web page. It can be used to update the selected parts of the page instead of refreshing the whole page with a postback. You can say that we can perform the Partial-page rendering using the UpdatePanel control in ASP.Net. Continue reading [Interview quiz] What is the Ajax UpdatePanel

Writing and reading cookies in ASP.net using c#

A cookie is just a little bit of text which is send by the browser each time with the request for the page. Each time on the machine when browser will make the request to the web page, it will send the existing cookies along with the request too. So this approach helping the Web site or web application remember users and other information such as user’s preferences, shopping cart contents and the user’s session. Continue reading Writing and reading cookies in ASP.net using c#

JQuery multiple files upload in asp.net and C# using Ajax form

In this article we will discuss about the files uploading in the asp.net. Asp.net provides the FileUpload control and you can use this with a small amount of code lines for uploading the documents. However there are many other ways to upload the files using the several jQuery,flash and Ajax plugins. In the following you can see a such a script that use the Ajax form plugin. Continue reading JQuery multiple files upload in asp.net and C# using Ajax form

Create user control for the footer of the your web application in asp.net

With the help of the following tutorial, you can create a user control for the footer of your web application. The article demonstrates how you can create a user control and how can you use that. In this article code is written using Visual Studio 2008. So first you need to add a web user control in your project, just follow these instructions: Continue reading Create user control for the footer of the your web application in asp.net

How to disable Session state on your asp.net page

Every asp.net page has the session state by default, this may decrease the total performance a little bit. So You can disable the session state from the pages when you don’t need to use sessions. And you can do so to set the false value of the EnableSessionState attribute in the page directive of your page like as: Continue reading How to disable Session state on your asp.net page