Using Using Using Using Using Using Using Using Using Using Using Using Public Partial Class Private String Protected Void Object

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

ICallbackEventHandler

//THIS BELOW CODE REPRESENT THE USING OF I CALLBACK WHERE PAGE DOENT REFRESH

WHEN REQUEST IS PLACED HERE GOES WITH C# CODE

using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;
using System;

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


{
private string m_callbackResult = String.Empty;
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
if (!Page.IsPostBack)
{
this.lblQuote.Text = this.getQuote();
}
// Creating the scripts which need to be called
// this below statment is based with timer for each second
//string clientScript = "function serverRequest(args, context){{ " +
ClientScript.GetCallbackEventReference(this, "args", "getResult", "context") +
"; }} function getResult(result, context){{ eval(result); callServer(); }}
function callServer(){setTimeout('serverRequest();', 1000);} callServer();";
// this below statment is based with out timer
string clientScript = "function serverRequest(args, context){{ " +
ClientScript.GetCallbackEventReference(this, "args", "getResult", "context") +
"; }} function getResult(result, context){{ eval(result); callServer(); }}
function callServer(){serverRequest()} callServer();";

// Register the client script


ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
"RandomQuoteCallback", clientScript, true);
}
private string getQuote()
{
int randomIndex = (new Random()).Next(0, 29);
string Wav = string.Empty;
SqlConnection con = new SqlConnection("server=softpro-
srv\\sqlexpress;User Id=apdrptest;Password=Apdrp@test;database=APDRP;");
SqlDataAdapter da = new SqlDataAdapter("select Parm_name from
Parameters where Parm_Id=" + randomIndex + "", con);
DataSet ds = new DataSet();
da.Fill(ds);
return Wav = ds.Tables[0].Rows[0].ItemArray[0].ToString();
//int randomIndex = (new Random()).Next(0, m_quotes.Count);
//return m_quotes[randomIndex].Author + " - " +
m_quotes[randomIndex].QuoteMessage;
}
//public string getsample()
//{
// SqlConnection con = new SqlConnection("server=softpro-
srv\\sqlexpress;User Id=apdrptest;Password=Apdrp@test;database=APDRP;");
// SqlCommand cmd = new SqlCommand("insert into "
//}
#region ICallbackEventHandler Members

/// <summary>
/// This method returns the result of a callback
/// </summary>
/// <returns>a string of the result of the callback</returns>
public string GetCallbackResult()
{
return m_callbackResult;
}
/// <summary>
/// This method processes the callback event
/// </summary>
/// <param name="eventArgument"></param>
public void RaiseCallbackEvent(string eventArgument)
{
m_callbackResult = "document.getElementById('randomQuote').innerHTML =
'" + this.getQuote() + "'";
}
#endregion
}

Callback.aspx

<asp:Content ID="Content1" ContentPlaceHolderID="CPHead" runat="Server">


</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="CP1" runat="Server">
<asp:Label ID="lblQuote" runat="server" Text="Label"></asp:Label>
<div id="randomQuote">
<asp:Literal runat="Server" ID="Literal1" />
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="CP2" runat="Server">
<asp:Label ID="Label1" runat="server" Width="120px" Height="20px"
BorderColor="Aqua"></asp:Label>
</asp:Content>

=======================******************==================================

Icallback Using in GridCiew .C#


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

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


{

private string strOutput;

protected void Page_Load(object sender, EventArgs e)


{
if (!IsPostBack)
{

ClientScriptManager csm = Page.ClientScript;


String callbackRef = csm.GetCallbackEventReference(this, "arg",
"fnGetOutputFromServer", "");

String callbackScript = "function fnCallServerMethod(arg, context)


{" + callbackRef + "; }";
csm.RegisterClientScriptBlock(this.GetType(),
"fnCallServerMethod", callbackScript, true);

GridView1.DataSource = BindGrid();
GridView1.DataBind();
}
}

private DataTable BindGrid()


{
string sql = "Select * from LoadSurvey";
SqlDataAdapter da = new SqlDataAdapter(sql, "server=softpro-
srv\\sqlexpress;User Id=apdrptest;Password=Apdrp@test;database=APDRP;");
DataTable dt = new DataTable();
da.Fill(dt);
this.ViewState["Table"] = dt;
return dt;
}

public void RaiseCallbackEvent(String clientArgs)


{
string[] str = clientArgs.Split('|');
FilterGrid(str[0], str[1]);
}

public string GetCallbackResult()


{
return strOutput;
}
private void FilterGrid(string CusName, string CusCity)
{
DataTable dt = (DataTable)this.ViewState["Table"];
DataView dv = dt.DefaultView;

if (CusName != "")
//dv.RowFilter = "ID Like '%" + CusName + "%' And Meter_No Like
'%" + CusCity + "%'";
dv.RowFilter = "Name =" + CusName + "%' And Meter_No =" + CusCity
+ "%'";
GridView1.DataSource = dv;
GridView1.DataBind();

using (StringWriter sw = new StringWriter())


{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
GridView1.RenderControl(htw);
htw.Flush();
strOutput = sw.ToString();
}
}
}
}

Callbackgrid.aspxPages

<%@ Page Language="C#" AutoEventWireup="true"


CodeFile="~/Test/callbackgrid.aspx.cs" Inherits="callbackgrid" %>

<!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>

function fnSearchGrid()
{
var param1 = document.getElementById('txtCusName').value;
var param2 = document.getElementById('txtCusCity').value;
var inputarg = param1+"|"+param2;
fnCallServerMethod(inputarg,'');
}

function fnShowAll()
{
document.getElementById('txtCusName').value="";
document.getElementById('txtCusCity').value="";
fnSearchGrid();
}
function fnGetOutputFromServer(strOutput)
{
document.getElementById('divGridView').innerHTML = strOutput;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<table width="300">
<tr>
<td>
Customer Name</td>
<td>
<input id="txtCusName" type="text" /></td>
</tr>
<tr>
<td>
Cutomer City</td>
<td>
<input id="txtCusCity" type="text" /></td>
</tr>
<tr>
<td>
</td>
<td>
<input id="btnFilter" type="button" value="Filter"
onclick="fnSearchGrid()" />
<input id="btnShowAll" type="button" value="Show All"
onclick="fnShowAll()" /></td>
</tr>
</table>
&nbsp; &nbsp;
<br />
<div id="divGridView">
<asp:GridView ID="GridView1" runat="server" EmptyDataText="No Records
Found.">
</asp:GridView>
</div>
</div>
</form>
</body>
</html>

You might also like