<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">

    Sub Page_Load(sender As Object, e As EventArgs)
      Ajax.Manager.Register(Me, "My.Page", Ajax.Debug.None)
    End Sub
    
    <Ajax.Method> _
    Public Function GetAddress(ByVal strPostalCode As String) As DataTable
      Dim strSQL As String = "Select Top 1 * From tblPostalCode Where Code=?"
      Dim strConnectionString As String = _
        String.Format("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source={0}", _
          Server.MapPath("webdb/PostalCode.mdb"))
      Dim con As New OleDbConnection(strConnectionString)
      Dim da As New OleDbDataAdapter
      Dim cmd As New OleDbCommand(strSQL, con)
      Dim dt As New DataTable()
    
    ' dt.TableName = "Items"
      cmd.Parameters.Add("Code", strPostalCode)
      da.SelectCommand = cmd
      da.Fill(dt)
      If dt.Rows.Count = 0 Then
        Dim dr As DataRow = dt.NewRow
        dr("ken") = "*"
        dr("city") = "*"
        dr("town") = "*"
        dt.Rows.Add(dr)
      End If
      Return dt
    End Function

</script>
<html>
<head>
<style>
TH {
    TEXT-ALIGN: right
}
SPAN.hidden {
    DISPLAY: none
}
SPAN.error {
    DISPLAY: inline; COLOR: black; BACKGROUND-COLOR: pink
}
</style>
<script>
function getAddress(postalCode) {
  My.Page.GetAddress(postalCode, doGetAddressCallBack);
  window.status = "読み込み中です...";
}

function doGetAddressCallBack(result) {
  var table = result.value;
  var msg = document.getElementById("message");
  if (table.Rows[0].Ken != "*") {
    msg.className = "hidden";
    document.getElementById("txtKen").value =
      table.Rows[0].Ken;
    document.getElementById("txtAddress").value =
      table.Rows[0].City +
      table.Rows[0].Town;
    window.status = "読み込み完了!";
  } else {
    msg.className = "error";
  }
}
</script>
</head>
<body>
    <form runat="server">
        <h3>Ajax4.aspx</h3>
        <table>
            <tbody>
                <tr>
                    <td align="right">
                        〒
                    </td>
                    <td>
                        <asp:TextBox id="txtPostalCode" onblur="getAddress(this.value);" runat="server" Width="7em"></asp:TextBox>
                        <span class="hidden" id="message">郵便番号が不正です...再入力してください.</span> 
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        都道府県
                    </td>
                    <td>
                        <asp:TextBox id="txtKen" runat="server" Width="7em"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        住所
                    </td>
                    <td>
                        <asp:TextBox id="txtAddress" runat="server" Width="20em"></asp:TextBox>
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
</body>
</html>