Click here to Skip to main content
Licence CPOL
First Posted 18 Nov 2008
Views 27,241
Downloads 348
Bookmarked 17 times

Ajax PostBack Async Callback example

By | 18 Nov 2008 | Article
Small demo of Async postback, and Callback

Introduction

When building an Ajax enabled application i stumbled into some nasty pitfalls.

  1. When creating ascx controls, with Update Panels etc.. i noticed that, whatever you do the Page_load action is executed always of all elements on that page. In my case it would always trigger a DataBase load from a SQL server to a GridView.
  2. When trying to avoid that i couldn't find a command which determined which UpdatePanel was invoking the Async Postback.
  3. So that's why i started to examine the PostBack, Async Postback, and Callback actions provided by ASP.NET

The small project i created just simply lets you show what to expect of the different actions.

Background

The project uses modified examples from other sources.

async_test.aspx show the use and difference between Callback, Async PostBack and Postback

Using the code

Below a code-snippet of the what to expect in async_test.aspx

The Asynctest.aspx page      
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AsyncTest.aspx.cs" Inherits="AjaxTest.AsyncTest" %>

<!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>Untitled Page</title>

<script type="text/ecmascript">

function LookUpStock()

{

var lb = document.getElementById("ListBox1");

var product = lb.options[lb.selectedIndex].text;

CallServer(product, "");

}

function ReceiveServerData(rValue)

{ 

document.getElementById("ResultsSpan").innerHTML = rValue;

}

</script>

</head>

<body>

<form id="form1" runat="server">

<div>



<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">

</asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">

<ContentTemplate>

<asp:Label ID="Label1" runat="server" Text="In UpdatePanel"></asp:Label>

<asp:Label ID="lbInPanel" runat="server"></asp:Label>

<asp:Label ID="lbAsync" runat="server"></asp:Label>

<asp:Button ID="btInPanel" Text="In Panel" runat="server" />

</ContentTemplate>

<Triggers>

<asp:AsyncPostBackTrigger ControlID="btInPanel" EventName="click" />

<asp:AsyncPostBackTrigger ControlID="btOutPanel" /> 

<asp:PostBackTrigger ControlID="btPostBack" />

</Triggers>

</asp:UpdatePanel>

<br />

<asp:Button ID="btOutPanel" Text="Out Panel" runat="server" />

<br />

<asp:Button ID="btpostBack" Text="PostBack" runat="server" />

<hr />

<asp:Label ID="Label2" runat="server" Text="Out Update Panel"></asp:Label>

<asp:Label ID="lbOutPanel" runat="server" Text="Label"></asp:Label>

<br />

<hr />

<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>

<br />

<button type="Button" onclick="LookUpStock()">Look Up Stock</button>



Callback results

<br />

<span id="ResultsSpan" runat="server"></span>

</div>



</form>

</body>

</html>
   
The async_test code behind   
using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

namespace AjaxTest

{

public partial class AsyncTest : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler

{

protected System.Collections.Specialized.ListDictionary catalog;

protected String returnValue;

protected void Page_Load(object sender, EventArgs e)

{

String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");

String callbackScript;

callbackScript = "function CallServer(arg, context)" + "{ " + cbReference + ";}";

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true);

catalog = new System.Collections.Specialized.ListDictionary();

catalog.Add("monitor", 12);

catalog.Add("laptop", 10);

catalog.Add("keyboard", 23);

catalog.Add("mouse", 17);

ListBox1.DataSource = catalog;

ListBox1.DataTextField = "key";

ListBox1.DataBind();

Boolean IsInAsync = ScriptManager.GetCurrent(Page).IsInAsyncPostBack;

lbAsync.Text = "Async:" + IsInAsync; 

lbAsync.Text = lbAsync.Text + " Postback:" + this.IsPostBack.ToString();

lbInPanel.Text = DateTime.Now.ToString();

lbOutPanel.Text = DateTime.Now.ToString();

}

public void RaiseCallbackEvent(String eventArgument)

{

if (catalog[eventArgument] == null)

{

returnValue = "-1";

}

else

{

returnValue = "<Strong>Callback:" + this.IsCallback.ToString() + "</Strong>:StockValue" + catalog[eventArgument].ToString();

}

}

public String GetCallbackResult()

{

return returnValue;

}

}

}
        

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Member 4286352

Web Developer

Netherlands Netherlands

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
General[My vote of 1] Same sample on MSDN PinmemberTheDmD1:24 12 May '09  
GeneralMy vote of 1 PinmemberMember 21124978:52 27 Jan '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 18 Nov 2008
Article Copyright 2008 by Member 4286352
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid