Friday, October 16, 2009

Partial page updates using ASP.NET Ajax

Please visit my new Web Site

In my previous tutorial or article i have explained how to start ASP.NET AJAX. In this ajax tutorial or ajax article i will show you how we can update partial page using ajax or how we can use multiple update panel in aspx page. Before going to an example i want to let you know some basics on partial page loading using ajax.

1. If you use muliple update panel in a page & initiate triger for this section within the update panel then you have no problem.

2. If you want to put a portion in a update panel & want ajax enabled render from an event outside of Update Panel control then you have to learn Triggers.

3. If you want to update more than one update panel control by a click event(outside of all Update Panel) its also possible.

4. Bydefault for each async or sync post back each update panel will be rendered. You can restrict the invocation by setting the UpdateMode="Conditional". Means bydefault UpdateMode="Always".

So ok we want to examine each issues by using the below example.

Add a page then copy the below HTML mark up code into your page.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PartialUpdate.aspx.cs" Inherits="PartialUpdate" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
<title>Untitled Page</title>
<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<asp:Label runat="server" ID="lbl1"></asp:Label>
<br /><br />
<asp:Button runat="server" ID="Button1" Text="Async PostBack 1"
onclick="Button1_Click" />
<Triggers><asp:AsyncPostBackTrigger ControlID="Button1_ext" EventName="Click" /></Triggers>

<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<asp:Label runat="server" ID="lbl2"></asp:Label>
<br /><br />
<asp:Button runat="server" ID="Button2" Text="Async PostBack 2"
onclick="Button2_Click" />
<Triggers><asp:AsyncPostBackTrigger ControlID="Button1_ext" EventName="Click" /></Triggers>

<br />
<br />

<asp:Button runat="server" ID="Button1_ext" Text="Render Update Panels Content"
onclick="Button1_ext_Click" />


Server Side Code:

protected void Button1_Click(object sender, EventArgs e)
lbl1.Text = "Current Time Is: " + DateTime.Now;

protected void Button2_Click(object sender, EventArgs e)
lbl2.Text = "Current Time Is: " + DateTime.Now;

protected void Button1_ext_Click(object sender, EventArgs e)
lbl1.Text = "Current Time Is: " + DateTime.Now;
lbl2.Text = "Current Time Is: " + DateTime.Now;


Ok now run the project. What you get corresponding buttons click event updates corresponding label of its Update Panel. The third button which is outside the Update Panels will render both Update Panels without refreshing the page? Why because here i used Triggers in both Update Panel for this Button control. Now remove Triggers tag from second Update Panel & examine the output. Hope now you can understand all basic aspects on partial page loading using ajax. So keep experimenting.


Want to say something?

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash