Breaking News

Editors Picks

Wednesday, March 23, 2011

Implementing ICallBackEventHandler in ASP.Net

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ICallBackEventHandlerSample._Default" %>

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

    <script language="javascript" type="text/javascript">
        function GetResult(returnValue, context) {
            document.getElementById("txtDateTime").value = returnValue;
        }
        function Request() {
            CallServer();
            return false;
        }
    script>

head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="txtDateTime" runat="server" Text="">
    asp:TextBox>
    <asp:Button ID="btnDateTime" OnClientClick="return Request()" runat="server"
        Text="Get Server Date Time" />
    form>
body>
html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
 
namespace ICallBackEventHandlerSample
{
    public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientScriptManager scriptManager = Page.ClientScript; 
            String cbReference = scriptManager.GetCallbackEventReference
            (this, "returnValue", "GetResult", "context");
            String callbackScript = "function CallServer(returnValue, context)
            {" + cbReference + "; }";
            scriptManager.RegisterClientScriptBlock(this.GetType(),
            "CallServer", callbackScript, true);
          
       }       
        private string returnValue;
        public string GetCallbackResult()
        {
            return returnValue;
        }
 
        public void RaiseCallbackEvent(string eventArgument)
        {
            returnValue = DateTime.Now.ToString();
        }      
    }
}

No comments :

Post a Comment

Contact Us

Name

Email *

Message *