Webブラウザー版 検索アプリケーション
Visual Basic index.aspx
Visual Basic index.aspx.vb
Option Explicit On
Option Strict On
Option Compare Binary
Imports System.Globalization
Imports Postan2.jp.co.est.webservice
Public Class Index
Inherits System.Web.UI.Page
Private Enum ZipCodeType As Integer
OTHER = -1
OLD_ZIP_CODE = 0 '3 or 5桁の郵便番号
NEW_ZIP_CODE = 1 '7桁の郵便番号
End Enum
Private Enum ListViewType As Integer
NewZip = 1
OldZip = 2
Address = 3
End Enum
#Region " Web フォーム デザイナで生成されたコード "
'この呼び出しは Web フォーム デザイナで必要です。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents tblList As System.Web.UI.HtmlControls.HtmlTable
Protected WithEvents hitInput As System.Web.UI.HtmlControls.HtmlInputText
Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
Protected WithEvents htItemList As System.Web.UI.HtmlControls.HtmlTable
Protected WithEvents phItemListBody As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents phItemListHead As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents tblDefault1 As System.Web.UI.HtmlControls.HtmlTable
Protected WithEvents tblDefault2 As System.Web.UI.HtmlControls.HtmlTable
'メモ : 次のプレースホルダ宣言は Web フォーム デザイナで必要です。
'削除および移動しないでください。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
' CODEGEN: このメソッド呼び出しは Web フォーム デザイナで必要です。
' コード エディタを使って変更しないでください。
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Me.IsPostBack = True Then
Me.tblList.Visible = True
Me.tblDefault1.Visible = False
Else
Me.tblList.Visible = False
Me.tblDefault1.Visible = True
End If
If Me.hitInput.Value <> Nothing AndAlso Me.hitInput.Value.Trim() <> "" Then
Me.hitInput.Value = Me.hitInput.Value.Trim()
Call Search(Me.hitInput.Value)
End If
'ViewStateを切る
Me.tblDefault1.EnableViewState = False
Me.tblDefault2.EnableViewState = False
Me.tblList.EnableViewState = False
End Sub
Private Sub Search(ByVal inputValue As String)
Dim CodeType As ZipCodeType
Dim viewType As ListViewType
If (Me.IsZipCode(inputValue, CodeType) = True) Then
Select Case CodeType
Case ZipCodeType.NEW_ZIP_CODE
viewType = ListViewType.NewZip
Case ZipCodeType.OLD_ZIP_CODE
viewType = ListViewType.OldZip
Case Else
viewType = ListViewType.NewZip
End Select
inputValue = Me.ConvNarrow(inputValue)
inputValue = Me.RemoveHyphen(inputValue)
Else
'住所と判断
viewType = ListViewType.Address
End If
Me.FindData(inputValue, viewType)
End Sub
'<summary>
'郵便番号として認識できればtrue
'</summary>
'<param name="strArg"></param>
'<returns></returns>
Private Function IsZipCode(ByVal strArg As String, ByRef Type As ZipCodeType) As Boolean
Dim bRet As Boolean
Dim ciJajp As CultureInfo = New CultureInfo("ja-JP")
Dim strWork As String
Type = ZipCodeType.OTHER 'とりあえず[OTHER]を入れておく
strArg = Me.ConvNarrow(strArg) '半角に変換
strArg = Me.RemoveHyphen(strArg) 'ハイフンを取除く
bRet = Me.CheckZip(strArg, strWork)
If bRet = True Then '文字数のチェック
Select Case strArg.Length
Case 3, 5
Type = ZipCodeType.OLD_ZIP_CODE
Case 7
Type = ZipCodeType.NEW_ZIP_CODE
Case Else
bRet = False
End Select
End If
Return bRet
End Function
'<summary>
'半角に変換
'</summary>
'<returns></returns>
Private Function ConvNarrow(ByVal arg As String) As String
'VBにある[StrConv]を呼びます。
Dim strNarrow As String
Dim ciJajp As CultureInfo = New CultureInfo("ja-JP")
strNarrow = Strings.StrConv(arg, VbStrConv.Narrow, ciJajp.LCID)
Return strNarrow
End Function
Private Function RemoveHyphen(ByVal arg As String) As String
Dim result As String
result = arg.Replace("-", "").Replace("ー", "")
Return result
End Function
Private Function CheckZip(ByVal postcode As String, ByRef errmsg As String) As Boolean
Dim word As String
errmsg = ""
For i As Integer = 0 To postcode.Length - 1 Step 1
word = postcode.Substring(i, 1)
Select Case word
Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "-"
Case Else
errmsg = "認識できない文字が入っています。入力し直してください。"
End Select
Next i
If errmsg = "" Then
Return True
Else
Return False
End If
End Function
Private Sub FindData(ByVal targetword As String, ByVal viewType As ListViewType)
Dim finddata As FindList03 = Nothing
Dim wsSearchEngine03 As SearchEngine03 = New jp.co.est.webservice.SearchEngine03
Dim bResult As Boolean
Dim strMatch As String
Const NOT_FOUND As String = "該当する住所が見つかりませんでした。"
'Me.lblInfo.Text = ""
Select Case viewType
Case ListViewType.NewZip
finddata = wsSearchEngine03.FromZipCode2(targetword, 0, 100)
Case viewType.OldZip
finddata = wsSearchEngine03.FromOldZipCode2(targetword, 0, 100)
Case viewType.Address
finddata = wsSearchEngine03.FromAddress2(targetword, 0, 100)
'bResult = wsSearchEngine03.FromAddressDecremental(targetword, 0, 100, finddata, strMatch)
'If bResult = True Then
'Me.lblInfo.Text = "「" & strMatch & "」でヒットしました"
'End If
End Select
If True = finddata.IsSuccess Then
If finddata.FindCount > 0 Then
If finddata.FindCount <> finddata.TotalFindCount Then
Me.lblMessage.Text = finddata.TotalFindCount.ToString() & "件中 1-" & finddata.FindCount.ToString() & "を表示"
Else
Me.lblMessage.Text = finddata.FindCount.ToString() & "件が見つかりました。"
End If
Call Me.SetFindList(Me.htItemList, viewType, finddata)
Else
Me.lblMessage.Text = NOT_FOUND
End If
ElseIf finddata.ErrorMessage <> Nothing AndAlso finddata.ErrorMessage <> "" Then
Me.lblMessage.Text = finddata.ErrorMessage
Else
Me.lblMessage.Text = NOT_FOUND
End If
End Sub
Private Sub SetFindList(ByVal table As HtmlTable, ByVal viewType As ListViewType, ByVal finddata As FindList03)
Call Me.SetFinDataHeader(table, viewType)
Call Me.SetFinDataItems(table, viewType, finddata)
End Sub
Private Sub SetFinDataHeader(ByVal table As HtmlTable, ByVal viewType As ListViewType)
Const NEWZIP As String = "新郵便番号"
Const OLDZIP As String = "旧郵便番号"
Const ADDRESS As String = "住所"
Const YOMI As String = "よみ"
Dim tr As HtmlTableRow
tr = New HtmlTableRow
tr.Attributes.Add("class", "Head")
Select Case viewType
Case ListViewType.NewZip
Me.CreateCell(tr, NEWZIP, False)
Me.CreateCell(tr, OLDZIP, False)
Me.CreateCell(tr, ADDRESS, False)
Me.CreateCell(tr, YOMI, False)
Case ListViewType.OldZip
Me.CreateCell(tr, OLDZIP, False)
Me.CreateCell(tr, NEWZIP, False)
Me.CreateCell(tr, ADDRESS, False)
Me.CreateCell(tr, YOMI, False)
Case ListViewType.Address
Me.CreateCell(tr, ADDRESS, False)
Me.CreateCell(tr, YOMI, False)
Me.CreateCell(tr, NEWZIP, False)
Me.CreateCell(tr, OLDZIP, False)
End Select
Call table.Rows.Add(tr)
End Sub
Private Sub SetFinDataItems(ByVal table As HtmlTable, ByVal viewType As ListViewType, ByVal finddata As FindList03)
Dim list As jp.co.est.webservice.FindItem03() = finddata.Item
Dim tr As HtmlTableRow
For i As Integer = 0 To list.Length - 1 Step 1
tr = New HtmlTableRow
tr.Attributes.Add("class", "Item")
table.Rows.Add(tr)
finddata.Item(i).ZipCode = finddata.Item(i).ZipCode.Insert(3, "-")
If finddata.Item(i).OldZipCode.Length = 5 Then
finddata.Item(i).OldZipCode = finddata.Item(i).OldZipCode.Insert(3, "-")
End If
Select Case viewType
Case ListViewType.NewZip
CreateCell(tr, finddata.Item(i).ZipCode, False)
CreateCell(tr, finddata.Item(i).OldZipCode, False)
CreateCell(tr, finddata.Item(i).Kanji1 & finddata.Item(i).Kanji2 & finddata.Item(i).Kanji3, True)
CreateCell(tr, finddata.Item(i).Yomi1 & finddata.Item(i).Yomi2 & finddata.Item(i).Yomi3, True)
Case ListViewType.OldZip
CreateCell(tr, finddata.Item(i).OldZipCode, False)
CreateCell(tr, finddata.Item(i).ZipCode, False)
CreateCell(tr, finddata.Item(i).Kanji1 & finddata.Item(i).Kanji2 & finddata.Item(i).Kanji3, True)
CreateCell(tr, finddata.Item(i).Yomi1 & finddata.Item(i).Yomi2 & finddata.Item(i).Yomi3, True)
Case ListViewType.Address
CreateCell(tr, finddata.Item(i).Kanji1 & finddata.Item(i).Kanji2 & finddata.Item(i).Kanji3, True)
CreateCell(tr, finddata.Item(i).Yomi1 & finddata.Item(i).Yomi2 & finddata.Item(i).Yomi3, True)
CreateCell(tr, finddata.Item(i).ZipCode, False)
CreateCell(tr, finddata.Item(i).OldZipCode, False)
End Select
Next i
End Sub
Private Sub CreateCell(ByVal tRow As HtmlTableRow, ByVal text As String, ByVal wrap As Boolean)
Dim td As HtmlTableCell = New HtmlTableCell
tRow.Cells.Add(td)
td.NoWrap = Not wrap
td.InnerText = text 'InnerTextは自動でHTMLエンコード
End Sub
Private Sub tblDefault2_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles tblDefault2.PreRender
Me.tblDefault2.Visible = Me.tblDefault1.Visible 'tblDefault1を真似る
End Sub
End Class