'Option Explicit On 
'Option Strict On
'Option Compare Text

Imports System.Data.SqlClient

Public Class ViewChildPageContacts
    Inherits System.Web.UI.Page
    Protected WithEvents daContactLinks As System.Data.SqlClient.SqlDataAdapter
    Protected WithEvents DsContactLinks1 As AllSaints.dsContactLinks
    Protected WithEvents Conn As System.Data.SqlClient.SqlConnection
    Protected WithEvents cmdCL_Select As System.Data.SqlClient.SqlCommand
    Protected WithEvents cmdCL_Update As System.Data.SqlClient.SqlCommand
    Protected WithEvents dlSubPages As System.Web.UI.WebControls.DataList
    Protected WithEvents daChildPageLinks As System.Data.SqlClient.SqlDataAdapter
    Protected WithEvents cmdCPL_Select As System.Data.SqlClient.SqlCommand
    Protected WithEvents daViewPage As System.Data.SqlClient.SqlDataAdapter
    Protected WithEvents cmdVP_Select As System.Data.SqlClient.SqlCommand
    Protected WithEvents daViewChildPages As System.Data.SqlClient.SqlDataAdapter
    Protected WithEvents cmdVCP_Select As System.Data.SqlClient.SqlCommand
    Protected WithEvents daParentPage As System.Data.SqlClient.SqlDataAdapter
    Protected WithEvents cmdPP_Select As System.Data.SqlClient.SqlCommand
    Protected WithEvents DsViewChildPages1 As AllSaints.dsViewChildPages
    Protected WithEvents DsViewPage1 As AllSaints.dsViewPage
    Protected WithEvents DsParentPage1 As AllSaints.dsParentPage
    Protected WithEvents DsChildPageLinks1 As AllSaints.dsChildPageLinks
    Protected WithEvents txtPageName As System.Web.UI.WebControls.TextBox
    Protected WithEvents cmdCL_Delete As System.Data.SqlClient.SqlCommand
    Protected WithEvents daRelatedContacts As System.Data.SqlClient.SqlDataAdapter
    Protected WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand
    Protected WithEvents SqlInsertCommand1 As System.Data.SqlClient.SqlCommand
    Protected WithEvents SqlUpdateCommand1 As System.Data.SqlClient.SqlCommand
    Protected WithEvents SqlDeleteCommand1 As System.Data.SqlClient.SqlCommand
    Protected WithEvents DsGetRelatedContacts1 As AllSaints.dsGetRelatedContacts
    Protected WithEvents btnTopPicUpdate As System.Web.UI.WebControls.Button
    Protected WithEvents TopPanel As System.Web.UI.WebControls.Panel
    Protected WithEvents TopPicture As System.Web.UI.HtmlControls.HtmlInputFile
    Protected WithEvents pnlPageHeading As System.Web.UI.WebControls.Panel
    Protected WithEvents pnlPageBodyLeft As System.Web.UI.WebControls.Panel
    Protected WithEvents RightPanel As System.Web.UI.WebControls.Panel
    Protected WithEvents imgRight As System.Web.UI.WebControls.Image
    Protected WithEvents txtImageDesc As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnRightPicUpdate As System.Web.UI.WebControls.Button
    Protected WithEvents RightPicture As System.Web.UI.HtmlControls.HtmlInputFile
    Protected WithEvents DataGrid2 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents btnUpdate As System.Web.UI.WebControls.Button
    Protected WithEvents lblPageID As System.Web.UI.WebControls.Label
    Protected WithEvents DG_RelatedLinks As System.Web.UI.WebControls.DataGrid
    Protected WithEvents DG_RelatedContacts As System.Web.UI.WebControls.DataGrid
    Protected WithEvents AddRelContact1 As AllSaints.AddRelatedContact
    Protected WithEvents pnlLinks As System.Web.UI.WebControls.Panel
    Protected WithEvents pnlContacts As System.Web.UI.WebControls.Panel
    Protected WithEvents imgTop As System.Web.UI.WebControls.Image
    Protected WithEvents lnkTopPicRemove As System.Web.UI.WebControls.LinkButton
    Protected WithEvents lnkRightPicRemove As System.Web.UI.WebControls.LinkButton
    'Protected WithEvents EWE1 As EasyWebEditorControl.EasyWebEdit
    Protected WithEvents lblPageHeading As System.Web.UI.WebControls.Label
    Protected WithEvents txtPageHeading As System.Web.UI.WebControls.TextBox
    Protected WithEvents DDL_Teachers As System.Web.UI.WebControls.DropDownList
    Protected WithEvents lnkClassAssignments As System.Web.UI.WebControls.LinkButton
    Protected WithEvents lnkViewCalendar As System.Web.UI.WebControls.LinkButton
    Protected WithEvents hlnkManageLinks As System.Web.UI.WebControls.HyperLink
    Protected WithEvents AddRelLinks1 As AllSaints.AddRelatedLinks
    Protected WithEvents EWE1 As EasyWebEditorControl.EasyWebEdit
    Protected WithEvents Counter1 As AllSaints.Counter

#Region " Web Form Designer Generated Code "
    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.daContactLinks = New System.Data.SqlClient.SqlDataAdapter
        Me.cmdCL_Delete = New System.Data.SqlClient.SqlCommand
        Me.Conn = New System.Data.SqlClient.SqlConnection
        Me.cmdCL_Select = New System.Data.SqlClient.SqlCommand
        Me.cmdCL_Update = New System.Data.SqlClient.SqlCommand
        Me.DsContactLinks1 = New AllSaints.dsContactLinks
        Me.daChildPageLinks = New System.Data.SqlClient.SqlDataAdapter
        Me.cmdCPL_Select = New System.Data.SqlClient.SqlCommand
        Me.daViewPage = New System.Data.SqlClient.SqlDataAdapter
        Me.cmdVP_Select = New System.Data.SqlClient.SqlCommand
        Me.daViewChildPages = New System.Data.SqlClient.SqlDataAdapter
        Me.cmdVCP_Select = New System.Data.SqlClient.SqlCommand
        Me.daParentPage = New System.Data.SqlClient.SqlDataAdapter
        Me.cmdPP_Select = New System.Data.SqlClient.SqlCommand
        Me.DsViewChildPages1 = New AllSaints.dsViewChildPages
        Me.DsViewPage1 = New AllSaints.dsViewPage
        Me.DsParentPage1 = New AllSaints.dsParentPage
        Me.DsChildPageLinks1 = New AllSaints.dsChildPageLinks
        Me.daRelatedContacts = New System.Data.SqlClient.SqlDataAdapter
        Me.SqlDeleteCommand1 = New System.Data.SqlClient.SqlCommand
        Me.SqlInsertCommand1 = New System.Data.SqlClient.SqlCommand
        Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand
        Me.SqlUpdateCommand1 = New System.Data.SqlClient.SqlCommand
        Me.DsGetRelatedContacts1 = New AllSaints.dsGetRelatedContacts
        CType(Me.DsContactLinks1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.DsViewChildPages1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.DsViewPage1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.DsParentPage1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.DsChildPageLinks1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.DsGetRelatedContacts1, System.ComponentModel.ISupportInitialize).BeginInit()
        '
        'daContactLinks
        '
        Me.daContactLinks.DeleteCommand = Me.cmdCL_Delete
        Me.daContactLinks.SelectCommand = Me.cmdCL_Select
        Me.daContactLinks.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "cmdCL_Select", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("ID", "ID"), New System.Data.Common.DataColumnMapping("Page_ID", "Page_ID"), New System.Data.Common.DataColumnMapping("LinkName", "LinkName"), New System.Data.Common.DataColumnMapping("LinkURL", "LinkURL"), New System.Data.Common.DataColumnMapping("LinkPic", "LinkPic")})})
        Me.daContactLinks.UpdateCommand = Me.cmdCL_Update
        '
        'cmdCL_Delete
        '
        Me.cmdCL_Delete.CommandText = "[cmdCL_Delete]"
        Me.cmdCL_Delete.CommandType = System.Data.CommandType.StoredProcedure
        Me.cmdCL_Delete.Connection = Me.Conn
        Me.cmdCL_Delete.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.cmdCL_Delete.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_ID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(10, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
        '
        'Conn
        '
        Me.Conn.ConnectionString = "data source=localhost;initial catalog=allsaints;persist security info=False;user " & _
        "id=sa;workstation id=GALAXY;packet size=4096"
        '
        'cmdCL_Select
        '
        Me.cmdCL_Select.CommandText = "[cmdCL_Select]"
        Me.cmdCL_Select.CommandType = System.Data.CommandType.StoredProcedure
        Me.cmdCL_Select.Connection = Me.Conn
        Me.cmdCL_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.cmdCL_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))
        '
        'cmdCL_Update
        '
        Me.cmdCL_Update.CommandText = "[cmdCL_Update]"
        Me.cmdCL_Update.CommandType = System.Data.CommandType.StoredProcedure
        Me.cmdCL_Update.Connection = Me.Conn
        Me.cmdCL_Update.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.cmdCL_Update.Parameters.Add(New System.Data.SqlClient.SqlParameter("@LinkName", System.Data.SqlDbType.VarChar, 255, "LinkName"))
        Me.cmdCL_Update.Parameters.Add(New System.Data.SqlClient.SqlParameter("@LinkURL", System.Data.SqlDbType.VarChar, 255, "LinkURL"))
        Me.cmdCL_Update.Parameters.Add(New System.Data.SqlClient.SqlParameter("@LinkPic", System.Data.SqlDbType.VarChar, 255, "LinkPic"))
        Me.cmdCL_Update.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_ID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(10, Byte), CType(0, Byte), "ID", System.Data.DataRowVersion.Current, Nothing))
        '
        'DsContactLinks1
        '
        Me.DsContactLinks1.DataSetName = "dsContactLinks"
        Me.DsContactLinks1.Locale = New System.Globalization.CultureInfo("en-US")
        '
        'daChildPageLinks
        '
        Me.daChildPageLinks.SelectCommand = Me.cmdCPL_Select
        Me.daChildPageLinks.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")})})
        '
        'cmdCPL_Select
        '
        Me.cmdCPL_Select.CommandText = "[GetChildPages]"
        Me.cmdCPL_Select.CommandType = System.Data.CommandType.StoredProcedure
        Me.cmdCPL_Select.Connection = Me.Conn
        Me.cmdCPL_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.cmdCPL_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))
        '
        'daViewPage
        '
        Me.daViewPage.SelectCommand = Me.cmdVP_Select
        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")})})
        '
        'cmdVP_Select
        '
        Me.cmdVP_Select.CommandText = "[GetPageInfo]"
        Me.cmdVP_Select.CommandType = System.Data.CommandType.StoredProcedure
        Me.cmdVP_Select.Connection = Me.Conn
        Me.cmdVP_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.cmdVP_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))
        '
        '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))
        '
        'daParentPage
        '
        Me.daParentPage.SelectCommand = Me.cmdPP_Select
        Me.daParentPage.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")})})
        '
        'cmdPP_Select
        '
        Me.cmdPP_Select.CommandText = "[GetPageInfo]"
        Me.cmdPP_Select.CommandType = System.Data.CommandType.StoredProcedure
        Me.cmdPP_Select.Connection = Me.Conn
        Me.cmdPP_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.cmdPP_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")
        '
        'DsParentPage1
        '
        Me.DsParentPage1.DataSetName = "dsParentPage"
        Me.DsParentPage1.Locale = New System.Globalization.CultureInfo("en-US")
        '
        'DsChildPageLinks1
        '
        Me.DsChildPageLinks1.DataSetName = "dsChildPageLinks"
        Me.DsChildPageLinks1.Locale = New System.Globalization.CultureInfo("en-US")
        '
        'daRelatedContacts
        '
        Me.daRelatedContacts.DeleteCommand = Me.SqlDeleteCommand1
        Me.daRelatedContacts.InsertCommand = Me.SqlInsertCommand1
        Me.daRelatedContacts.SelectCommand = Me.SqlSelectCommand1
        Me.daRelatedContacts.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "GetRelContacts", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("ID", "ID"), New System.Data.Common.DataColumnMapping("Page_ID", "Page_ID"), New System.Data.Common.DataColumnMapping("Heading", "Heading"), New System.Data.Common.DataColumnMapping("InfoText", "InfoText")})})
        Me.daRelatedContacts.UpdateCommand = Me.SqlUpdateCommand1
        '
        'SqlDeleteCommand1
        '
        Me.SqlDeleteCommand1.CommandText = "[DelRelContacts]"
        Me.SqlDeleteCommand1.CommandType = System.Data.CommandType.StoredProcedure
        Me.SqlDeleteCommand1.Connection = Me.Conn
        Me.SqlDeleteCommand1.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.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@ContactID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(10, Byte), CType(0, Byte), "ID", System.Data.DataRowVersion.Original, Nothing))
        '
        'SqlInsertCommand1
        '
        Me.SqlInsertCommand1.CommandText = "[InsRelContacts]"
        Me.SqlInsertCommand1.CommandType = System.Data.CommandType.StoredProcedure
        Me.SqlInsertCommand1.Connection = Me.Conn
        Me.SqlInsertCommand1.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.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@PageID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(10, Byte), CType(0, Byte), "Page_ID", System.Data.DataRowVersion.Current, Nothing))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Heading", System.Data.SqlDbType.VarChar, 255, "Heading"))
        Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@InfoText", System.Data.SqlDbType.VarChar, 255, "InfoText"))
        '
        'SqlSelectCommand1
        '
        Me.SqlSelectCommand1.CommandText = "[GetRelContacts]"
        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 = "[SetRelContacts]"
        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("@ContactID", 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("@Heading", System.Data.SqlDbType.VarChar, 255, "Heading"))
        Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@InfoText", System.Data.SqlDbType.VarChar, 255, "InfoText"))
        '
        'DsGetRelatedContacts1
        '
        Me.DsGetRelatedContacts1.DataSetName = "dsGetRelatedContacts"
        Me.DsGetRelatedContacts1.Locale = New System.Globalization.CultureInfo("en-US")
        CType(Me.DsContactLinks1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.DsViewChildPages1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.DsViewPage1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.DsParentPage1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.DsChildPageLinks1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.DsGetRelatedContacts1, 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()
        Dim sConStr As String = ConfigurationSettings.AppSettings("sConnection")
        Try
            Conn.ConnectionString = sConStr
        Catch ex As Exception

        End Try
    End Sub

    Private Sub GetData()

        If CType(Request("Level"), Integer) = 4 Then
            'get the parent id of this child page
            Dim cmd As New SqlCommand("Select ParentID from as_Pages where ID=" & CType(Request("ID"), Integer), 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 intParentID As Integer = CType(cmd.ExecuteScalar(), Integer)

            'get the second level parent id of this child page
            cmd = New SqlCommand("Select ParentID from as_Pages where ID=" & CType(intParentID, Integer), 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
            intParentID = CType(cmd.ExecuteScalar(), Integer)


            'get the second level parent id of this child page
            cmd = New SqlCommand("Select ParentID from as_Pages where ID=" & CType(intParentID, Integer), 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
            intParentID = CType(cmd.ExecuteScalar(), Integer)

            cmd = New SqlCommand("Select ParentID from as_Pages where ID=" & CType(intParentID, Integer), 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
            intParentID = CType(cmd.ExecuteScalar(), Integer)

            'get the parent's child pages to be binded against dlSubPages 'intParentID
            daViewChildPages.SelectCommand.Parameters("@PageID").Value = intParentID
            daViewChildPages.Fill(DsViewChildPages1)

            'get the first level parent id of this child page
            cmd = New SqlCommand("Select ParentID from as_Pages where ID=" & intParentID, 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
            intParentID = CType(cmd.ExecuteScalar(), Integer)

            'get the details about this page's parent's sublings
            daChildPageLinks.SelectCommand.Parameters("@PageID").Value = intParentID
            daChildPageLinks.Fill(DsChildPageLinks1)

            'get the contents links of this child page
            daContactLinks.SelectCommand.Parameters("@PageID").Value = CType(CType(Request("ID"), Integer), Integer)
            daContactLinks.Fill(DsContactLinks1)


            'get the details about this page
            daViewPage.SelectCommand.Parameters("@PageID").Value = CType(Request("ID"), Integer)
            daViewPage.Fill(DsViewPage1)

        ElseIf CType(Request("Level"), Integer) = 3 Then
            'get the parent id of this child page
            Dim cmd As New SqlCommand("Select ParentID from as_Pages where ID=" & CType(Request("ID"), Integer), 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 intParentID As Integer = CType(cmd.ExecuteScalar(), Integer)

            'get the second level parent id of this child page
            cmd = New SqlCommand("Select ParentID from as_Pages where ID=" & CType(intParentID, Integer), 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
            intParentID = CType(cmd.ExecuteScalar(), Integer)


            'get the second level parent id of this child page
            cmd = New SqlCommand("Select ParentID from as_Pages where ID=" & CType(intParentID, Integer), 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
            intParentID = CType(cmd.ExecuteScalar(), Integer)

            'get the parent's child pages to be binded against dlSubPages 'intParentID
            daViewChildPages.SelectCommand.Parameters("@PageID").Value = intParentID
            daViewChildPages.Fill(DsViewChildPages1)

            'get the first level parent id of this child page
            cmd = New SqlCommand("Select ParentID from as_Pages where ID=" & intParentID, 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
            intParentID = CType(cmd.ExecuteScalar(), Integer)

            'get the details about this page's parent's sublings
            daChildPageLinks.SelectCommand.Parameters("@PageID").Value = intParentID
            daChildPageLinks.Fill(DsChildPageLinks1)

            'get the contents links of this child page
            daContactLinks.SelectCommand.Parameters("@PageID").Value = CType(CType(Request("ID"), Integer), Integer)
            daContactLinks.Fill(DsContactLinks1)


            'get the details about this page
            daViewPage.SelectCommand.Parameters("@PageID").Value = CType(Request("ID"), Integer)
            daViewPage.Fill(DsViewPage1)

        ElseIf CType(Request("Level"), Integer) = 2 Then
            'get the parent id of this child page
            Dim cmd As New SqlCommand("Select ParentID from as_Pages where ID=" & CType(Request("ID"), Integer), 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 intParentID As Integer = CType(cmd.ExecuteScalar(), Integer)
            'Response.Write(intParentID)

            'get the second level parent id of this child page
            cmd = New SqlCommand("Select ParentID from as_Pages where ID=" & CType(intParentID, Integer), 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
            intParentID = CType(cmd.ExecuteScalar(), Integer)
            'Response.Write(intParentID)
            'get the parent's child pages to be binded against dlSubPages 'intParentID
            daViewChildPages.SelectCommand.Parameters("@PageID").Value = intParentID
            daViewChildPages.Fill(DsViewChildPages1)

            'get the first level parent id of this child page
            cmd = New SqlCommand("Select ParentID from as_Pages where ID=" & intParentID, 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
            intParentID = CType(cmd.ExecuteScalar(), Integer)
            'Response.Write(intParentID)
            '???
            'cmd = New SqlCommand("Select ParentID from as_Pages where ID=" & CType(Request("ID"), Integer), Conn)
            'If Conn.State = ConnectionState.Closed Then Conn.Open()
            'intParentID = CType(cmd.ExecuteScalar(), Integer)

            'get the details about this page's parent's sublings
            daChildPageLinks.SelectCommand.Parameters("@PageID").Value = intParentID
            daChildPageLinks.Fill(DsChildPageLinks1)


            'get the first level parent id of this child page
            'cmd = New SqlCommand("Select ParentID from as_Pages where ID=" & CType(Request("ID"), Integer), Conn)
            'If Conn.State = ConnectionState.Closed Then Conn.Open()
            'intParentID = CType(cmd.ExecuteScalar(), Integer)

            'get the contents links of this child page
            daContactLinks.SelectCommand.Parameters("@PageID").Value = CType(CType(Request("ID"), Integer), Integer)
            daContactLinks.Fill(DsContactLinks1)


            'get the details about this page
            daViewPage.SelectCommand.Parameters("@PageID").Value = CType(Request("ID"), Integer)
            daViewPage.Fill(DsViewPage1)

        ElseIf (CType(Request("Level"), Integer) = 1) Then
            'get the second level parent id of this child page
            Dim cmd As New SqlCommand("Select ParentID from as_Pages where ID=" & CType(Request("ID"), Integer), 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 intParentID As Integer = CType(cmd.ExecuteScalar(), Integer)
            'Response.Write(intParentID)
            'get the parent's child pages to be binded against dlSubPages 'intParentID
            daViewChildPages.SelectCommand.Parameters("@PageID").Value = intParentID
            daViewChildPages.Fill(DsViewChildPages1)

            'get the first level parent id of this child page
            cmd = New SqlCommand("Select ParentID from as_Pages where ID=" & intParentID, 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
            intParentID = CType(cmd.ExecuteScalar(), Integer)
            'Response.Write(intParentID)
            'get the details about this page's parent's sublings
            daChildPageLinks.SelectCommand.Parameters("@PageID").Value = intParentID
            daChildPageLinks.Fill(DsChildPageLinks1)


            'get the first level parent id of this child page
            'cmd = New SqlCommand("Select ParentID from as_Pages where ID=" & CType(Request("ID"), Integer), Conn)
            'If Conn.State = ConnectionState.Closed Then Conn.Open()
            'intParentID = CType(cmd.ExecuteScalar(), Integer)

            'get the contents links of this child page
            daContactLinks.SelectCommand.Parameters("@PageID").Value = CType(CType(Request("ID"), Integer), Integer)
            daContactLinks.Fill(DsContactLinks1)


            'get the details about this page
            daViewPage.SelectCommand.Parameters("@PageID").Value = CType(Request("ID"), Integer)
            daViewPage.Fill(DsViewPage1)

        Else
            'get the details about this page
            daViewPage.SelectCommand.Parameters("@PageID").Value = CType(Request("ID"), Integer)
            daViewPage.Fill(DsViewPage1)

            'get the second level parent id of this child page
            Dim cmd As New SqlCommand("Select ParentID from as_Pages where ID=" & CType(Request("ID"), Integer), 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 intParentID As Integer = CType(cmd.ExecuteScalar(), Integer)

            'get the details about this child page
            daChildPageLinks.SelectCommand.Parameters("@PageID").Value = intParentID
            daChildPageLinks.Fill(DsChildPageLinks1)

            'get the parent's child pages to be binded against dlSubPages 'intParentID
            daViewChildPages.SelectCommand.Parameters("@PageID").Value = CType(Request("ID"), Integer)
            daViewChildPages.Fill(DsViewChildPages1)

            'get the contents links of this child page
            daContactLinks.SelectCommand.Parameters("@PageID").Value = CType(Request("ID"), Integer)
            daContactLinks.Fill(DsContactLinks1)

        End If
        Page.DataBind()

    End Sub

    Private Sub GetContactData()
        daRelatedContacts.SelectCommand.Parameters("@PageID").Value = CType(Request("ID"), Integer)
        daRelatedContacts.Fill(DsGetRelatedContacts1)
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim sConStr As String = ConfigurationSettings.AppSettings("sConnection")
        Try
            Conn.ConnectionString = sConStr
        Catch ex As Exception
        End Try
        If Request("Level") Is Nothing Then
            Counter1.Visible = False
        Else
            Counter1.PageID = CType(Request("ID"), Integer)
        End If
        'let try if this page is calender of level 3 then goto edit calender
        Try
            Dim i As Integer = CType(Request("Level").ToString, Integer)
            If i >= 1 Then
                Dim cmdCalendar As New SqlCommand
                With cmdCalendar
                    .CommandText = "Select PageType from as_Pages where ID=" & Request("ID").ToString
                    .CommandType = CommandType.Text
                    .Connection = Conn
                    If Conn.State = ConnectionState.Closed Then Conn.Open()
                    Dim sType As String = .ExecuteScalar().ToString.Trim
                    If sType = "Calendar" Then
                        Response.Redirect("ViewCalender.aspx?Level=1&View=Calendar&ID=" & Request("ID").ToString, True)
                        Exit Sub
                    End If
                End With
            End If
        Catch ex As Exception

        End Try

        'If Conn.State <> ConnectionState.Open Then Conn.ConnectionString = sConStr
        AddRelContact1.PageID = CType(Request("ID"), Integer)
        AddRelLinks1.PageID = CType(Request("ID"), Integer)
        lnkRightPicRemove.Attributes.Add("onclick", "return confirm('Are you sure ?');")
        lnkTopPicRemove.Attributes.Add("onclick", "return confirm('Are you sure ?');")

        If Not Page.IsPostBack Then

            Call GetContactData()
            Call GetData()
            If CType(Request("Level"), Integer) <> 0 Then
                TopPanel.Visible = True
                RightPanel.Visible = True
                pnlPageBodyLeft.Visible = True
                pnlPageHeading.Visible = True
                pnlContacts.Visible = True
                pnlLinks.Visible = True
                lblPageHeading.Visible = True
                EWE1.Visible = True
                EWE1.HTMLValue = CType(DsViewPage1.GetPageInfo.Rows(0).Item("Body"), String)
                lnkViewCalendar.Visible = False
                lnkClassAssignments.Visible = False

                'if this is a class page then show some extra class controls
                Select Case Trim(CType(DsViewPage1.GetPageInfo.Rows(0).Item("PageType"), String))
                    Case "Class"
                        lnkClassAssignments.CommandArgument = CType(DsViewPage1.GetPageInfo.Rows(0).Item("ID"), String)
                        lnkViewCalendar.Visible = True
                        lnkClassAssignments.Visible = True

                        'fill the techers' dropdown list
                        DDL_Teachers.Items.Insert(0, "Select Class Teacher")
                        Dim cmdTeachers As New SqlCommand()
                        With cmdTeachers
                            .CommandType = CommandType.StoredProcedure
                            .Connection = Conn
                            If Conn.State = ConnectionState.Closed Then Conn.Open()
                            'first get this class' any previous teacher id so automatically selected in dropdownlist
                            .CommandText = "GetClassTeacher"
                            .Parameters.Add("@ClassPageID", SqlDbType.Int)
                            .Parameters("@ClassPageID").Value = CType(DsViewPage1.GetPageInfo.Rows(0).Item("ID"), Integer)
                            Dim tname As String = ""
                            tname = CType(CType(.ExecuteScalar(), Object), String)

                            'clear all previous parameters
                            .Parameters.Clear()
                            .CommandText = "GetTeacherFullNames"
                            Dim i As Integer = 1
                            Dim tdr As SqlDataReader = .ExecuteReader
                            While tdr.Read
                                Dim li As New ListItem()
                                li.Text = CType(tdr.Item("FullName"), String)
                                li.Value = CType(tdr.Item("LoginName"), String)
                                DDL_Teachers.Items.Add(li)
                                If Not IsNothing(tname) AndAlso tname.Trim = li.Value.Trim Then
                                    li.Selected = True
                                End If
                                i += 1
                            End While
                            tdr.Close()
                        End With
                        DDL_Teachers.Visible = True
                    Case Else
                        DDL_Teachers.Visible = False
                End Select
            End If
        End If

        '<TEST LINES>
        Session("Links") = "Botom"
        '<TEST LINES>
        Session("Links") = "Bottom"
        Dim sOWScript As String = "<script language='javascript' src='OpenWindow.js'></script>"
        Page.RegisterClientScriptBlock("OWScript", sOWScript)
        hlnkManageLinks.Attributes.Add("onclick", "return OpenWindow(" & lblPageID.Text & ");")

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

    End Sub
    '****************************************************************************************************
    '****************************************************************************************************
    Private Sub DDL_Teachers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DDL_Teachers.SelectedIndexChanged
        Dim cmd As New SqlCommand()
        If DDL_Teachers.SelectedIndex > 0 Then
            With cmd
                .CommandText = "SetClassTeacher"
                .CommandType = CommandType.StoredProcedure
                .Connection = Conn

                .Parameters.Add("@ClassPageID", SqlDbType.Int, 4)
                .Parameters("@ClassPageID").Value = CType(lblPageID.Text, Integer)

                .Parameters.Add("@TeacherLoginID", SqlDbType.VarChar, 100)
                .Parameters("@TeacherLoginID").Value = DDL_Teachers.SelectedItem.Value
                If Conn.State = ConnectionState.Closed Then Conn.Open()
                .ExecuteNonQuery()
            End With
        Else
            Dim ClassID As Integer = CType(lblPageID.Text, Integer)
            With cmd
                .CommandType = CommandType.StoredProcedure
                .Connection = Conn
                If Conn.State = ConnectionState.Closed Then Conn.Open()
                'first get this class' any previous teacher id so teacher can be removed from the class management
                .CommandText = "GetClassTeacher"
                .Parameters.Add("@ClassPageID", SqlDbType.Int)
                .Parameters("@ClassPageID").Value = ClassID
                Dim tname As String = ""
                Try
                    tname = CType(CType(.ExecuteScalar(), Object), String)
                    .Dispose()
                    cmd = New SqlCommand()
                    .CommandType = CommandType.Text
                    .Connection = Conn
                    If Conn.State = ConnectionState.Closed Then Conn.Open()
                    .CommandText = "Delete from as_Teachers_Classes where TeacherLoginName='" & tname & "' and ClassPageID=" & ClassID
                    .ExecuteNonQuery()
                Catch ex As Exception
                    'do nothing
                End Try
            End With
        End If
        'bind all the page with new data from server
        Call GetContactData()
        Call GetData()

        '<SAQIB ADDED LINES>
        If Conn.State = ConnectionState.Open Then Conn.Close()
    End Sub
    '****************************************************************************************************
    Private Sub dlSubPages_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlSubPages.ItemCommand
        If e.CommandName = "ParentPageName" Then
            Response.Redirect("MgrContent.aspx?ID=" & CType(e.CommandArgument, Integer))
        End If

        '<SAQIB ADDED LINES>
        If Conn.State = ConnectionState.Open Then Conn.Close()
    End Sub
    '****************************************************************************************************
    Private Sub dlSubPages_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlSubPages.ItemCreated
        Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)

        If (Not drv Is Nothing) And (DsChildPageLinks1.Tables(0).Rows.Count > 0) Then
            If (CType(drv.Item("id"), Integer) = CType(Request("id"), Integer)) Then
                Dim li As LinkButton = CType(e.Item.Controls(1), LinkButton)
                With li
                    .CssClass = "nav1linkon"
                End With
            End If
        End If

        '<SAQIB ADDED LINES>
        If Conn.State = ConnectionState.Open Then Conn.Close()
    End Sub
    '****************************************************************************************************
    Private Sub DataGrid2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGrid2.SelectedIndexChanged
    End Sub
    Private Sub DataGrid2_ItemCreated(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid2.ItemCreated
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet

        If CType(Request("Level"), Integer) >= 1 Then    'for level 1, 2 and more

            hlnkManageLinks.Visible = False    'hide the manage links for level  > 0

            'if this is not the header or footer item then 
            If e.Item.ItemIndex <> -1 Then

                'get the data row view {database link to data source} for this item {row }
                Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)

                Dim itemID As Integer = CType(lblPageID.Text, Integer)     'CType(Request("ID"), Integer) 'current page id posted
                Dim oitemID As Integer = 0
                Dim ooitemID As Integer = 0

                'if this is the second level then get the parent ID to match with the row
                'that is being created
                If CType(Request("Level"), Integer) = 2 Then
                    'Response.Write("Level2")
                    da.SelectCommand = New SqlCommand
                    da.SelectCommand.CommandText = "Select ParentID from as_Pages where ID=" & CType(Request("ID"), Integer)
                    da.SelectCommand.CommandType = CommandType.Text
                    da.SelectCommand.Connection = Conn

                    da.Fill(ds)

                    itemID = ds.Tables(0).Rows(0)("parentid")

                    ds.Clear()

                End If

                'get to 1 more steup to get the parent page id
                If CType(Request("Level"), Integer) = 3 Then
                    'Response.Write("Level3")
                    da.SelectCommand = New SqlCommand
                    da.SelectCommand.CommandText = "Select ParentID from as_Pages where ID=" & CType(Request("ID"), Integer)
                    da.SelectCommand.CommandType = CommandType.Text
                    da.SelectCommand.Connection = Conn

                    da.Fill(ds)

                    itemID = ds.Tables(0).Rows(0)("parentid")
                    oitemID = itemID    'save it as to use with the followin level 3 if condition

                    ds.Clear()

                    da.SelectCommand = New SqlCommand
                    da.SelectCommand.CommandText = "Select ParentID from as_Pages where ID=" & itemID
                    da.SelectCommand.CommandType = CommandType.Text
                    da.SelectCommand.Connection = Conn
                    da.Fill(ds)


                    itemID = ds.Tables(0).Rows(0)("parentid")

                End If

                'Level 4th
                If CType(Request("Level"), Integer) = 4 Then

                    da.SelectCommand = New SqlCommand
                    da.SelectCommand.CommandText = "Select ParentID from as_Pages where ID=" & CType(Request("ID"), Integer)
                    da.SelectCommand.CommandType = CommandType.Text
                    da.SelectCommand.Connection = Conn

                    da.Fill(ds)

                    itemID = CType(ds.Tables(0).Rows(0)("parentid"), Integer)
                    oitemID = itemID    'save it as to use with the followin level 3 if condition

                    ds.Clear()

                    'cmd = New SqlCommand("Select ParentID from as_Pages where ID=" & itemID, Conn)
                    da.SelectCommand = New SqlCommand
                    da.SelectCommand.CommandText = "Select ParentID from as_Pages where ID=" & itemID
                    da.SelectCommand.CommandType = CommandType.Text
                    da.SelectCommand.Connection = Conn

                    da.Fill(ds)

                    itemID = CType(ds.Tables(0).Rows(0)("parentid"), Integer)
                    ooitemID = itemID

                    ds.Clear()

                    'cmd = New SqlCommand("Select ParentID from as_Pages where ID=" & itemID, Conn)
                    da.SelectCommand = New SqlCommand
                    da.SelectCommand.CommandText = "Select ParentID from as_Pages where ID=" & itemID
                    da.SelectCommand.CommandType = CommandType.Text
                    da.SelectCommand.Connection = Conn

                    da.Fill(ds)

                    itemID = CType(ds.Tables(0).Rows(0)("parentid"), Integer)

                    ds.Clear()

                End If

                'End Of Level 4th
                '----------

                'If drv{databse link to datasource for this item row} of this row exits then
                If (Not drv Is Nothing) AndAlso itemID = CType(drv.Item(0), Integer) Then
                    Dim ParentID As Integer = CType(drv.Item(0), Integer)
                    Dim l As HyperLink = CType(e.Item.Cells(0).Controls(0), HyperLink)
                    l.ForeColor = Color.Gray
                    '.CssClass = "nav2linkon"

                    'get the child pages and display their links for editing {nav3...}

                    da.SelectCommand = New SqlCommand
                    da.SelectCommand.CommandText = "Select * from as_Pages where ParentID=" & ParentID
                    da.SelectCommand.CommandType = CommandType.Text
                    da.SelectCommand.Connection = Conn

                    da.Fill(ds, "level2")

                    'if found any then loop through each and display in table rows

                    Dim c As TableCell = CType(e.Item.Controls(0), TableCell)
                    Dim ph As PlaceHolder = New PlaceHolder
                    ph.Controls.Add(New LiteralControl("<Table border='0' align='right' width='130'>"))

                    If ds.Tables("level2").Rows.Count > 0 Then

                        Dim sCss As String = "nav3linkoff"
                        Dim s4th As String = ""
                        Dim i

                        For i = 0 To ds.Tables("level2").Rows.Count - 1

                            '*********************************************************************
                            'change the approperiate css according to the currently navigating page
                            If (CType(Request("Level"), Integer) = 4) And (Request("ID") = Request("id")) Then
                                sCss = "nav3linkon"

                                If ooitemID = ds.Tables("level2").Rows(i)("id") Then
                                    s4th = "<table width='100%' border='0' cellpadding='0' cellspacing='0'>"
                                    da.SelectCommand = New SqlCommand
                                    da.SelectCommand.CommandText = "Select * from as_Pages where ParentID = " & ds.Tables("level2").Rows(i)("ID")
                                    da.SelectCommand.CommandType = CommandType.Text
                                    da.SelectCommand.Connection = Conn
                                    da.Fill(ds, "level3")
                                    Dim j

                                    For j = 0 To ds.Tables("level3").Rows.Count - 1

                                        s4th &= "<tr><td><font size=1>&nbsp;&nbsp;</font><a href='MgrContent.aspx?Level=3&ID=" & CType(ds.Tables("level3").Rows(j)("ID"), Integer) & "' class='nav4linkoff'>" & CType(ds.Tables("level3").Rows(j)("PageName"), String) & "</a></td></tr>"

                                        'For Level 4th
                                        If Request("level") = 4 And oitemID = ds.Tables("level3").Rows(j)("ID") Then

                                            da.SelectCommand = New SqlCommand
                                            da.SelectCommand.CommandText = "Select * from as_Pages where ParentID = (select parentid from as_pages where id = " & Request("ID") & ")"
                                            da.SelectCommand.CommandType = CommandType.Text
                                            da.SelectCommand.Connection = Conn

                                            da.Fill(ds, "level4")

                                            Dim k

                                            For k = 0 To ds.Tables("level4").Rows.Count - 1

                                                Dim ssCss As String = "nav4linkoff"
                                                If (CType(Request("Level"), Integer) = 4 AndAlso CType(Request("id"), Integer) = CType(ds.Tables("level4").Rows(k)("ID"), Integer)) Then
                                                    ssCss = "nav4linkon"
                                                End If

                                                s4th &= "<tr><td><font size=1>&nbsp;&nbsp;&nbsp;&nbsp;</font><a href='MgrContent.aspx?Level=4&ID=" & ds.Tables("level4").Rows(k)("ID") & "' class='" & ssCss & "'>" & ds.Tables("level4").Rows(k)("PageName") & "</a></td></tr>"

                                            Next

                                        End If
                                        'End of For Level 4th

                                    Next

                                    s4th &= "</table>"
                                End If
                            Else

                                sCss = "nav3linkoff"

                            End If

                            If (CType(Request("Level"), Integer) = 3) And (oitemID = CType(ds.Tables("level2").Rows(i)("id"), Integer)) Then
                                sCss = "nav3linkon"

                                s4th = "<table width='100%' border='0' cellpadding='0' cellspacing='0'>"
                                da.SelectCommand = New SqlCommand
                                da.SelectCommand.CommandText = "Select * from as_Pages where ParentID = " & ds.Tables("level2").Rows(i)("ID")
                                da.SelectCommand.CommandType = CommandType.Text
                                da.SelectCommand.Connection = Conn
                                da.Fill(ds, "level3")
                                Dim j

                                For j = 0 To ds.Tables("level3").Rows.Count - 1
                                    Dim ssCss As String = "nav4linkoff"
                                    If (CType(Request("Level"), Integer) = 3 AndAlso CType(Request("ID"), Integer) = CType(ds.Tables("level3").Rows(j)("ID"), Integer)) Then
                                        ssCss = "nav4linkon"
                                    End If
                                    s4th &= "<tr><td><font size=1>&nbsp;&nbsp;</font><a href='MgrContent.aspx?Level=3&ID=" & CType(ds.Tables("level3").Rows(j)("ID"), Integer) & "' class='" & ssCss & "'>" & CType(ds.Tables("level3").Rows(j)("PageName"), String) & "</a></td></tr>"

                                    'For Level 4th
                                    If Request("level") = 3 And Request("id") = ds.Tables("level3").Rows(j)("ID") Then

                                        da.SelectCommand = New SqlCommand
                                        da.SelectCommand.CommandText = "Select * from as_Pages where ParentID=" & CType(ds.Tables("level3").Rows(j)("ID"), Integer)
                                        da.SelectCommand.CommandType = CommandType.Text
                                        da.SelectCommand.Connection = Conn

                                        da.Fill(ds, "level4")

                                        Dim k

                                        For k = 0 To ds.Tables("level4").Rows.Count - 1
                                            s4th &= "<tr><td><font size=1>&nbsp;&nbsp;&nbsp;&nbsp;</font><a href='MgrContent.aspx?Level=4&ID=" & ds.Tables("level4").Rows(k)("ID") & "' class='nav4linkoff'>" & ds.Tables("level4").Rows(k)("PageName") & "</a></td></tr>"
                                        Next
                                        s4th &= "<tr><td><img src='images/spacer.gif' width=10 height=1><a href='#' class='nav1linkhigh' onClick='return OpenWindow(" & CType(ds.Tables("level3").Rows(j)("ID"), Integer) & ");'>Manage Links</a></td></tr>"

                                    End If
                                    'End of For Level 4th

                                Next

                                s4th &= "</table>"

                            Else

                                sCss = "nav3linkoff"

                            End If

                            '*********************************************************************
                            'change the approperiate css according to the currently navigating page
                            If (CType(Request("Level"), Integer) = 2) And (CType(Request("ID"), Integer) = CType(ds.Tables("level2").Rows(i)("ID"), Integer)) Then
                                sCss = "nav3linkon"
                                'as the current link is selected now in editing so load it's 4level lilnks here

                                da.SelectCommand = New SqlCommand
                                da.SelectCommand.CommandText = "Select * from as_Pages where ParentID=" & ds.Tables("level2").Rows(i)("ID")
                                da.SelectCommand.CommandType = CommandType.Text
                                da.SelectCommand.Connection = Conn

                                da.Fill(ds, "level21")

                                s4th = "<table width='100%' border='0'>"

                                Dim a

                                For a = 0 To ds.Tables("level21").Rows.Count - 1
                                    s4th &= "<tr><td><font size=1>&nbsp;&nbsp;</font><a href='MgrContent.aspx?Level=3&ID=" & CType(ds.Tables("level21").Rows(a)("ID"), Integer) & "' class='nav4linkoff'>" & CType(ds.Tables("level21").Rows(a)("PageName"), String) & "</a></td></tr>"
                                Next
                                s4th &= "<tr><td><img src='images/spacer.gif' width=10 height=1><a href='#' class='nav1linkhigh' onClick='return OpenWindow(" & CType(ds.Tables("level2").Rows(i)("ID"), Integer) & ");'>Manage Links</a></td></tr>"
                                s4th &= "</table>"

                            Else

                                sCss = "nav3linkoff"

                            End If

                            Dim sstr As String = "<TR><TD><a href='MgrContent.aspx?Level=2&ID=" & CType(ds.Tables("level2").Rows(i)("ID"), Integer) & "' class='" & sCss & "'>" & CType(ds.Tables("level2").Rows(i)("PageName"), String) & "</a></TD></TR>"

                            If s4th.Trim.Length > 0 And s4th.Trim <> "<table width='100%' border='0'></table>" Then

                                sstr &= "<tr><td>" & s4th & "</td></tr>"
                                s4th = ""

                            End If

                            sstr &= ""
                            ph.Controls.Add(New LiteralControl(sstr))

                        Next

                        ds.Clear()

                    End If

                    If CType(Request("Level"), Integer) = 1 Then

                        ph.Controls.Add(New LiteralControl("<tr><td><a href='#' class='nav1linkhigh' onClick='return OpenWindow(" & lblPageID.Text & ");'>Manage Links</a></td></tr>"))

                    End If

                    ph.Controls.Add(New LiteralControl("</Table>"))
                    c.Controls.Add(ph)
                    hlnkManageLinks.Visible = False

                End If

            End If

        End If

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

    End Sub
    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        If CType(lblPageID.Text, Integer) <> 0 Then
            Dim cmd As New SqlCommand
            With cmd
                .CommandText = "Update as_Pages set PageName=@PageName,Body=@BodyHTML,BodyHeading=@BodyHeading where(ID=@PageID) "
                .Parameters.Add("@PageName", Trim(txtPageName.Text))
                .Parameters.Add("@BodyHTML", Trim(EWE1.HTMLValue))
                .Parameters.Add("@BodyHeading", Trim(txtPageHeading.Text))
                .Parameters.Add("@PageID", CType(lblPageID.Text, Integer))
                .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()
            End With
        End If

        'bind all the page with new data from server
        Call GetContactData()
        Call GetData()

        '<SAQIB ADDED LINES>
        'If CType(lblPageID.Text, Integer) = 5 Then
        Dim result As Int32 = CheckForValidUpdate(CType(lblPageID.Text, Integer), 268)
        If result = 1 Then
            Copydata()
        End If

        'End If

        If Conn.State = ConnectionState.Open Then Conn.Close()
    End Sub
#Region "Related Contacts Data Grid ***************************************************"
    '******************************************************************************
    Private Sub DG_RelatedContacts_ItemCreated(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DG_RelatedContacts.ItemCreated
        If (e.Item.ItemType = ListItemType.Header) Then
            'Dim lb As LinkButton = New LinkButton()
            'With lb
            '	.Text = "Add New"
            '	.CommandArgument = ""
            '	.CommandName = "ADD"
            '	.CssClass = "nav1linkon"
            '	.ID = "lnkAddNewContact"
            '	.Visible = True
            '	.Attributes.Add("onClick", "window.open('AddRelatedContact.aspx?ID=" & Request("ID") & "','','width=400,height=200, menu=no');return false;")
            'End With
            'e.Item.Cells(0).Controls.Add(lb)
        ElseIf e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.EditItem Then
            Dim lb As LinkButton = CType(e.Item.Cells(3).Controls(0), LinkButton)
            lb.Attributes.Add("onClick", "return confirm('Are you you sure to delete this contact?');")

        End If

    End Sub
    Private Sub DG_RelatedContacts_CancelCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DG_RelatedContacts.CancelCommand
        With DG_RelatedContacts
            .Columns(3).Visible = True
            .EditItemIndex = -1
        End With
        Call GetContactData()
        Call GetData()

    End Sub
    Private Sub DG_RelatedContacts_EditCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DG_RelatedContacts.EditCommand
        With DG_RelatedContacts
            .Columns(3).Visible = False
            .EditItemIndex = e.Item.ItemIndex
        End With
        Call GetContactData()
        Call GetData()
    End Sub
    Private Sub DG_RelatedContacts_DeleteCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DG_RelatedContacts.DeleteCommand

        With daRelatedContacts.DeleteCommand
            .Parameters("@ContactID").Value = CType(DG_RelatedContacts.DataKeys(e.Item.ItemIndex), Integer)
            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()
        End With

        With DG_RelatedContacts
            .Columns(3).Visible = True
            .EditItemIndex = -1
        End With
        Call GetContactData()
        Call GetData()

    End Sub
    Private Sub DG_RelatedContacts_UpdateCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DG_RelatedContacts.UpdateCommand

        Dim tlnk As TextBox = CType(e.Item.Cells(0).Controls(0), TextBox)
        Dim tadd As TextBox = CType(e.Item.Cells(1).Controls(0), TextBox)
        With daRelatedContacts.UpdateCommand
            .Parameters("@ContactID").Value = CType(DG_RelatedContacts.DataKeys(e.Item.ItemIndex), Integer)
            .Parameters("@Heading").Value = Trim(tlnk.Text)
            .Parameters("@InfoText").Value = Trim(tadd.Text)
            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()
        End With

        With DG_RelatedContacts
            .Columns(3).Visible = True
            .EditItemIndex = -1
        End With
        Call GetContactData()
        Call GetData()

    End Sub
#End Region

#Region "Related Links Data Grid ******************************************************"
    '******************************************************************************
    Private Sub DG_RelatedLinks_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DG_RelatedLinks.SelectedIndexChanged

    End Sub
    Private Sub DG_RelatedLinks_UpdateCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DG_RelatedLinks.UpdateCommand
        Dim tlnk As TextBox = CType(e.Item.Cells(0).Controls(0), TextBox)
        Dim tadd As TextBox = CType(e.Item.Cells(1).FindControl("TextBox1"), TextBox)
        With daContactLinks.UpdateCommand
            .Parameters("@LinkName").Value = Trim(tlnk.Text)
            .Parameters("@LinkURL").Value = Trim(tadd.Text)
            .Parameters("@LinkPic").Value = ""
            .Parameters("@Original_ID").Value = CType(DG_RelatedLinks.DataKeys(e.Item.ItemIndex), Integer)
            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()
        End With

        With DG_RelatedLinks
            .Columns(3).Visible = True
            .EditItemIndex = -1
        End With

        Call GetContactData()
        Call GetData()
    End Sub
    Private Sub DG_RelatedLinks_CancelCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DG_RelatedLinks.CancelCommand
        With DG_RelatedLinks
            .Columns(3).Visible = True
            .EditItemIndex = -1
        End With
        Call GetContactData()
        Call GetData()
    End Sub
    Private Sub DG_RelatedLinks_DeleteCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DG_RelatedLinks.DeleteCommand
        With daContactLinks.DeleteCommand
            .Parameters("@Original_ID").Value = CType(DG_RelatedLinks.DataKeys(e.Item.ItemIndex), Integer)
            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()
        End With

        With DG_RelatedLinks
            .EditItemIndex = -1
        End With
        Call GetContactData()
        Call GetData()
    End Sub
    Private Sub DG_RelatedLinks_EditCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DG_RelatedLinks.EditCommand
        With DG_RelatedLinks
            .Columns(3).Visible = False
            .EditItemIndex = e.Item.ItemIndex
            'Dim s As String = "<script type=""text/javascript"" language=""javascript"">"
            's &= "document.getElementById(""" & e.Item.Cells(0).Controls(0).ID & """).focus();"
            's &= "</script>"
            'Me.Page.RegisterStartupScript("FocusManager", s)
        End With
        Call GetContactData()
        Call GetData()
    End Sub
    Private Sub DG_RelatedLinks_ItemCreated(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DG_RelatedLinks.ItemCreated
        If (e.Item.ItemType = ListItemType.Header) Then
            'Dim lb As LinkButton = New LinkButton()
            'With lb
            '	.Text = "Add New"
            '	.CommandArgument = ""
            '	.CommandName = "ADD"
            '	.CssClass = "nav1linkon"
            '	.ID = "lnkAddNewContact"
            '	.Visible = True
            '	.Attributes.Add("onClick", "window.open('AddRelatedLinks.aspx?ID=" & Request("ID") & "','','width=400,height=200, menu=no');return false;")
            'End With
            'e.Item.Cells(0).Controls.Add(lb)
        ElseIf e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.EditItem Then
            Dim lb As LinkButton = CType(e.Item.Cells(3).Controls(0), LinkButton)
            lb.Attributes.Add("onClick", "return confirm('Are you you sure to delete this link?');")
        End If
    End Sub
    'Private Sub DG_RelatedLinks_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DG_RelatedLinks.ItemDataBound
    'Dim ctl As Control
    'For Each ctl In e.Item.Cells(1).Controls
    'Response.Write(ctl.GetType.ToString & vbCrLf)
    'Next
    'End Sub
#End Region
    Private Sub btnTopPicUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTopPicUpdate.Click
        Dim cmd As New SqlCommand

        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\" & Trim(lblPageID.Text) & "top" & sFilename
                            .SaveAs(Server.MapPath("..") & sFilename)

                            With cmd
                                .CommandText = "Update as_Pages Set TopPicture='." & sFilename & "' where ID=" & CType(Trim(lblPageID.Text), Integer)
                                .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()
                            End With

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

                        End Try

                    Case Else
                        'nothing
                End Select
                'referesh the page data
                Call GetContactData()
                Call GetData()
            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

        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\" & Trim(lblPageID.Text) & "right" & sFilename
                            .SaveAs(Server.MapPath("..") & sFilename)

                            With cmd
                                .CommandText = "Update as_Pages Set  RightPicDesc='" & Replace(Trim(txtImageDesc.Text), "'", "''") & "' , RightPicture='." & sFilename & "' where ID=" & CType(Trim(lblPageID.Text), Integer)
                                .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()
                            End With

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

                        End Try

                    Case Else
                        'nothing
                End Select

                'referesh the page data with latest one from db
                Call GetContactData()
                Call GetData()
            Else    'just update the description of the 
                With cmd
                    .CommandText = "Update as_Pages Set  RightPicDesc='" & Replace(Trim(txtImageDesc.Text), "'", "''") & "' where ID=" & CType(Trim(lblPageID.Text), Integer)
                    .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()
                End With

            End If
        End With

        '<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
        Else
            img.ImageUrl = "..\contents\images\noimage.jpg"
        End If

        '<SAQIB ADDED LINES>
        If Conn.State = ConnectionState.Open Then Conn.Close()
    End Sub
    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 & "?" & DateTime.Now.Second.ToString
        Else
            img.ImageUrl = "..\contents\images\noimage.jpg"
        End If

        '<SAQIB ADDED LINES>
        If Conn.State = ConnectionState.Open Then Conn.Close()
    End Sub
    Private Sub AddRelContact1_ContactAdded() Handles AddRelContact1.ContactAdded
        'referesh the page data with latest one from db
        Call GetContactData()
        Call GetData()

        '<SAQIB ADDED LINES>
        If Conn.State = ConnectionState.Open Then Conn.Close()
    End Sub
    Private Sub AddRelLinks1_LinkAdded() Handles AddRelLinks1.LinkAdded
        'referesh the page data with latest one from db
        Call GetContactData()
        Call GetData()

        '<SAQIB ADDED LINES>
        If Conn.State = ConnectionState.Open Then Conn.Close()
    End Sub
    '****************************************************************************
    '	This will Remove Top Body Picture { right of Top Links }
    '****************************************************************************
    Private Sub lnkTopPicRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkTopPicRemove.Click
        Dim cmd As New SqlCommand
        With cmd
            .CommandText = "Select TopPicture from as_Pages where ID=" & CType(Trim(lblPageID.Text), Integer)
            .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=" & CType(Trim(lblPageID.Text), Integer)
                .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()

                'referesh the page data
                Call GetContactData()
                Call GetData()

            End If

        End With

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

    End Sub
    '****************************************************************************
    '	This will Remove Right Body Picture
    '****************************************************************************
    Private Sub lnkRightPicRemove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkRightPicRemove.Click
        Dim cmd As New SqlCommand
        With cmd

            .CommandText = "Select RightPicture from as_Pages where ID=" & CType(Trim(lblPageID.Text), Integer)
            .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 RightPicDesc='' , RightPicture='' where ID=" & CType(Trim(lblPageID.Text), Integer)
                .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()
                'referesh the page data
                Call GetContactData()
                Call GetData()
            End If
        End With

        '<SAQIB ADDED LINES>
        If Conn.State = ConnectionState.Open Then Conn.Close()
    End Sub
    Private Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Unload
        If Conn.State = ConnectionState.Open Then Conn.Dispose()
    End Sub
    'View/Manage Class assignments
    Private Sub lnkClassAssignments_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkClassAssignments.Click
        Session("ClassPageID") = lblPageID.Text
        Response.Redirect("ClassAssignments.aspx", True)
    End Sub
    Private Sub lnkViewCalendar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkViewCalendar.Click
        Response.Redirect("ViewCalender.aspx?ID=" & lblPageID.Text & "&Level=1&view=Calendar", True)
    End Sub

    Private Sub Btndelchild_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'Dim pageno_todelete As Int32
        'Dim pageno_tocopyfrom As Int32
        'pageno_todelete = 526  ' The page to which the contents shall be added.
        'DeleteChildPage(pageno_todelete) 'All The pages are deleted from given Node
        'pageno_tocopyfrom = 5
        'CopyPageLevelContents(pageno_todelete, pageno_tocopyfrom)
        'InsertChildPagesData(5, 0, 526)

        'CopyChildpages()

    End Sub
    Private Sub Copydata()

        Dim pageno_todelete As Int32
        Dim pageno_tocopyfrom As Int32
        pageno_todelete = 222  ' The page to which the contents shall be added.
        DeleteChildPage(pageno_todelete) 'All The pages are deleted from given Node
        pageno_tocopyfrom = 268
        CopyPageLevelContents(pageno_todelete, pageno_tocopyfrom)
        InsertChildPagesData(268, 0, 222)

    End Sub
    Private Sub DeleteChildPage(ByVal pageno As Int32)

        Dim ChildPageId As Int32
        Dim selcmd As New SqlCommand
        Dim delcmd As New SqlCommand
        Dim adpter As New SqlDataAdapter
        Dim data As New DataSet

        selcmd.CommandText = "select ID from as_pages where ParentID = " & pageno
        selcmd.CommandType = CommandType.Text
        selcmd.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 temp As String = selcmd.ExecuteScalar()
        adpter = New SqlDataAdapter(selcmd)
        adpter.Fill(data)

        If data.Tables(0).Rows.Count > 0 Then

            For i As Int32 = 0 To (data.Tables(0).Rows.Count - 1)

                ChildPageId = Convert.ToInt32(data.Tables(0).Rows(i)("ID").ToString())
                DeleteChildPage(ChildPageId)

                pageno = Convert.ToInt32(data.Tables(0).Rows(i)("ID").ToString())
                delcmd.CommandText = "delete from as_pages where ID =" & pageno
                delcmd.CommandType = CommandType.Text
                delcmd.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
                delcmd.ExecuteNonQuery()

            Next
        End If


    End Sub


    Private Function CheckForValidUpdate(ByVal checkfor As Int32, ByVal checkIn As Int32) As Int32

        Dim ChildPageId As Int32
        Dim selcmd As New SqlCommand
        Dim delcmd As New SqlCommand
        Dim adpter As New SqlDataAdapter
        Dim data As New DataSet
        Dim result As New Int32

        If checkfor = checkIn Then
            result = 1
            CheckForValidUpdate = result
            Exit Function
        Else
            selcmd.CommandText = "select ID from as_pages where ParentID = " & checkIn
            selcmd.CommandType = CommandType.Text
            selcmd.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 temp As String = selcmd.ExecuteScalar()
            adpter = New SqlDataAdapter(selcmd)
            adpter.Fill(data)
            If data.Tables(0).Rows.Count > 0 Then
                For i As Int32 = 0 To (data.Tables(0).Rows.Count - 1)
                    ChildPageId = Convert.ToInt32(data.Tables(0).Rows(i)("ID").ToString())
                    If ChildPageId = checkfor Then
                        result = 1
                        CheckForValidUpdate = result
                        Exit Function
                    Else
                        If result = 0 Then
                            result = CheckForValidUpdate(checkfor, ChildPageId)
                        End If
                    End If
                Next
            End If
        End If
        CheckForValidUpdate = result
    End Function



    Private Sub CopyPageLevelContents(ByVal pageno_des As Int32, ByVal pageno_source As Int32)
        Dim ChildPageId As Int32
        Dim selcmd As New SqlCommand
        Dim copycmd As New SqlCommand
        Dim adpter As New SqlDataAdapter
        Dim data As New DataSet
        Dim SQLUpdate As String

        Dim PageName As String = ""
        Dim Body As String = ""
        Dim BodyHeading As String = ""
        Dim TopPicture As String = ""
        Dim RightPicture As String = ""
        Dim ParentID As Int32
        Dim RightPicDesc As String = ""
        Dim PageType As String = ""
        Dim CanDelete As Int32

        selcmd.CommandText = "select * from as_pages where ID = " & pageno_source
        selcmd.CommandType = CommandType.Text
        selcmd.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

        adpter = New SqlDataAdapter(selcmd)
        adpter.Fill(data)

        If data.Tables(0).Rows.Count > 0 Then

            Try
                PageName = data.Tables(0).Rows(0)("PageName")
            Catch
            End Try
            Try
                Body = data.Tables(0).Rows(0)("Body")
            Catch
            End Try

            Try
                BodyHeading = data.Tables(0).Rows(0)("BodyHeading")
            Catch
            End Try

            Try
                TopPicture = data.Tables(0).Rows(0)("TopPicture")
            Catch
            End Try

            Try
                RightPicture = data.Tables(0).Rows(0)("RightPicture")
            Catch
            End Try

            Try
                ParentID = pageno_des
            Catch
            End Try

            Try
                RightPicDesc = data.Tables(0).Rows(0)("RightPicDesc")
            Catch
            End Try

            Try
                PageType = data.Tables(0).Rows(0)("PageType")
            Catch
            End Try

            Try
                CanDelete = 1 'CType(data.Tables(0).Rows(0)("CanDelete"), Int32)
            Catch
            End Try


            SQLUpdate = "update as_pages SET "
            'SQLUpdate = SQLUpdate & "PageName	 = '" & PageName
            SQLUpdate = SQLUpdate & "Body	 = '" & Body
            SQLUpdate = SQLUpdate & "',BodyHeading 	 = '" & BodyHeading
            SQLUpdate = SQLUpdate & "',TopPicture	 = '" & TopPicture
            SQLUpdate = SQLUpdate & "',RightPicture	 = '" & RightPicture
            'SQLUpdate = SQLUpdate & ",ParentID	     = " & ParentID
            SQLUpdate = SQLUpdate & "',RightPicDesc	 = '" & RightPicDesc
            SQLUpdate = SQLUpdate & "',PageType	 = '" & PageType
            SQLUpdate = SQLUpdate & "',CanDelete = " & CanDelete
            SQLUpdate = SQLUpdate & " WHERE ( ID = " & pageno_des & ")"

            copycmd.CommandText = SQLUpdate
            copycmd.CommandType = CommandType.Text
            copycmd.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
            copycmd.ExecuteNonQuery()

        End If

        '[ID], [PageName], [Body], [BodyHeading], [TopPicture], [RightPicture], [ParentID], [RightPicDesc], [PageType], [CanDelete]

    End Sub

    Private Sub InsertChildPagesData(ByVal childpageno As Int32, ByVal parentpageno As Int32, ByVal Newparentpageno As Int32)

        Dim Newparentpagenopass As Int32
        Dim ChildPageId As Int32
        Dim selcmd As New SqlCommand
        Dim selchildcmd As New SqlCommand
        Dim insertcmd As New SqlCommand
        Dim adpter As New SqlDataAdapter
        Dim data As New DataSet
        Dim datachildId As New DataSet
        Dim parentdata As New DataSet
        Dim SQLInsert As String
        Dim SQLSelect As String

        Dim PageName As String
        Dim Body As String
        Dim BodyHeading As String
        Dim TopPicture As String
        Dim RightPicture As String
        Dim ParentID As Int32
        Dim RightPicDesc As String
        Dim PageType As String
        Dim CanDelete As Int32


        selchildcmd.CommandText = "select ID from as_pages where ParentID = " & childpageno
        selchildcmd.CommandType = CommandType.Text
        selchildcmd.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

        adpter = New SqlDataAdapter(selchildcmd)
        adpter.Fill(datachildId)

        If datachildId.Tables(0).Rows.Count > 0 Then

            For i As Int32 = 0 To datachildId.Tables(0).Rows.Count - 1


                selcmd.CommandText = "select * from as_pages where ID = " & Convert.ToInt32(datachildId.Tables(0).Rows(i)("ID").ToString())

                selcmd.CommandType = CommandType.Text
                selcmd.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

                adpter = New SqlDataAdapter(selcmd)
                adpter.Fill(data)


                Try
                    PageName = data.Tables(0).Rows(i)("PageName")
                    PageName = ReplaceQuotes(PageName)
                Catch
                End Try
                Try
                    Body = data.Tables(0).Rows(i)("Body")
                    Body = ReplaceQuotes(Body)
                Catch
                End Try

                Try
                    BodyHeading = data.Tables(0).Rows(i)("BodyHeading")
                    BodyHeading = ReplaceQuotes(BodyHeading)
                Catch
                End Try

                Try
                    TopPicture = data.Tables(0).Rows(i)("TopPicture")
                Catch
                End Try

                Try
                    RightPicture = data.Tables(0).Rows(i)("RightPicture")
                Catch
                End Try

                Try
                    ParentID = Newparentpageno 'data.Tables(0).Rows(i)("ParentID")
                Catch
                End Try

                Try
                    RightPicDesc = data.Tables(0).Rows(i)("RightPicDesc")
                Catch
                End Try

                Try
                    PageType = data.Tables(0).Rows(i)("PageType")
                Catch
                End Try

                Try
                    CanDelete = 1 'CType(data.Tables(0).Rows(0)("CanDelete"), Int32)
                Catch
                End Try


                SQLInsert = "insert into as_pages ( PageName,Body,BodyHeading,TopPicture,RightPicture,"
                SQLInsert = SQLInsert & "ParentID,RightPicDesc,PageType,CanDelete)values('"
                SQLInsert = SQLInsert & PageName & "','" & Body & "','" & BodyHeading & "','" & TopPicture
                SQLInsert = SQLInsert & "','" & RightPicture & "'," & ParentID & ",'" & RightPicDesc & "','" & PageType & "'," & CanDelete & ")"

                insertcmd.CommandText = SQLInsert
                insertcmd.CommandType = CommandType.Text
                insertcmd.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
                insertcmd.ExecuteNonQuery()


                selcmd.CommandText = "select ID from as_pages " _
                & "where (PageName = null or PageName = '" & PageName & "') AND (Body Like null or Body Like '" _
                & Body & "') AND (BodyHeading = null or BodyHeading= '" & BodyHeading & "') AND (TopPicture = null or TopPicture='" & TopPicture _
                & "') AND ( RightPicture = null or RightPicture = '" & RightPicture _
                & "') AND (ParentID =" & ParentID & ") AND (RightPicDesc = null or RightPicDesc='" & RightPicDesc _
                & "') AND ( PageType = '" & PageType & "') AND (CanDelete = " & CanDelete & ")"

                selcmd.CommandType = CommandType.Text
                selcmd.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
                parentdata = New DataSet
                adpter = New SqlDataAdapter(selcmd)
                adpter.Fill(parentdata)

                If parentdata.Tables(0).Rows.Count > 0 Then
                    Try
                        Newparentpagenopass = Convert.ToInt32(parentdata.Tables(0).Rows(0)("ID").ToString())
                    Catch
                        'do nothing
                    End Try

                End If

                InsertChildPagesData(Convert.ToInt32(data.Tables(0).Rows(i)("ID").ToString()), childpageno, Newparentpagenopass)

            Next



        End If


    End Sub

    Public Function ReplaceQuotes(ByVal chkStr As String) As String
        Dim Pos As Integer
        chkStr = Trim(chkStr)
        Dim TempStr As String
        Pos = InStr(chkStr, "'")
        While Pos > 0
            TempStr = Mid(chkStr, Pos + 1)
            TempStr = Left(TempStr, 1)
            If TempStr = "'" Then
                chkStr = Mid(chkStr, 1, Pos - 1) & "``" & Mid(chkStr, Pos + 2)
            Else
                chkStr = Mid(chkStr, 1, Pos - 1) & "`" & Mid(chkStr, Pos + 1)
            End If
            Pos = InStr(Pos + 2, chkStr, "'")
        End While
        'Return value
        ReplaceQuotes = chkStr
    End Function

    Private Sub CopyChildPages(ByVal pageno_des As Int32, ByVal pageno_source As Int32)

        'Dim ChildPageId As Int32
        'Dim selcmd As New SqlCommand
        'Dim delcmd As New SqlCommand
        'Dim adpter As New SqlDataAdapter
        'Dim data As New DataSet

        'selcmd.CommandText = "select ID from as_pages where ParentID = " & pageno_des
        'selcmd.CommandType = CommandType.Text
        'selcmd.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 temp As String = selcmd.ExecuteScalar()
        'adpter = New SqlDataAdapter(selcmd)
        'adpter.Fill(data)

        'If data.Tables(0).Rows.Count > 0 Then

        '    For i As Int32 = 0 To (data.Tables(0).Rows.Count - 1)

        '        ChildPageId = Convert.ToInt32(data.Tables(0).Rows(i)("ID").ToString())
        '        InsertChildPagesData(pageno, parentpageno)

        '        pageno = Convert.ToInt32(data.Tables(0).Rows(i)("ID").ToString())
        '        delcmd.CommandText = "delete from as_pages where ID =" & pageno
        '        delcmd.CommandType = CommandType.Text
        '        delcmd.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
        '        delcmd.ExecuteNonQuery()

        '    Next
        'End If


    End Sub
End Class
