Option Explicit On 
Option Strict On
Option Compare Text

Imports System.Data.SqlClient

Public Class ViewParentPage
	Inherits System.Web.UI.Page
	Protected WithEvents txtPageName As System.Web.UI.WebControls.TextBox
	Protected WithEvents txtPageHeading As System.Web.UI.WebControls.TextBox
	Protected WithEvents btnUpdate As System.Web.UI.WebControls.Button
	Protected WithEvents lblID As System.Web.UI.WebControls.Label
	Protected WithEvents daViewPage As System.Data.SqlClient.SqlDataAdapter
	Protected WithEvents daViewChildPages As System.Data.SqlClient.SqlDataAdapter
	Protected WithEvents cmdVCP_Select As System.Data.SqlClient.SqlCommand
	Protected WithEvents DsViewChildPages1 As AllSaints.dsViewChildPages
	Protected WithEvents dlChildPages As System.Web.UI.WebControls.DataList
	Protected WithEvents rfvPageName As System.Web.UI.WebControls.RequiredFieldValidator
	Protected WithEvents rfvPageHeading As System.Web.UI.WebControls.RequiredFieldValidator
	Protected WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand
	Protected WithEvents SqlUpdateCommand1 As System.Data.SqlClient.SqlCommand
	Protected WithEvents DsViewPage1 As AllSaints.dsViewPage
	Protected WithEvents Label1 As System.Web.UI.WebControls.Label
	Protected WithEvents hlnkManageLinks As System.Web.UI.WebControls.HyperLink
    Protected WithEvents imgTop As System.Web.UI.WebControls.Image
    Protected WithEvents imgRight As System.Web.UI.WebControls.Image
    Protected WithEvents TopPicture As System.Web.UI.HtmlControls.HtmlInputFile
    Protected WithEvents RightPicture As System.Web.UI.HtmlControls.HtmlInputFile
    Protected WithEvents btnTopPicUpdate As System.Web.UI.WebControls.Button
    Protected WithEvents btnRightPicUpdate As System.Web.UI.WebControls.Button
    Protected WithEvents BoostersTR As System.Web.UI.HtmlControls.HtmlTableRow
    Protected WithEvents lnkRightPicRemove1 As System.Web.UI.WebControls.LinkButton
    Protected WithEvents lnkTopPicRemove1 As System.Web.UI.WebControls.LinkButton
    Protected WithEvents RemoveTPic As System.Web.UI.WebControls.LinkButton
    Protected WithEvents RemoveRPic As System.Web.UI.WebControls.LinkButton
    Protected WithEvents Conn As System.Data.SqlClient.SqlConnection

#Region " Web Form Designer Generated Code "

	'This call is required by the Web Form Designer.
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Conn = New System.Data.SqlClient.SqlConnection
        Me.daViewPage = New System.Data.SqlClient.SqlDataAdapter
        Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand
        Me.SqlUpdateCommand1 = New System.Data.SqlClient.SqlCommand
        Me.daViewChildPages = New System.Data.SqlClient.SqlDataAdapter
        Me.cmdVCP_Select = New System.Data.SqlClient.SqlCommand
        Me.DsViewChildPages1 = New AllSaints.dsViewChildPages
        Me.DsViewPage1 = New AllSaints.dsViewPage
        CType(Me.DsViewChildPages1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.DsViewPage1, System.ComponentModel.ISupportInitialize).BeginInit()
        '
        'Conn
        '
        Me.Conn.ConnectionString = "data source=localhost;initial catalog=allsaints;persist security info=False;user " & _
        "id=sa;workstation id=GALAXY;packet size=4096"
        '
        'daViewPage
        '
        Me.daViewPage.SelectCommand = Me.SqlSelectCommand1
        Me.daViewPage.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "GetPageInfo", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("ID", "ID"), New System.Data.Common.DataColumnMapping("PageName", "PageName"), New System.Data.Common.DataColumnMapping("Body", "Body"), New System.Data.Common.DataColumnMapping("BodyHeading", "BodyHeading"), New System.Data.Common.DataColumnMapping("TopPicture", "TopPicture"), New System.Data.Common.DataColumnMapping("RightPicture", "RightPicture"), New System.Data.Common.DataColumnMapping("ParentID", "ParentID")})})
        Me.daViewPage.UpdateCommand = Me.SqlUpdateCommand1
        '
        'SqlSelectCommand1
        '
        Me.SqlSelectCommand1.CommandText = "[GetPageInfo]"
        Me.SqlSelectCommand1.CommandType = System.Data.CommandType.StoredProcedure
        Me.SqlSelectCommand1.Connection = Me.Conn
        Me.SqlSelectCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(10, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
        Me.SqlSelectCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@PageID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(10, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
        '
        'SqlUpdateCommand1
        '
        Me.SqlUpdateCommand1.CommandText = "[SetPageInfo]"
        Me.SqlUpdateCommand1.CommandType = System.Data.CommandType.StoredProcedure
        Me.SqlUpdateCommand1.Connection = Me.Conn
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(10, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@PageID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(10, Byte), CType(0, Byte), "ID", System.Data.DataRowVersion.Current, Nothing))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@PageName", System.Data.SqlDbType.VarChar, 255, "PageName"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Body", System.Data.SqlDbType.VarChar, 255, "Body"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@BodyHeading", System.Data.SqlDbType.VarChar, 255, "BodyHeading"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@TopPicture", System.Data.SqlDbType.VarChar, 255, "TopPicture"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RightPicture", System.Data.SqlDbType.VarChar, 25, "RightPicture"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@ParentID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(10, Byte), CType(0, Byte), "ParentID", System.Data.DataRowVersion.Current, Nothing))
        '
        'daViewChildPages
        '
        Me.daViewChildPages.SelectCommand = Me.cmdVCP_Select
        Me.daViewChildPages.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "GetChildPages", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("ID", "ID"), New System.Data.Common.DataColumnMapping("PageName", "PageName"), New System.Data.Common.DataColumnMapping("Body", "Body"), New System.Data.Common.DataColumnMapping("BodyHeading", "BodyHeading"), New System.Data.Common.DataColumnMapping("TopPicture", "TopPicture"), New System.Data.Common.DataColumnMapping("RightPicture", "RightPicture"), New System.Data.Common.DataColumnMapping("ParentID", "ParentID")})})
        '
        'cmdVCP_Select
        '
        Me.cmdVCP_Select.CommandText = "[GetChildPages]"
        Me.cmdVCP_Select.CommandType = System.Data.CommandType.StoredProcedure
        Me.cmdVCP_Select.Connection = Me.Conn
        Me.cmdVCP_Select.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(10, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
        Me.cmdVCP_Select.Parameters.Add(New System.Data.SqlClient.SqlParameter("@PageID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(10, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
        '
        'DsViewChildPages1
        '
        Me.DsViewChildPages1.DataSetName = "dsViewChildPages"
        Me.DsViewChildPages1.Locale = New System.Globalization.CultureInfo("en-US")
        '
        'DsViewPage1
        '
        Me.DsViewPage1.DataSetName = "dsViewPage"
        Me.DsViewPage1.Locale = New System.Globalization.CultureInfo("en-US")
        CType(Me.DsViewChildPages1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.DsViewPage1, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub

	Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
		'CODEGEN: This method call is required by the Web Form Designer
		'Do not modify it using the code editor.
		InitializeComponent()
	End Sub

#End Region

	Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Session("Links") Is "Top" Then
            Session.Remove("Links")
        End If

        Dim sConStr As String = ConfigurationSettings.AppSettings("sConnection")
        Conn.ConnectionString = sConStr

        If Not Page.IsPostBack Then
            'get the value of ID from the url posing and give it to 
            '@PageID Parameter for parent page
            daViewPage.SelectCommand.Parameters("@PageID").Value = Convert.ToInt32(Request("ID"))
            '@PageID Parameter for child pages
            daViewChildPages.SelectCommand.Parameters("@PageID").Value = Convert.ToInt32(Request("ID"))
            'fill the parent page dataset
            daViewPage.Fill(DsViewPage1)
            'fill the children page dataset
            daViewChildPages.Fill(DsViewChildPages1)
            'bind all the controls of this page
            Page.DataBind()
        End If

        Dim sOWScript As String = "<script language='javascript' src='OpenWindow.js'></script>"

        '<TEST LINES>
        Session("Links") = "Top"
        '<TEST LINES>

        Page.RegisterClientScriptBlock("OWScript", sOWScript)
        hlnkManageLinks.Attributes.Add("onclick", "return OpenWindow(" & lblID.Text & ");")


        RemoveTPic.Attributes.Add("onclick", "return confirm('Are you sure ?');")
        RemoveRPic.Attributes.Add("onclick", "return confirm('Are you sure ?');")

        If Not Request("ID") Is Nothing Then
            Dim id As Integer = Convert.ToInt32(Request("ID"))
            If id = 42 Then
                BoostersTR.Visible = True
            Else
                BoostersTR.Visible = False
            End If
        End If

	End Sub

	Private Sub dlChildPages_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dlChildPages.SelectedIndexChanged

	End Sub

	Private Sub dlChildPages_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlChildPages.ItemCommand
		'redirect to specific child page with the Child ID
		If e.CommandName = "PageName" Then
			Response.Redirect("MgrContent.aspx?ID=" & CType(e.CommandArgument, String))
		End If
	End Sub

	Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
		With daViewPage.UpdateCommand
			.Parameters("@PageID").Value = CType(Trim(lblID.Text), Integer)
			.Parameters("@PageName").Value = txtPageName.Text
			.Parameters("@Body").Value = ""
			.Parameters("@BodyHeading").Value = txtPageHeading.Text
			.Parameters("@TopPicture").Value = "images/top_img.gif"
			.Parameters("@RightPicture").Value = "images/right_img.jpg"
            .Parameters("@ParentID").Value = 0

			If Conn.State = ConnectionState.Closed Then Conn.Open()
			.ExecuteNonQuery()
		End With
		'get the value of ID from the url posing and give it to 
		'@PageID Parameter for parent page
		daViewPage.SelectCommand.Parameters("@PageID").Value = Convert.ToInt32(Request("ID"))
		'@PageID Parameter for child pages
		daViewChildPages.SelectCommand.Parameters("@PageID").Value = Convert.ToInt32(Request("ID"))
		'fill the parent page dataset
		daViewPage.Fill(DsViewPage1)
		'fill the children page dataset
		daViewChildPages.Fill(DsViewChildPages1)
        Page.DataBind()

        '<SAQIB ADDED LINES>
        If Conn.State = ConnectionState.Open Then Conn.Close()
        '<SAQIB ADDED LINES/>
	End Sub

    Private Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Unload
        '<SAQIB ADDED LINES>
        If Conn.State = ConnectionState.Open Then Conn.Close()
        '<SAQIB ADDED LINES/>
    End Sub

    '<Sohaib>

    Private Sub imgRight_PreRender(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imgRight.PreRender
        Dim img As System.Web.UI.WebControls.Image = CType(sender, System.Web.UI.WebControls.Image)
        If img.ImageUrl.Length > 0 Then
            img.ImageUrl = img.ImageUrl.Substring(2)
            img.ImageUrl = "..\" & img.ImageUrl
            img.Visible = True
        Else
            img.ImageUrl = "..\contents\images\noimage.jpg"
            img.Visible = False
        End If

        '<SAQIB ADDED LINES>
        If Conn.State = ConnectionState.Open Then Conn.Close()
    End Sub

    Private Sub imgTop_PreRender(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imgTop.PreRender
        Dim img As System.Web.UI.WebControls.Image = CType(sender, System.Web.UI.WebControls.Image)
        If img.ImageUrl.Length > 0 Then
            img.ImageUrl = img.ImageUrl.Substring(2)
            img.ImageUrl = "..\" & img.ImageUrl
            img.Visible = True
        Else
            img.ImageUrl = "..\contents\images\noimage.jpg"
            img.Visible = False
        End If

        '<SAQIB ADDED LINES>
        If Conn.State = ConnectionState.Open Then Conn.Close()
    End Sub

    '</Sohaib>
    Private Sub btnTopPicUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTopPicUpdate.Click

        Dim cmd As New SqlCommand
        Dim fid As Integer = Convert.ToInt32(Request("ID"))

        With TopPicture.PostedFile
            If .FileName <> "" Then
                Select Case Trim(.ContentType.ToLower)

                    Case "image/gif", "image/pjpg", "image/jpeg", "image/pjpeg", "image/jpg", "image/png", "image/x-png", "image/bmp"
                        Try
                            Dim sFilename As String = System.IO.Path.GetExtension(.FileName)
                            sFilename = "\contents\images\" & fid & "top" & sFilename
                            .SaveAs(Server.MapPath("..") & sFilename)

                            With cmd
                                .CommandText = "Update as_Pages Set TopPicture='." & sFilename & "' where ID=" & fid
                                .CommandType = CommandType.Text
                                .Connection = Conn
                                If Conn.State = ConnectionState.Closed Then
                                    Dim sConStr As String = ConfigurationSettings.AppSettings("sConnection")
                                    Try
                                        Conn.ConnectionString = sConStr
                                    Catch ex As Exception
                                    End Try
                                    Conn.Open()
                                End If
                                .ExecuteNonQuery()
                                If Conn.State = ConnectionState.Open Then Conn.Close()
                                Response.Redirect(Request.Url.ToString())
                            End With

                        Catch ex As Exception
                            Response.Write(ex.ToString)

                        End Try

                    Case Else
                        'nothing
                End Select
            End If
        End With

        '<SAQIB ADDED LINES>
        If Conn.State = ConnectionState.Open Then Conn.Close()

    End Sub

    Private Sub btnRightPicUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRightPicUpdate.Click

        Dim cmd As New SqlCommand
        Dim fid As Integer = Convert.ToInt32(Request("ID"))

        With RightPicture.PostedFile
            If .FileName <> "" Then
                Select Case Trim(.ContentType.ToLower)

                    Case "image/gif", "image/pjpg", "image/jpeg", "image/pjpeg", "image/jpg", "image/bmp", "image/png", "image/x-png"
                        Try
                            Dim sFilename As String = System.IO.Path.GetExtension(.FileName)
                            sFilename = "\contents\images\" & fid & "right" & sFilename
                            .SaveAs(Server.MapPath("..") & sFilename)

                            With cmd
                                .CommandText = "Update as_Pages Set RightPicture='." & sFilename & "' where ID=" & fid
                                .CommandType = CommandType.Text
                                .Connection = Conn
                                If Conn.State = ConnectionState.Closed Then
                                    Dim sConStr As String = ConfigurationSettings.AppSettings("sConnection")
                                    Try
                                        Conn.ConnectionString = sConStr
                                    Catch ex As Exception
                                    End Try
                                    Conn.Open()
                                End If
                                .ExecuteNonQuery()
                                If Conn.State = ConnectionState.Open Then Conn.Close()
                                Response.Redirect(Request.Url.ToString())
                            End With

                        Catch ex As Exception
                            Response.Write(ex.ToString)

                        End Try

                    Case Else
                        'nothing
                End Select
            End If
        End With

        '<SAQIB ADDED LINES>
        If Conn.State = ConnectionState.Open Then Conn.Close()
    End Sub

    Private Sub RemoveTPic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveTPic.Click
        Dim cmd As New SqlCommand
        Dim fid As Integer = Convert.ToInt32(Request("ID"))

        With cmd
            .CommandText = "Select TopPicture from as_Pages where ID=" & fid
            .CommandType = CommandType.Text
            .Connection = Conn
            If Conn.State = ConnectionState.Closed Then
                Dim sConStr As String = ConfigurationSettings.AppSettings("sConnection")
                Try
                    Conn.ConnectionString = sConStr
                Catch ex As Exception
                End Try
                Conn.Open()
            End If
            Dim sFilename As String = CType(.ExecuteScalar, String)

            If sFilename.Length > 0 Then
                Try
                    sFilename = sFilename.Substring(1)
                    'System.IO.File.Delete(Server.MapPath("..") & sFilename)
                Catch ex As Exception
                    'do nothing
                End Try
                .CommandText = "Update as_Pages Set TopPicture='' where ID=" & fid
                .CommandType = CommandType.Text
                .Connection = Conn
                If Conn.State = ConnectionState.Closed Then
                    Dim sConStr As String = ConfigurationSettings.AppSettings("sConnection")
                    Try
                        Conn.ConnectionString = sConStr
                    Catch ex As Exception
                    End Try
                    Conn.Open()
                End If
                .ExecuteNonQuery()
                If Conn.State = ConnectionState.Open Then Conn.Close()
                Response.Redirect(Request.Url.ToString())
            End If

        End With

        '<SAQIB ADDED LINES>
        If Conn.State = ConnectionState.Open Then Conn.Close()
    End Sub

    Private Sub RemoveRPic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveRPic.Click
        Dim cmd As New SqlCommand
        Dim fid As Integer = Convert.ToInt32(Request("ID"))

        With cmd

            .CommandText = "Select RightPicture from as_Pages where ID=" & fid
            .CommandType = CommandType.Text
            .Connection = Conn
            If Conn.State = ConnectionState.Closed Then
                Dim sConStr As String = ConfigurationSettings.AppSettings("sConnection")
                Try
                    Conn.ConnectionString = sConStr
                Catch ex As Exception
                End Try
                Conn.Open()
            End If
            Dim sFilename As String = CType(.ExecuteScalar, String)

            If sFilename.Length > 0 Then
                Try
                    sFilename = sFilename.Substring(1)
                    'System.IO.File.Delete(Server.MapPath("..") & sFilename)
                Catch ex As Exception
                    'do nothing
                End Try
                .CommandText = "Update as_Pages Set RightPicture='' where ID=" & fid
                .CommandType = CommandType.Text
                .Connection = Conn
                If Conn.State = ConnectionState.Closed Then
                    Dim sConStr As String = ConfigurationSettings.AppSettings("sConnection")
                    Try
                        Conn.ConnectionString = sConStr
                    Catch ex As Exception
                    End Try
                    Conn.Open()
                End If
                .ExecuteNonQuery()
                If Conn.State = ConnectionState.Open Then Conn.Close()
                Response.Redirect(Request.Url.ToString())
            End If
        End With

        '<SAQIB ADDED LINES>
        If Conn.State = ConnectionState.Open Then Conn.Close()
    End Sub
End Class



