<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CustomerList.aspx.cs" Inherits="CustomerList"
    EnableEventValidation="false" EnableViewState="false"%>
<%@Register Namespace="Westwind.Web.Controls" assembly="Westwind.AjaxToolkit"  TagPrefix="ww" %>
<%@ Register Src="ShowCode.ascx" TagName="ShowCode" TagPrefix="uc1" %>
<!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>Northwind Customer List</title>
    <link href="WestWind.css" rel="stylesheet" type="text/css" />

</head>
<body  onload="GetCustomerList()">
    <form id="form1" runat="server">
        <div>
            <h1>wwMethodCallback Northwind Customer List</h1>
        <a href="default.aspx">Home</a> | <a href="CustomerList.aspx">Reset Page</a><br />
        <br />
        <br />
        <center>
            &nbsp;<table class="blackborder" style="background: #eeeeee; text-align: left; height: 459px;"
                cellpadding="0">
                <tr class="gridheader">
                    <td colspan="2" class="gridheader" height="24">
                        &nbsp;<span id="DialogTitle">Customer List</span></td>
                </tr>
                <tr>
                    <td valign="top" style="height: 435px">
                        <asp:ListBox ID="lstCustomers" runat="server" Height="442px" Width="300px" 
                                     onchange="CustomerCallback.GetCustomerRow(this.value,GetCustomerRowCallback);">
                        </asp:ListBox></td>
                    <td valign="top" style="padding-left: 20px; padding-top: 10px; padding-right: 10px;">
                        <div id="ErrorDisplay" class="errordisplay" style="display:none;padding:5px;margin-bottom:5px;" ></div>
                        <asp:Label ID="Label2" runat="server" Text="Name:" Width="263px"></asp:Label><br />
                        <asp:TextBox ID="txtContactName" runat="server" Width="350px"></asp:TextBox><br />
                        <br />
                        <asp:Label ID="Label1" runat="server" Text="Company:"></asp:Label><br />
                        <asp:TextBox ID="txtCompany" runat="server" Width="350px"></asp:TextBox><br />
                        <br />
                        Address:<br />
                        <asp:TextBox ID="txtAddress" runat="server" Width="350px" Height="53px" TextMode="MultiLine"></asp:TextBox><br />
                        <br />
                        City:<br />
                        <asp:TextBox ID="txtCity" runat="server" Width="350px"></asp:TextBox><br />
                        <br />
                        <table>
                            <tr>
                                <td>
                                    Region:
                                    <br />
                                    <asp:TextBox ID="txtRegion" runat="server" Width="74px"></asp:TextBox></td>
                                <td>
                                    &nbsp;&nbsp;Postal Code:<br />
                                    &nbsp;&nbsp;<asp:TextBox ID="txtPostalCode" runat="server" Width="192px"></asp:TextBox><br />
                                </td>
                            </tr>
                        </table>
                        <br />
                        Country:<br />
                        <asp:TextBox ID="txtCountry" runat="server" Width="350px" /><br />
                        <hr />
                        &nbsp;
                        <input type="button" value=" Save Customer " accesskey="S"
                               onclick="CustomerCallback.SaveCustomer(SaveCustomerCallback,OnPageError);"
                               style="width: 150px" size="" />
                        <input type="button" value="Refresh List " 
                               onclick="GetCustomerList()"
                               style="width: 150px" size="" /></td>
                </tr>
            </table>
        </center>
        <br />
        <hr />
        <blockquote>
            This example demonstrates Method Callbacks into the same page with the wwMethodCallback
            control. By using server side methods marked up with [CallbackMethod] you are allowing
            client side code to call these server methods and return the results as typed values
            or objects via the JSON encoding format.
            <br />
            <br />
            Client side code fires requests against the server with code like this:<br />
            <br />
            <strong><span style="font-family: Courier New">CustomerCallback.GetCustomerEntity('ALFKI',GetCustomerRow_Callback,OnPageError);</span></strong></blockquote>
        <blockquote>
            The control returns the results of the server method calls via JSON serialization,
            which attempts to maintain the object structure and typing across the callback from
            client to server and back.
        </blockquote>
        <blockquote>
            In this sample all data is retrieved via Callbacks. The DataTable is returned for
            the listbox and a Customer Entity object for the population of the Customer data fields. The data can then be updated on the page and is saved by calling the SaveCustomer()
            method which takes no parameters. Rather than passing the Entity back (which is
            possible) the server side code instead accesses the POST buffer and ASP.NET server
            control values to retrieve the values and store them onto the server side entity
            object which is then validated and saved.<br />
            <br />
            The server side code fires exceptions on errors - the exceptions are also serialized
            into an Exception object and returned to the client. The client can check for an
            error flag - if (Result.IsCallbackError) to determine error status. This works for
            operational failures as well as Application level errors such as business rule validations.
            For example, if you leave out the name or company fields a validation error is triggered
            which is sent back to the client.</blockquote>
        <hr />
        <uc1:ShowCode ID="ShowCode1" runat="server" CodeFile="CustomerList.aspx.cs"
            Text="Show Code" />
        <uc1:ShowCode ID="ShowCode2" runat="server" CodeFile="CustomerList.aspx"
            Text="Show ASPX" />
        <br />
        
        <!-- Callback control -->
        <ww:wwMethodCallback runat="server" ID="CustomerCallback" 
                         PostBackMode="POST" 
                         ScriptLocationType="WebResource" />


<script type="text/javascript">
var CustRow = null;
function GetCustomerList()
{
    CustomerCallback.GetCustomerList(GetCustomerListCallback,OnPageError);
}
function GetCustomerListCallback(Result) 
{    
    // *** Returns a 'DataTable' object
    var Table = Result;    
    
    // *** Get a reference to the list for easier access
    var List = new wwList("lstCustomers");
    if (List.isNull)
        return;
        
    List.dataTextField = "CompanyName";
    List.dataValueField = "CustomerId";
    List.setData(Table);
    
    // Save selectedIndex
    var SelectedIndex = List.element.selectedIndex;

    // *** Show First Item
    if (List.element.length > 0) 
    {
        if (SelectedIndex < 0)
            SelectedIndex = 0;
            
        List.element.selectedIndex = SelectedIndex;
        List.element.onchange();
    }
}
function GetCustomerRowCallback(Result) 
{
    // *** Return a DataRow from the Server
    CustRow = Result;

    // *** NOTE: You can also return entity objects in the same way
    
    AssignValue( "txtContactName",CustRow.ContactName);
    AssignValue( "txtCompany",CustRow.CompanyName);
    AssignValue( "txtAddress",CustRow.Address);
    AssignValue( "txtCity",CustRow.City);
    AssignValue( "txtCountry",CustRow.Country);
    AssignValue( "txtRegion",CustRow.Region);
    AssignValue( "txtPostalCode",CustRow.PostalCode);
    
    var TitleSpan = document.getElementById("DialogTitle");
    if (TitleSpan) 
        TitleSpan.innerHTML = CustRow.CompanyName;
}
function AssignValue(ControlName,Value)
{
    if (Value == null)
        Value = "";
        
    var Ctl = $w(ControlName);
    if (Ctl)
        Ctl.value = Value;
}
function GetValue(ControlName)
{
    var Ctl = $w(ControlName);
    if (Ctl)
        return Ctl.value;
    
    return null;
}
function SaveCustomerCallback(Result)
{    
    // Force list to refresh - realistically
    // we should update only the one listitem
    GetCustomerList();
    alert("Customer Saved!");
}
function ShowError(message)
{
    var Ctl = new wwControl("ErrorDisplay");
    Ctl.setText( message );    
    Ctl.fadein(5);
    setTimeout(function() { Ctl.fadeout.apply(this,[true,2,1,true]); },5000);
}
function OnPageError(Result)
{
    //alert("*** Callback Error:\r\n\r\n" + Result.message);
    ShowError(Result.message);
}
        </script>
    </form>
</body>
</html>
