ASP.NET
AJAX provides the most visible feature that the ability to partial or
incremental page updates with PostBack to the server. Before AJAX
implementation is ASP.NET page update require a round trip to the server which
requires a page refresh.
UpdatePanel control remove the requirement to refresh the
whole page with each postback which
improves user experience. While working with UpdatePanel you may notice that
there are two child elements,
ContentTemplate
<contentTemplate>
element which is essentially encapsulates the content that will be held
by UpdatePanel.
Triggers
<Triggers> element which
specifies the controls on the page that will trigger partial render of the
UpdatePanel in which the <Triggers> element resides. <Triggers> element
contains two child nodes <asp:AsyncPostBackTrigger> and <asp:PostBackTrigger>. Both
accept the two attributes ControlID and EventName.
<asp:AsyncPostBackTrigger> element that particularly useful in that it can target any control
that exists of a child of any UpdatePanel control in the unit of encaptulation.
Thus, any control made to trigger a partial page update.
<asp:PostBackTrigger> element can be used to trigger a partial page update but this one
requires round trip to the server.
Syntax:
<asp:UpdatePanel ID="UpdatePanel1"
runat="server"
UpdateMode="Conditional">
<ContentTemplate>
…
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="" EventName="" />
</Triggers>
</asp:UpdatePanel>
Note that when UpdatePanel controls are
nested, when the UpdateMode is set to Conditional,
if the child UpdatePanel is triggered, but the parent is not, then only the
child UpdatePanel will refresh. However, if the parent UpdatePanel is
refreshed, then the child UpdatePanel will also be refreshed.
Designer source code:
<%@ 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>UpdatePanel
with trigger</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePartialRendering="true">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" /><br />
<asp:Button ID="Button1" runat="server" Text="Update Both Panels" OnClick="Button1_Click"
/>
<asp:Button ID="Button2" runat="server" Text="Update This Panel" OnClick="Button2_Click"
/>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label2" runat="server" ForeColor="red" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
C#
code:
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)
{ }
protected void Button1_Click(object
sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToLongTimeString();
Label2.Text = DateTime.Now.ToLongTimeString();
}
protected void Button2_Click(object
sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToLongTimeString();
}
}