Script Callbacks were also part of my "Advanced ASP.NET 2.0" day at the Community Bootcamp 2005 in Bad Ischl. Aside from showing the usual callback sample, I decided that something more useful was in order. That is why I went a tad further by showing off the controls introduced in the RefreshPanel GotDotNet workspace. I came across those a while back when reading articles on Bertrand Le Roy's blog:
Of course we did labs on CallbackProxy and RefreshPanel, the latter one is described in this blog entry (again, a streamlined version of the lab done by Alexander Schoeppl).
Let's start with the result we wanted to achieve:
Step 1: Copy RefreshPanel.dll
First, copy RefreshPanel.dll to the \bin directory of your site. You can get it here.
Step 2: Set up a connection string in web.config
We will use that later both in markup and code beside file:
connectionString="Data Source=cbc05vpc\cbc05;Initial Catalog=Northwind;User=sa;Password=P@ssw0rd"/>
Step 3: ShowCustomerOrders.aspx
Basically, "organized" in three sections (separated by a blank line):
<%@ Page Language="C#" AutoEventWireup="true"
Title="Callback Demo" %>
<%@ Register TagPrefix="rp" Namespace="MyControls.RefreshPanel"
<html xmlns="http://www.w3.org/1999/xhtml" >
<form id="form1" runat="server">
Render Date: <asp:Literal ID="Literal1" runat="server"></asp:Literal>
<asp:DropDownList ID="DropDownList1" runat="server"
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CustomerID] FROM [Customers]">
<rp:RefreshButton ID="MyButton" RefreshPanelID="RFPanel1"
<rp:RefreshPanel runat="server" ID="RFPanel1" OnRefreshing="FillData">
<asp:GridView ID="GridView1" runat="server">
Register imports the RefreshPanel control suite for us, the Label and DropDown are also very straightforward. The RefreshPanel control itself contains a single GridView control, and it is linked to the server-side method FillData which we will examine in the next step. The RefreshButton is responsible for activating the out of band call back to the server - that's also where we get the value from the dropdown control, and pass it as an event argument to FillData.
Note that the control names are hardcoded, in the real world we'd build that string dynamically, because otherwise we'd get into trouble, eg with master pages.
Step 4: ShowCustomerOrders.aspx.cs
Page_Load is trivial, we are only interested in FillData:
public void FillData(object sender, RefreshingEventArgs e)
string connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString();
string sqlCmd = "Select * from Orders where customerID = @CustomerID";
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(sqlCmd, conn);
SqlDataReader reader = cmd.ExecuteReader();
GridView1.DataSource = reader;
No magic in our code, but: RefreshPanel takes care of giving us a GridView control to work with, and shipping the resulting HTML to the client - and inserting it into the page. Very, very neat indeed.
CallbackDemo.zip (43.51 KB)