
Partial Class Management
    Inherits System.Web.UI.Page

#Region "******************** ページイベント ********************"

    ''' <summary>
    ''' ページロード時
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        '追加ボタンに確認ダイアログをハンドル
        Insert.Attributes("onclick") = "return Dialog_YN(""追加してもよろしいですか？"")"
    End Sub

#End Region

#Region "***************** コントロールイベント *****************"

    Protected Sub MST質問_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles MST質問.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If (Not e.Row.FindControl("btn_更新") Is Nothing) Then
                '更新ボタンに確認ダイアログをハンドル
                CType(e.Row.FindControl("btn_更新"), Button).Attributes("onclick") = "return Dialog_YN(""更新してもよろしいですか？"")"
            End If

            If (Not e.Row.FindControl("btn_削除") Is Nothing) Then
                '削除ボタンに確認ダイアログをハンドル
                CType(e.Row.FindControl("btn_削除"), Button).Attributes("onclick") = "return Dialog_YN(""削除してもよろしいですか？"")"
            End If

            If (Not e.Row.FindControl("Generate") Is Nothing) Then
                '対象者データの展開ボタンに確認ダイアログをハンドル
                CType(e.Row.FindControl("Generate"), Button).Attributes("onclick") = "return Dialog_YN(""対象者データを展開してもよろしいですか？"")"
            End If
        End If
    End Sub

    Protected Sub Insert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Insert.Click
        Using spm As New StoredProcedureManager("Q_管理_アンケート_INSERT")
            spm.Exec()
        End Using

        MST質問.DataBind()
        Response.Redirect(Request.RawUrl.ToString)
    End Sub

    Protected Sub MST質問_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles MST質問.RowCommand
        Err_Msg.Text = Nothing

        Select Case e.CommandName
            Case "Generate"
                Dim row_index As Integer = Integer.Parse(e.CommandArgument)
                Dim year As String
                Dim term As String

                With MST質問.Rows(row_index)
                    year = CType(.Cells(3).FindControl("授業年度Label"), Label).Text
                    term = CType(.Cells(4).FindControl("期間LabelDisp"), Label).Text
                End With

                If String.IsNullOrEmpty(year) OrElse String.IsNullOrEmpty(term) Then
                    Err_Msg.Text = "「履修年度」「期間」を入力してください。"
                    Exit Sub
                End If

                GenerateAnswerRecords(MST質問.DataKeys(row_index).Values(0), Integer.Parse(year), term)

                Response.Redirect(Request.RawUrl.ToString)

            Case Else

        End Select
    End Sub

    Protected Sub MST質問_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles MST質問.RowUpdating
        If UpdateChoices(CType(MST質問.Rows(e.RowIndex).Cells(9).FindControl("MST回答"), GridView)) Then
            Exit Sub
        End If

        Err_Msg.Text = "回答の更新処理にてエラーが発生しました。"
    End Sub

#End Region

#Region "******************* Sub及びFunction ********************"

    Private Function UpdateChoices(ByVal gv As GridView) As Boolean
        Try
            For row_index As Integer = 0 To gv.Rows.Count - 1
                gv.UpdateRow(row_index, False)
            Next
        Catch ex As Exception
            Return False
        End Try

        Return True
    End Function

    Private Sub GenerateAnswerRecords(ByVal qNo As Integer, ByVal year As Integer, ByVal term As String)
        Using spm As New StoredProcedureManager("Q_管理_回答生成")
            spm.AddPrmIn("@QNO", Data.SqlDbType.Int, qNo)
            spm.AddPrmIn("@授業年度", Data.SqlDbType.SmallInt, year)
            spm.AddPrmIn("@期間", Data.SqlDbType.VarChar, 6, term)
            spm.Exec()
        End Using
    End Sub

#End Region

End Class