Saturday, September 04, 2010

How to use ICallbackEventHandler and execute a server method from java script in Asp.net

Markup:
<%@ Page Language="C#" CodeBehind="~/Test.aspx.cs" Inherits="ActiveTest.Test" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ClientScriptManager Example</title>
    <script type="text/javascript">
        var value1 = 0;
        var value2 = 0;
        function ReceiveServerData2(arg, context) {
            Message2.innerText = arg;
            value2 = arg;
        }
        function ProcessCallBackError(arg, context) {
            Message2.innerText = 'An error has occurred.';
        }
    </script>
</head>
<body>
    <form id="Form1" runat="server">
    <div>
        Callback 1 result: <span id="Message1">0</span>
        <br />
        Callback 2 result: <span id="Message2">0</span>
        <br />
        <br />
        <input type="button" value="ClientCallBack1" onclick="CallTheServer1(value1, alert('Increment value'))" />
        <input type="button" value="ClientCallBack2" onclick="CallTheServer2(value2, alert('Increment value'))" />
        <br />
        <br />
        <asp:Label ID="lblMessage" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>
Code Behind:
namespace ActiveTest
{
    public partial class Test : PageICallbackEventHandler
    {
        #region Attributes
 
        public int callBackCount = 0;
        private string script = @"
            function ReceiveServerData1(arg, context)
            {
                Message1.innerText = arg;
                value1 = arg;
            }
        ";
 
        #endregion
 
        #region ICallbackEventHandler Members
 
        public void RaiseCallbackEvent(String eventArgument)
        {
            callBackCount = Convert.ToInt32(eventArgument) + 1;
        }
        public string GetCallbackResult()
        {
            return callBackCount.ToString();
        }
 
        #endregion
 
        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("No page postbacks have occurred.");
            if (Page.IsPostBack)
            {
                sb.Append("A page postback has occurred.");
            }
            this.lblMessage.Text = sb.ToString();
            ClientScriptManager cs = Page.ClientScript;
 
            String cbReference1 = cs.GetCallbackEventReference(this"arg""ReceiveServerData1"this.script);
            String cbReference2 = cs.GetCallbackEventReference("'" + Page.UniqueID + "'""arg",
                                                            "ReceiveServerData2""""ProcessCallBackError"false);
            String callbackScript1 = "function CallTheServer1(arg, context) {" + cbReference1 + "; }";
            String callbackScript2 = "function CallTheServer2(arg, context) {" + cbReference2 + "; }";
            cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer1", callbackScript1, true);
            cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer2", callbackScript2, true);
        }
    }
}
 

Reference - MSDN

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...