Imports System.Data
Imports AppVariable

Partial Class uc_SearchStudent
    Inherits System.Web.UI.UserControl

#Region "************************* 定数 *************************"

    '検索
    Private Const SESSION_SEARCH_GAKUNEN As String = "COM_Search_Gakusei_Gakunen"               '学年
    Private Const SESSION_SEARCH_CLASS As String = "COM_Search_Gakusei_Zaigaku_Class"           'クラス
    Private Const SESSION_SEARCH_GAKUSEKINO As String = "COM_Search_Gakuseki_NO"                '学籍番号
    Private Const SESSION_SEARCH_SIMEI As String = "COM_Search_Gakusei_Name"                    '氏名/ふりがな
    Private Const SESSION_SEARCH_KATEI As String = "COM_Search_Gakusei_Katei_CD"                '学部
    Private Const SESSION_SEARCH_GAKKA As String = "COM_Search_Gakusei_Gakka_CD"                '学科
    Private Const SESSION_SEARCH_SENKO As String = "COM_Search_Gakusei_Senko_CD"                '専攻
    Private Const SESSION_SEARCH_ZAISEKIKUBUN As String = "COM_Search_Gakusei_Zaiseki_Kubun"    '在籍区分
    Private Const SESSION_SEARCH_SOTUNEN As String = "COM_Search_Gakusei_Sotunen"               '卒業年度
    Private Const SESSION_SEARCH_SYUSYOKUSAKI As String = "COM_Search_Gakusei_Syusyokusaki"     '就職先

    '一覧
    Private Const SESSION_LIST_PAGE_INDEX As String = "SearchStudent_PageIndex"             'ページの位置
    Private Const SESSION_LIST_SELECTED_INDEX As String = "SearchStudent_SelectedIndex"     '選択行の位置

#End Region

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

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '---------------------------------------------------------------
        '検索条件をセット
        '---------------------------------------------------------------
        'If Not IsPostBack OrElse Not IsNothing(Session("ポートフォリオの検索条件を復元")) Then
        If Not IsPostBack Then
            'ドロップダウンリストを作成(選択肢が変動するもののみ)
            sql_dd_学科_select(Session(SESSION_SEARCH_KATEI))
            sql_dd_専攻_select(Session(SESSION_SEARCH_KATEI), Session(SESSION_SEARCH_GAKKA))

            '検索条件を復元
            '・1セクション目
            dd_検索_学年.SelectedValue = Session(SESSION_SEARCH_GAKUNEN)
            dd_検索_クラス.SelectedValue = Session(SESSION_SEARCH_CLASS)
            txt_検索_学籍NO.Text = Session(SESSION_SEARCH_GAKUSEKINO)
            '・2セクション目
            txt_検索_名前.Text = Session(SESSION_SEARCH_SIMEI)
            '・3セクション目
            dd_検索_課程.SelectedValue = Session(SESSION_SEARCH_KATEI)
            dd_検索_学科.SelectedValue = Session(SESSION_SEARCH_GAKKA)
            dd_検索_専攻.SelectedValue = Session(SESSION_SEARCH_SENKO)
            '・4セクション目
            dd_検索_在籍区分.SelectedValue = Session(SESSION_SEARCH_ZAISEKIKUBUN)
            txt_検索_卒業年度.Text = Session(SESSION_SEARCH_SOTUNEN)
            '・5セクション目
            txt_検索_就職先.Text = Session(SESSION_SEARCH_SYUSYOKUSAKI)

            'Session.Remove("ポートフォリオの検索条件を復元")    '※どうしてもうまくいかない為
        End If

        If Not Session(SESSION_LIST_SELECTED_INDEX) Is Nothing Then
            gv_学生.SelectedIndex = Session(SESSION_LIST_SELECTED_INDEX)
        Else
            gv_学生.SelectedIndex = -1
        End If
    End Sub

#End Region

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

    Protected Sub sql_dd_在籍区分_Init(sender As Object, e As EventArgs) Handles sql_dd_在籍区分.Init
        sql_dd_在籍区分_select()
    End Sub

    Protected Sub sql_dd_学年_Init(sender As Object, e As EventArgs) Handles sql_dd_学年.Init
        sql_dd_学年_select()
    End Sub

    Protected Sub sql_dd_クラス_Init(sender As Object, e As EventArgs) Handles sql_dd_クラス.Init
        sql_dd_クラス_select()
    End Sub

    Protected Sub sql_dd_課程_Init(sender As Object, e As EventArgs) Handles sql_dd_課程.Init
        sql_dd_課程_select()
    End Sub

    Protected Sub sql_dd_専攻_Init(ByVal sender As Object, ByVal e As EventArgs) Handles sql_dd_専攻.Init
        sql_dd_専攻_select(dd_検索_課程.SelectedValue, dd_検索_学科.SelectedValue)
    End Sub

    Protected Sub sql_学生_Init(sender As Object, e As EventArgs) Handles sql_学生.Init
        sql_学生_select()
    End Sub

    Protected Sub dd_検索_学年_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dd_検索_学年.SelectedIndexChanged
        sql_dd_クラス_select()
        dd_検索_クラス.SelectedValue = ""
    End Sub

    Protected Sub dd_検索_課程_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dd_検索_課程.SelectedIndexChanged
        sql_dd_学科_select(dd_検索_課程.SelectedValue)
        dd_検索_学科.SelectedValue = ""
        dd_検索_専攻.SelectedValue = ""
    End Sub

    Protected Sub dd_検索_学科_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dd_検索_学科.SelectedIndexChanged
        sql_dd_専攻_select(dd_検索_課程.SelectedValue, dd_検索_学科.SelectedValue)
        dd_検索_専攻.SelectedValue = ""
    End Sub

    Protected Sub gv_学生_SelectedIndexChanged(sender As Object, e As EventArgs) Handles gv_学生.SelectedIndexChanged
        '検索リストから選択された授業情報を保持
        Session(S_COM_Selected_Gakuseki_No) = CType(gv_学生.SelectedRow.FindControl("lb_学籍NO"), Label).Text

        '選択行の位置を保持
        Session(SESSION_LIST_SELECTED_INDEX) = gv_学生.SelectedIndex

        Response.Redirect(Request.RawUrl.ToString)
    End Sub

    Protected Sub gv_学生_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gv_学生.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            'リストクリック時選択状態にする
            Dim instance As ClientScriptManager = Page.ClientScript
            Dim button As LinkButton = CType(e.Row.FindControl("lbtn_選択"), LinkButton)
            e.Row.Attributes("onclick") = instance.GetPostBackClientHyperlink(button, "")
        End If
    End Sub

    Protected Sub btn_検索_Click(sender As Object, e As EventArgs) Handles btn_検索.Click
        'ページと選択行の位置を初期化
        gv_学生.PageIndex = 0
        Session.Remove(SESSION_LIST_PAGE_INDEX)
        Session.Remove(SESSION_LIST_SELECTED_INDEX)

        '検索条件をクリア
        Session(SESSION_SEARCH_GAKUSEKINO) = ""
        Session(SESSION_SEARCH_SIMEI) = ""
        Session(SESSION_SEARCH_GAKUNEN) = ""
        Session(SESSION_SEARCH_KATEI) = ""
        Session(SESSION_SEARCH_GAKKA) = ""
        Session(SESSION_SEARCH_SENKO) = ""
        Session(SESSION_SEARCH_ZAISEKIKUBUN) = ""
        Session(SESSION_SEARCH_CLASS) = ""
        Session(SESSION_SEARCH_SOTUNEN) = ""
        Session(SESSION_SEARCH_SYUSYOKUSAKI) = ""

        '検索条件をセッションに保存
        Session(SESSION_SEARCH_GAKUSEKINO) = txt_検索_学籍NO.Text
        Session(SESSION_SEARCH_SIMEI) = txt_検索_名前.Text
        Session(SESSION_SEARCH_GAKUNEN) = dd_検索_学年.SelectedValue
        Session(SESSION_SEARCH_KATEI) = dd_検索_課程.SelectedValue
        Session(SESSION_SEARCH_GAKKA) = dd_検索_学科.SelectedValue
        Session(SESSION_SEARCH_SENKO) = dd_検索_専攻.SelectedValue
        Session(SESSION_SEARCH_ZAISEKIKUBUN) = dd_検索_在籍区分.SelectedValue
        Session(SESSION_SEARCH_CLASS) = dd_検索_クラス.SelectedValue
        Session(SESSION_SEARCH_SOTUNEN) = txt_検索_卒業年度.Text
        Session(SESSION_SEARCH_SYUSYOKUSAKI) = txt_検索_就職先.Text

        Response.Redirect(Request.RawUrl.ToString)
    End Sub

    Protected Sub btn_検索クリア_Click(sender As Object, e As EventArgs) Handles btn_検索クリア.Click
        '検索条件をクリア
        Session(SESSION_SEARCH_GAKUSEKINO) = ""
        Session(SESSION_SEARCH_SIMEI) = ""
        Session(SESSION_SEARCH_GAKUNEN) = ""
        Session(SESSION_SEARCH_KATEI) = ""
        Session(SESSION_SEARCH_GAKKA) = ""
        Session(SESSION_SEARCH_SENKO) = ""
        Session(SESSION_SEARCH_ZAISEKIKUBUN) = "在籍"
        Session(SESSION_SEARCH_CLASS) = ""
        Session(SESSION_SEARCH_SOTUNEN) = ""
        Session(SESSION_SEARCH_SYUSYOKUSAKI) = ""

        Response.Redirect(Request.RawUrl.ToString)
    End Sub

    Protected Sub gv_学生_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles gv_学生.PageIndexChanging
        '選択行を解除
        gv_学生.SelectedIndex = -1
        'ページを移動
        gv_学生.PageIndex = e.NewPageIndex

        'ページの位置を保持
        Session(SESSION_LIST_PAGE_INDEX) = gv_学生.PageIndex.ToString
    End Sub

    Protected Sub gv_学生_DataBound(sender As Object, e As EventArgs) Handles gv_学生.DataBound
        'ページの位置を復元
        gv_学生.PageIndex = Session(SESSION_LIST_PAGE_INDEX)
    End Sub

#End Region

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

    Private Sub sql_学生_select()
        '初期値を設定
        If IsNothing(Session(SESSION_SEARCH_ZAISEKIKUBUN)) Then
            Session(SESSION_SEARCH_ZAISEKIKUBUN) = "在籍"
        End If

        Dim sds As SqlDataSource = sql_学生
        WebControlsUtil.DeclareSP(sds, "共通_学生検索_SELECT")
        With sds
            .SelectParameters.Add("学籍NO", DbType.String, Session(SESSION_SEARCH_GAKUSEKINO))
            .SelectParameters.Add("氏名ふり", DbType.String, Session(SESSION_SEARCH_SIMEI))
            .SelectParameters.Add("課程CD", DbType.String, Session(SESSION_SEARCH_KATEI))
            .SelectParameters.Add("学科CD", DbType.String, Session(SESSION_SEARCH_GAKKA))
            .SelectParameters.Add("専攻CD", DbType.String, Session(SESSION_SEARCH_SENKO))
            .SelectParameters.Add("在籍区分", DbType.String, Session(SESSION_SEARCH_ZAISEKIKUBUN))
            .SelectParameters.Add("学年", DbType.Int32, Session(SESSION_SEARCH_GAKUNEN))
            .SelectParameters.Add("PORTALID", DbType.String, Session(S_COM_PortalID))
            .SelectParameters.Add("クラス", DbType.String, Session(SESSION_SEARCH_CLASS))
            .SelectParameters.Add("卒業年度", DbType.Int32, Session(SESSION_SEARCH_SOTUNEN))
            .SelectParameters.Add("就職先", DbType.String, Session(SESSION_SEARCH_SYUSYOKUSAKI))
        End With
    End Sub

    Private Sub sql_dd_学年_select()
        Dim sds As SqlDataSource = sql_dd_学年
        WebControlsUtil.DeclareSP(sds, "共通_カスタム_DDL")
        With sds
            .SelectParameters.Add("TABLE", DbType.String, "VW_共通_学生")
            .SelectParameters.Add("GROUPBY", DbType.String, "学年")
            .SelectParameters.Add("ORDERBY", DbType.String, "学年")
            .SelectParameters.Add("DESC", DbType.String, "")
        End With
    End Sub

    Private Sub sql_dd_クラス_select()
        Dim sds As SqlDataSource = sql_dd_クラス
        WebControlsUtil.DeclareSP(sds, "共通_クラス_DDL")
        With sds
            .SelectParameters.Add("学年", DbType.Int32, dd_検索_学年.SelectedValue)
        End With
    End Sub

    Private Sub sql_dd_在籍区分_select()
        Dim sds As SqlDataSource = sql_dd_在籍区分
        WebControlsUtil.DeclareSP(sds, "共通_カスタム_DDL")
        With sds
            .SelectParameters.Add("TABLE", DbType.String, "VW_共通_学生")
            .SelectParameters.Add("GROUPBY", DbType.String, "在籍区分")
            .SelectParameters.Add("ORDERBY", DbType.String, "在籍区分")
            .SelectParameters.Add("DESC", DbType.String, "")
        End With
    End Sub

    Private Sub sql_dd_課程_select()
        Dim sds As SqlDataSource = sql_dd_課程
        WebControlsUtil.DeclareSP(sds, "共通_課程_DDL")
    End Sub

    Private Sub sql_dd_学科_select(ByVal prm課程CD As Object)
        Dim sds As SqlDataSource = sql_dd_学科
        WebControlsUtil.DeclareSP(sds, "共通_学科_DDL")
        With sds
            .SelectParameters.Add("課程CD", DbType.String, prm課程CD)
        End With
    End Sub

    Private Sub sql_dd_専攻_select(ByVal prm課程CD As Object, ByVal prm学科CD As Object)
        Dim sds As SqlDataSource = sql_dd_専攻
        WebControlsUtil.DeclareSP(sds, "共通_専攻_DDL")
        With sds
            .SelectParameters.Add("課程CD", DbType.String, prm課程CD)
            .SelectParameters.Add("学科CD", DbType.String, prm学科CD)
        End With
    End Sub

#End Region

End Class
