Imports System.Data
Imports AppCommon
Imports AppVariable

Partial Class uc_infoCardPublic
    Inherits System.Web.UI.UserControl

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

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session(S_Info_Selected_No) Is Nothing Then
            Session(S_Info_Selected_No) = 0
        End If

        frm_info.DataSource = GetData()
        frm_info.DataBind()

        If Not CType(frm_info.FindControl("gv_掲示対象"), GridView) Is Nothing Then
            CType(frm_info.FindControl("gv_掲示対象"), GridView).DataSource = GetData_掲示対象()
            CType(frm_info.FindControl("gv_掲示対象"), GridView).DataBind()
        End If

        gv_添付.DataSource = GetData_添付()
        gv_添付.DataBind()

        '2018/0/5/25 M.Okabe ADD Start
        If DirectCast(frm_info.DataSource, DataSet).Tables(0).Rows.Count = 0 Then
            gv_添付.Visible = False
        End If

        'エラーメッセージをクリア
        lbl_Error.Text = ""
        '2018/0/5/25 M.Okabe ADD End
    End Sub

#End Region

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

    Protected Sub gv_添付_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv_添付.SelectedIndexChanged
        Dim Index As Integer
        Dim FileName As String
        Dim tFileName As String

        Index = gv_添付.SelectedIndex

        FileName = CType(gv_添付.Rows.Item(Index).FindControl("lb_FILE_NAME"), Label).Text & "." & _
            CType(gv_添付.Rows.Item(Index).FindControl("lb_拡張子"), Label).Text

        tFileName = CType(gv_添付.Rows.Item(Index).FindControl("lbtn_添付名"), LinkButton).Text

        Dim strFile As String
        strFile = Info_FileFolder_Path() & FileName.Replace(vbNullChar, "") '拡張子文字の右にNULL文字が入っているため空文字に変換

        '2018/05/25 M.Okabe MOD
        'If strFile <> "" Then
        If System.IO.File.Exists(strFile) Then
            Response.ContentType = "application/octet-stream"
            Response.HeaderEncoding = System.Text.Encoding.GetEncoding("SHIFT-JIS")     '2014.2.18 IE10～でファイル名が表示できないバグを回避/牧
            Response.AddHeader("Content-Disposition", "attachment; filename=" + tFileName)
            Response.WriteFile(strFile)
            Response.Flush()
            Response.End()
        Else
            'エラーメッセージを表示
            Me.lbl_Error.Text = "添付ファイルのダウンロードに失敗しました。"
            Me.lbl_Error.ForeColor = Drawing.Color.Red
        End If
    End Sub

#End Region

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

    Private Function GetData() As DataSet
        Using spm As New StoredProcedureManager("掲示_掲示個別_SELECT")
            spm.AddPrmIn("@掲示NO", Data.SqlDbType.Int, Session(S_Info_Selected_No))
            spm.Fill()

            Return spm.ds
        End Using
    End Function

    Private Function GetData_掲示対象() As DataSet
        Using spm As New StoredProcedureManager("掲示_掲示個別_掲示対象_SELECT")
            spm.AddPrmIn("@掲示NO", Data.SqlDbType.Int, Session(S_Info_Selected_No))
            spm.Fill()

            Return spm.ds
        End Using
    End Function

    Private Function GetData_添付() As DataSet
        Using spm As New StoredProcedureManager("掲示_掲示個別_添付_SELECT")
            spm.AddPrmIn("@掲示NO", Data.SqlDbType.Int, Session(S_Info_Selected_No))
            spm.Fill()

            Return spm.ds
        End Using
    End Function

#End Region

End Class
