Monday, December 14, 2009

ICallbackEventHandler Sample

.ASPX Page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CallBackEventValidationServer.aspx.cs" Inherits="CallBackEventHandler_CallBackEventValidationServer" %>

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 id="Head1" runat="server">

<title>Untitled Page<title>

<script language="javascript" type="text/javascript">

function ReceiveServerData(rValue)

{

alert(rValue);

}

<script>

>

<body>

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

<asp:ScriptManager ID="sm" runat="server">asp:ScriptManager>

<div>

Name: <input type="text" id="Name" runat="server" />

Address: <input type="text" id="Address" runat="server" />

<input type="button" id="SUBMIT" value="SUBMIT" onclick="CallServer(1, alert('Callback'))" />

<div>

<form>

<body>

<html>



.CS Page

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;

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

{

protected string returnValue = string.Empty;

protected void Page_Load(object sender, EventArgs e)

{

ClientScriptManager cm = Page.ClientScript;

String cbReference = cm.GetCallbackEventReference(this, "arg", "ReceiveServerData", "");

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

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

}

#region ICallbackEventHandler Members

public string GetCallbackResult()

{

return returnValue;

}

public void RaiseCallbackEvent(string eventArgument)

{

try

{

Page.ClientScript.ValidateEvent("SUBMIT");

// Callback logic goes here.

if (Name.Value == string.Empty)

{

returnValue += "* Please fill the value for Name \n";

}

if (Address.Value == string.Empty)

{

returnValue += "* Please fill the value for Address \n";

}

}

catch

{

// Failed callback validation logic.

}

}

#endregion

protected override void Render(HtmlTextWriter writer)

{

Page.ClientScript.RegisterForEventValidation("SUBMIT");

base.Render(writer);

}

}




No comments: