Monday, February 13, 2012

DoCallback Function Implementation for Response.end Response.flush

Here is DoCallback Function Implementation when you use Response.end Response.flush in your aspx page

ASPX Code :

< %@ Page language="c#" Codebehind="WebForm1.aspx.cs" inherits="MsdnMag.CallbackPage" %>

< SCRIPT language="javascript">

function DoCallback(url, params)

{

var pageUrl = url + "?callback=true&param=" + params;

var xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");

xmlRequest.open("POST", pageUrl, false);

xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

xmlRequest.send(null);

return xmlRequest;

}

function MoreInfo()

{

var selectedEmpID = document.all["EmployeeList"].value;

var xmlRequest = DoCallback("webform1.aspx", selectedEmpID);

// sync updates

Msg.innerHTML = xmlRequest.responseText;

}

< /SCRIPT>

< HTML>

< HEAD>

< title>Script Callbacks< /title>

< /HEAD>

< body>

< form runat="server">

< h1>Demonstrate Out-of-band Calls< /h1>

< h2>< %=Request.Url%>< /h2>

< hr>

< asp:DropDownList Runat="server" ID="EmployeeList" />

< Button Runat="server" ID="ButtonGo">Go Get Data< /Button>

< hr>

< span ID="Msg" />

< /form>

< /body>

< /HTML>

Code Behind:

using System;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

// Definition of the page class

namespace MsdnMag

{

public class CallbackPage : System.Web.UI.Page

{

// References to page controls

protected DropDownList EmployeeList;

protected HtmlButton ButtonGo;

// Initialize the page here

private void Page_Load(object sender, EventArgs e)

{

if (IsCallback())

return;

if (!IsPostBack)

PopulateList();

// Button-to-callback binding

string callbackRef = "MoreInfo()";

ButtonGo.Attributes["onclick"] = callbackRef;

}

private bool IsCallback()

{

if (Request.QueryString["callback"] != null)

{

string param = Request.QueryString["param"].ToString();

Response.Write(RaiseCallbackEvent(param));

Response.Flush();

Response.End();

return true;

}

return false;

}

// Handle the "Go Get Data" button click

protected void OnGoGetData(object sender, EventArgs e)

{

}

// Populate the drop-down list with employee names

private void PopulateList()

{

//SqlDataAdapter adapter = new SqlDataAdapter(

// "SELECT employeeid, lastname FROM employees",

// "SERVER=(local);DATABASE=northwind;TRUSTED_CONNECTION=true;");

//DataTable table = new DataTable();

//adapter.Fill(table);

//EmployeeList.DataTextField = "lastname";

//EmployeeList.DataValueField = "employeeid";

DataTable dt = CreateDataTable();

AddDataToTable("test", "test", "test", dt);

EmployeeList.DataSource = dt;

EmployeeList.DataBind();

}

private DataTable CreateDataTable()

{

DataTable myDataTable = new DataTable();

DataColumn myDataColumn;

myDataColumn = new DataColumn();

myDataColumn.DataType = Type.GetType("System.String");

myDataColumn.ColumnName = "id";

myDataTable.Columns.Add(myDataColumn);

myDataColumn = new DataColumn();

myDataColumn.DataType = Type.GetType("System.String");

myDataColumn.ColumnName = "username";

myDataTable.Columns.Add(myDataColumn);

myDataColumn = new DataColumn();

myDataColumn.DataType = Type.GetType("System.String");

myDataColumn.ColumnName = "firstname";

myDataTable.Columns.Add(myDataColumn);

myDataColumn = new DataColumn();

myDataColumn.DataType = Type.GetType("System.String");

myDataColumn.ColumnName = "lastname";

myDataTable.Columns.Add(myDataColumn);

return myDataTable;

}

private void AddDataToTable(string username, string firstname, string lastname, DataTable myTable)

{

DataRow row;

row = myTable.NewRow();

row["id"] = Guid.NewGuid().ToString();

row["username"] = username;

row["firstname"] = firstname;

row["lastname"] = lastname;

myTable.Rows.Add(row);

}

// *******************************************************

// Implement the callback interface

string RaiseCallbackEvent(string eventArgument)

{

return "You clicked: " + eventArgument;

}

// *******************************************************

}

}

No comments:

Post a Comment