<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SimpleMethodCallbacks.aspx.cs"
    Inherits="SimpleMethodCallbacks" EnableViewState="false" EnableEventValidation="false" %>
<%@ Register Namespace="Westwind.Web.Controls" Assembly="Westwind.AjaxToolkit" TagPrefix="ww" %>
<%@ Register Src="~/ShowCode.ascx" TagName="ShowCode" TagPrefix="uc1" %>
<html>
<head runat="server">
    <title>wwMethodCallback Page Method Callbacks</title>
    <link href="WestWind.css" rel="stylesheet" type="text/css" />    
</head>
<body>
       <form id="form1" runat="server">
<ww:wwMethodCallback ID="Proxy" runat="server" 
                    PostBackMode="PostMethodParametersOnly"                          
                    ScriptLocationType="WebResource"
></ww:wwMethodCallback>
        <div>
            <h1>wwHoverWindow Method Callbacks</h1>
            <br />
            <a href="default.aspx">Home</a> | <a href="SimpleMethodCallbacks.aspx">
                Reset Page</a> | <span style="font-style: italic; font-size: x-small">Page generated
                    at:
                    <%= DateTime.Now %></span>
                    
                    <div style="margin-left: 15px">
                        <p />
            
            The following examples call methods on the running ASPX page. The methods on the server 
            are accessible once marked with [CallbackMethod] attribute. Once set the wwMethodCallback
            control will expose these methods either via manual client code or via an auto-generated
            proxy object. The Proxy maps the signatures of the methods so you can do:
            <pre>            Proxy.Helloworld("Rick",HelloworldCallback,OnPageError);
            Proxy.AddNumbers(15,12,AddNumbersCallback,OnPageError);</pre>
            
            Results are sent back to a callback javascript client handler. Parameters and Result values
            are passed as JSON values between client and server which is a lightweight way to pass simple
            and complex data. To see what's happening  I recommend you hook up an HTTP viewer like 
            <a href="http://www.fiddlertool.com/">Fiddler</a> to watch the HTTP traffic.<br />
                        <br />
            <br />
            <strong>Mandatory Hello World Method<br />
            </strong><span style="font-size: 8pt">protected string Helloworld(string Name)</span><br />
            <hr />
            &nbsp;<strong></strong>Enter your name:
            <asp:TextBox ID="txtName" runat="server" Width="178px">John Doe</asp:TextBox><input id="btnSubmit" type="button" value="Say Hello" 
                   onclick="HelloWorld();" /> 
            <input type="button" value=" Hide " id="btnHide" 
                    onclick="HideHelloWorld();" style="width:100"/>
            <input type="button" value="Manual Call" id="btnManual" 
                    onclick="ManualMethodCall();" style="width:100"/>
            <hr />
            <div class="errordisplay" id="lblHelloMessage" style="padding:10px;display:none;width:500px;"></div>                   
            <br />
            <br />
            <!-- Number Calculator Sample -->
            <strong>Number Addition<br />
            </strong><span style="font-size: 8pt">protected decimal AddNumbers(decimal Num1,decimal
                Num2) &nbsp; (try entering an invalid number)</span><strong><br />
                </strong>
            <hr />
            <asp:TextBox ID="txtAdd1" runat="server" Width="44px" Style="text-align: right;">12</asp:TextBox>
            +
            <asp:TextBox ID="txtAdd2" runat="server" Width="48px" Style="text-align: right;">11</asp:TextBox>
            <br />
            <input id="btnMethod" type="button" value="Add Numbers" onclick="AddNumbers();" style="width: 123px" />

            <br />
            =
            <asp:TextBox ID="txtAddResult" runat="server" Width="102px" Style="text-align: right;"></asp:TextBox><br />
            <br />
            <br />
            <strong>Server Exception Marshalling<br />
            </strong><span style="font-size: 8pt">protected string ThrowException(string Value)
                &nbsp; &nbsp; (checked checkbox causes a server Exception)</span><br />
            <hr />
            <br />
            <asp:TextBox runat="server" ID="txtSaySomething" textmode=MultiLine width="400" Height="50"></asp:TextBox>
            <br />
            <input id="btnSaySomething" type="button" value="Say Something" 
                    onclick="ThrowException();"
                style="width: 123px" /><asp:CheckBox ID="chkException" runat="server" Text="Throw Exception" /><br />
            <br />
                        &nbsp;
            <hr />
            This sample demonstrates using the wwMethodCallback control to make callbacks
                        to the
            current page. 
                        <br />
                        The control creates a page proxy instance that maps the server methods and simply
                        lets you call these<br />
                        server methods from the with code like:
                        <br />
                        <br />
                        Proxy.Helloworld("Rick",OnSuccess,OnFailure);<br />
                        <br />
                        Callbacks are asynchronous and the callback handler is called on success with the
                        result value. On<br />
                        the server you simply implement a method on the Page (or any user or custom control
                        or HttpHandler)<br />
                        and the control manages the rest.<br />
            <br />
            1. Create a method on the server with standard parameters<br />
            2. Mark up the method with the [CallbackMethod] attribute<br />
            3. Hook up your clientside code with code like this:<br />
            &nbsp;&nbsp; Proxy.Helloworld<span style="font-size: 10pt; font-family: Monospace">(document.getElementById('txtName').value,OnSuccess,OnFailure);<br />
            </span><em><span style="font-size: 8pt">&nbsp; &nbsp; (Parameters: ControlId,Method
                name,Parameter1,Parameter2...n,CallbackMethod)</span></em><br />
            4. Implement the callback method which receives a result value or object.<br />
            <br />
            Values are passed back from the server as typed values using JSON, so you can pass
            back strings, dates, number,<br />
            bools as well as many objects, including Arrays, IList based objects, &nbsp;DataTables
            and DataRows. You can also pass<br />
            typed values and objects back to the server. Currently simple types, simple hierarchical
            objects and array and IList<br />
            style collections are supported. DataTable/DataRow are not support for return trips
            at this point.<br />
            <br />
            Finally this example also demonstrates how an exception is returned from the server.
            Check the Throw Exception<br />
            checkbox, then click on the Pass Entity button. When checked the server code throws
            an explicit exception which<br />
            is returned as a special Exception object you can check for. Typically your client
            script callback handler checks<br />
            for a null result and can check for Result.IsCallbackError to see if an error occured
            on the server. Result.Message<br />
            returns the error message.</div>
        <hr />
        <uc1:ShowCode ID="ShowSource" runat="server" CodeFile="~/SimpleMethodCallbacks.aspx.cs">
        </uc1:ShowCode>
        <uc1:ShowCode ID="ShowAspx" runat="server" CodeFile="~/SimpleMethodCallbacks.aspx"
            Text="Show ASPX"></uc1:ShowCode>
        &nbsp;

<script type="text/javascript">
    var CustomerObject = null;
function HelloWorld()
{
    // *** 'Callback' is a global proxy object with the same name
    // *** as the wwCallbackMethod control. The proxy exposes
    // *** each of the server's methods with the same signature
    // *** plus client callback and error javascript functions
    Proxy.HelloWorld($w("txtName").value,Helloworld_Callback,OnPageError);
}

function Helloworld_Callback(Result)
{
    // *** Using wwControl fx
    var wwCtl = new wwControl("lblHelloMessage");
    wwCtl.setText(Result);
    wwCtl.fadein(3);

    // *** Simple Alert
    //alert(Result);
    
    // *** Simple raw DOM Assignment
//    var Ctl = $w("lblHelloMessage");
//    Ctl.style.display = "";
//    Ctl.innerHTML = Result;
    
}
function HideHelloWorld()
{
    var wwCtl = new wwControl("lblHelloMessage");
    
    // *** Fade out: Hide when complete, step 4, start at opacity 1,use visibility (leave space for control)
    wwCtl.fadeout(true,4,1,true);
}

function AddNumbers()
{
  var Num1 = $w('txtAdd1').value-0; // force to number
  var Num2 = $w('txtAdd2').value-0;
        
  // *** Note that you have to pass the value as a number!
  // *** Server Signature: decimal AddNumbers(decimal,decimal)
  Proxy.AddNumbers(Num1,Num2,AddNumbers_Callback,OnPageError);
}
function AddNumbers_Callback(Result)
{
    $w("txtAddResult").value = Result;
}
        
function ThrowException()
{   
    // *** This callback causes an exception on the server. 
    // *** The exception gets returned and routed to the error
    // *** handler (OnPageError), which receives a simple exception object

    // *** Manually create a new callback object and call dynamically without the proxy
    var ExceptionCallback = Proxy_GetCallback();
    
    ExceptionCallback.postbackMode = "Post";  // pass form vars so we can read the checkbox selection on server
    ExceptionCallback.callMethod('ThrowException',[$w("txtSaySomething").value],ThrowException_Callback,OnPageError);
}
function ThrowException_Callback(Result)
{        
    // *** Otherwise we have our result value - in this case a string
    alert(Result);
}

function ManualMethodCall() 
{
    // *** Example how to manually make a remove method call without the 
    // *** built in proxy. This allows dynamic method invokation.
    //
    // *** Create the callback class and pass the ID of the Callback control
    var Manual = new wwCallbackMethod("Proxy");

    // *** serverUrl is not required for same page callbacks. But allows calling another page in the app
    // *** which essentially allows you to create a 'service page'
    Manual.serverUrl = "SimpleMethodcallbacks.aspx";  

    // *** You can route a callback to a specific server control. By default 
    // *** calls are routed to the Page, but any user or custom control can
    // *** be referenced by it's server ID. Optional - 
    // Manual.targetControlId = "Page"

    // *** You can specify how data is posted back. Get, Post, PostNoViewState and parameters only (default)      
    Manual.postbackMode = "PostMethodParametersOnly";

    // *** Call method: Method name, Array of parameters, Callback and Error handlers
    var v = $w("txtName").value;
    Manual.callMethod("HelloWorld",[v],ManualMethodCall_Callback,OnPageError);
}
function ManualMethodCall_Callback(Result)
{
    alert("*** Server Response:\r\n" + Result);
}
function OnPageError(Result)
{
    alert("*** OnPageError captured a CallbackMethod error:\r\n\r\n" + Result.message)
}
</script>
    </form>
</body>
</html>
