In other word we can say that it enables you to perform partial page rendering. You can Update a portion of a web page after a time interval or after perform any event.
To do this task we use following AJAX control.
UpdatePanel
Update panel is a Server-Side AJAX control. The update panel allow us to update a portion of a page without updating the entire page. In other word we can say that it enables us to perform partial page rendering.
Timer
Timer is also a Server-Side AJAX control. The timer control is great to be used when you have an area of user interface that you want to be dynamically update based on some periodic interval.
Example:
First Create a website, then add a web form and copy the below code into aspx page.
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <asp:ScriptManager ID="ScriptManager1" runat="server">
- </asp:ScriptManager>
- <div>
- Page Time:
- <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
- <asp:UpdatePanel ID="UpdatePanel1" runat="server">
- <ContentTemplate>
- Update Panel Time:
- <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
- <br />
- <asp:Button ID="Button1" runat="server" Text="Update" OnClick="Button1_Click" />
- <asp:Timer ID="Timer1" runat="server" Interval="2000" OnTick="Timer1_Tick">
- </asp:Timer>
- </ContentTemplate>
- </asp:UpdatePanel>
- </div>
- </form>
- </body>
- </html>
Copy the below Code into aspx.cs page:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- Label1.Text = Label2.Text = DateTime.Now.ToString();
- }
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- Label1.Text = Label2.Text = DateTime.Now.ToString();
- }
- protected void Timer1_Tick(object sender, EventArgs e)
- {
- Label1.Text = Label2.Text = DateTime.Now.ToString();
- }
- }
Now run the website and see the result.
You can check that only the Update Panel Time is automatically updated after two second. Now click on Update button, only Update Panel Time is update.