Public Class Keypad
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then
            ' ポストバック時
        Else
            ' 初期表示時
            Page.Header.Title = MyProperty.システムＭ.選挙コード名称

            ' セッションタイムアウトチェック
            If IsNothing(Session(MyConstant.SessionKey.投票者ID)) Then
                Throw New Exception(MyMessage.ログ.セッションタイムアウト)
            End If

            ' クエリ文字列から選挙IDをセッションに保存
            If Not IsNothing(Page.Request.QueryString.Get(MyConstant.QueryStringKey.選挙ID)) Then
                Session(MyConstant.SessionKey.選挙ID) = Page.Request.QueryString.Get(MyConstant.QueryStringKey.選挙ID)                
            End If

            ' 選挙名の表示
            Using myDataBase As MyDataBase = New MyDataBase
                LtlSenkyoMei.Text = myDataBase.Get選挙名称By選挙ID(Session(MyConstant.SessionKey.選挙ID))
            End Using

            ' 候補者Mを取得する。
            Dim ds As DataSet1 = New DataSet1
            Using myDataBase As MyDataBase = New MyDataBase
                myDataBase.Fill候補者MBy選挙ID(ds, Session(MyConstant.SessionKey.選挙ID))
            End Using

            ' 投票数を表示
            lblNumOfVotes.Text = CType(Session(MyConstant.SessionKey.投票数), String) & "票の投票"

            ' キーパッドのHTMLタグ動的出力
            Dim tag As StringBuilder = New StringBuilder
            Dim arr As String()
            arr = New String() {"あ", "か", "さ", "た", "な", "は", "ま", "や", "ら", "わ", _
                                "い", "き", "し", "ち", "に", "ひ", "み", "", "り", "", _
                                "う", "く", "す", "つ", "ぬ", "ふ", "む", "ゆ", "る", "を", _
                                "え", "け", "せ", "て", "ね", "へ", "め", "", "れ", "", _
                                "お", "こ", "そ", "と", "の", "ほ", "も", "よ", "ろ", "ん"}
            Dim i As Integer = 0
            For Each key As String In arr
                If i Mod 10 = 0 Then
                    tag.Append("<tr>")
                End If
                If isExist候補者(key, ds) Then
                    Dim url As String = "KohoList.aspx?" + MyConstant.QueryStringKey.選挙ID + "=" + Session(MyConstant.SessionKey.選挙ID) + _
                           "&" + MyConstant.QueryStringKey.名前検索キー + "=" + HttpUtility.UrlEncode(key)
                    tag.Append("<td class=""white""><a href=""" + url + """>" + key + "</a>")
                Else
                    tag.Append("<td class=""gray"">" + key)
                End If
                tag.Append("</td>")
                tag.Append(System.Environment.NewLine)
                If i Mod 10 = 9 Then
                    tag.Append("</tr>")
                    tag.Append(System.Environment.NewLine)
                End If
                i += 1
            Next
            LtlKeypad.Text = tag.ToString()

            ' 「指定しない」ボタン
            LtlAllButton.Text = "<a href=""KohoList.aspx?" + _
                       MyConstant.QueryStringKey.選挙ID + "=" + Session(MyConstant.SessionKey.選挙ID) + _
                       "&" + MyConstant.QueryStringKey.名前検索キー + "=all"" class=""btn"">指定しない</a>"
        End If
    End Sub

    Private Function isExist候補者(ByVal key As String, ByVal ds As DataSet1) As Boolean
       For Each row As DataSet1.候補者MRow In ds.候補者M
            If MyUtility.Get頭文字(row.候補者名称かな) = key Then
                Return True
            End If
        Next
        Return False
    End Function

End Class