Imports DotNetNuke
Imports System.Collections.Generic
 
Namespace DotNetNuke.Modules.DynamicForms
 
    Partial Class View
        Inherits DotNetNuke.Entities.Modules.PortalModuleBase
        Dim CurrentPageIndex As Integer
        Dim tempTextBox As TextBox = Nothing
        Dim tempDropDownList As DropDownList = Nothing
        Dim tempListBox As ListBox = Nothing
        Dim tempCheckBox As CheckBox = Nothing
        Dim tempCheckBoxList As CheckBoxList = Nothing
        Dim tempRadioButton As RadioButton = Nothing
        Dim tempRadioButtonList As RadioButtonList = Nothing
 
        Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                If Not Page.IsPostBack Then
                    LoadFormsDropDown()
                    ShowData(1)
                End If
 
            Catch exc As Exception        'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub
 
#Region " Show Data "
        Private Sub ShowData(ByVal intCurrentPageIndex As Integer)
            Dim objRecordData As RecordData = DynamicForms_DAL.GetRecords(PortalId, intCurrentPageIndex)
 
            If objRecordData.TotalRecords > 0 Then
                Me.lblTotalPages.Text = objRecordData.TotalRecords.ToString()
                Me.lblCurrentPage.Text = intCurrentPageIndex.ToString()
                CurrentRecord.Text = objRecordData.RecordID
            Else
                CurrentRecord.Text = "New"
                CurrentPageIndex = 0
                Me.lblCurrentPage.Text = "0"
                Me.lblTotalPages.Text = "0"
                CurrentRecord.Visible = True
                pnlNavigator.Visible = False
            End If
 
            If objRecordData.IsFirstRecord Then
                btnPrevious.Visible = False
            Else
                btnPrevious.Visible = True
            End If
            If objRecordData.IsLastRecord Then
                btnNext.Visible = False
            Else
                btnNext.Visible = True
            End If
 
            LoadDynamicControl()
        End Sub
#End Region
 
#Region " Save Data "
        Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
            Dim colControlCollection As ControlCollection
            Dim Item As Control
            Dim tmpType As String
 
            Dim objItemType As New ItemType
 
            ' Delete the previous data
            If CurrentRecord.Text <> "New" Then
                DynamicForms_DAL.DeleteExistingData(CInt(CurrentRecord.Text), CInt(ddlControl.SelectedValue))
            Else
                ' A New Record
                Dim i As Integer = 0
                i = DynamicForms_DAL.AddNewRecord(PortalId)
                CurrentRecord.Text = i
                CurrentRecord.Visible = False
                pnlNavigator.Visible = True
                btnNext.Visible = False
 
                If CInt(Me.lblTotalPages.Text) > 0 Then
                    btnPrevious.Visible = True
                    Me.lblTotalPages.Text = CStr(CInt(Me.lblTotalPages.Text) + 1)
                    Me.lblCurrentPage.Text = Me.lblTotalPages.Text
                Else
                    btnPrevious.Visible = False
                    Me.lblTotalPages.Text = "1"
                    Me.lblCurrentPage.Text = "1"
                End If
 
            End If
 
            colControlCollection = CType(DynamicPlaceHolder.FindControl("DynamicPage"), Entities.Modules.PortalModuleBase).Controls
            For Each Item In colControlCollection
                tmpType = Item.GetType().FullName
 
                objItemType.RecordID = CInt(CurrentRecord.Text)
                objItemType.FormID = CInt(ddlControl.SelectedValue)
 
                Select Case Item.GetType().FullName
                    Case "System.Web.UI.WebControls.TextBox"
                        tempTextBox = CType(Item, TextBox)
                        objItemType.ItemTypeName = "TextBox"
                        objItemType.ItemName = tempTextBox.ID
                        objItemType.ItemValue = tempTextBox.Text
                        If tempTextBox.Rows > 1 Then
                            objItemType.LargeText = True
                        End If
                    Case "System.Web.UI.WebControls.DropDownList"
                        tempDropDownList = CType(Item, DropDownList)
                        objItemType.ItemTypeName = "DropDownList"
                        objItemType.ItemName = tempDropDownList.ID
                        objItemType.ItemValue = tempDropDownList.SelectedValue
                    Case "System.Web.UI.WebControls.ListBox"
                        tempListBox = CType(Item, ListBox)
                        objItemType.ItemTypeName = "ListBox"
                        objItemType.ItemName = tempListBox.ID
                        objItemType.ItemValue = tempListBox.SelectedValue
                    Case "System.Web.UI.WebControls.CheckBox"
                        tempCheckBox = CType(Item, CheckBox)
                        objItemType.ItemTypeName = "CheckBox"
                        objItemType.ItemName = tempCheckBox.ID
                        objItemType.ItemValue = tempCheckBox.Checked
                    Case "System.Web.UI.WebControls.CheckBoxList"
                        tempCheckBoxList = CType(Item, CheckBoxList)
                        objItemType.ItemTypeName = "CheckBoxList"
                        objItemType.ItemName = tempCheckBoxList.ID
 
                        Dim strSelectedItem As String = ""
                        Dim objListItemCollection As ListItemCollection = tempCheckBoxList.Items
                        Dim objListItem As ListItem
                        For Each objListItem In objListItemCollection
                            If objListItem.Selected Then
                                strSelectedItem += objListItem.Value.Replace(",", "") & ","
                            End If
                        Next
 
                        If strSelectedItem <> "" Then
                            strSelectedItem = strSelectedItem.Substring(0, (strSelectedItem.Length - 1))
                        End If
 
                        objItemType.ItemValue = strSelectedItem
                    Case "System.Web.UI.WebControls.RadioButton"
                        tempRadioButton = CType(Item, RadioButton)
                        objItemType.ItemTypeName = "RadioButton"
                        objItemType.ItemName = tempRadioButton.ID
                        objItemType.ItemValue = tempRadioButton.Checked
                    Case "System.Web.UI.WebControls.RadioButtonList"
                        tempRadioButtonList = CType(Item, RadioButtonList)
                        objItemType.ItemTypeName = "RadioButtonList"
                        objItemType.ItemName = tempRadioButtonList.ID
                        objItemType.ItemValue = tempRadioButtonList.SelectedValue
                    Case Else
                        objItemType.RecordID = -1
                End Select
 
                ' If we have a value then save it
                If objItemType.RecordID > 0 Then
                    DynamicForms_DAL.AddRecordData(objItemType)
                    DynamicForms_DAL.AddFormData(objItemType)
                End If
 
            Next
 
        End Sub
#End Region
 
#Region " Load DropDown "
        Private Sub LoadFormsDropDown()
            ddlControl.DataSource = DynamicForms_DAL.GetForms()
            ddlControl.DataTextField = "FormName"
            ddlControl.DataValueField = "FormID"
            ddlControl.DataBind()
        End Sub
#End Region
 
#Region " Load Dynamic Control "
        Private Sub LoadDynamicControl()
            Dim DynamicPage As String = DotNetNuke.Common.ResolveUrl(Me.TemplateSourceDirectory & String.Format("/DynamicControls/{0}.ascx", DynamicForms_DAL.GetControlName(CInt(ddlControl.SelectedValue))))
            Dim objModule As Entities.Modules.PortalModuleBase = CType(Me.LoadControl(DynamicPage), DotNetNuke.Entities.Modules.PortalModuleBase)
            If Not objModule Is Nothing Then
                objModule.ID = "DynamicPage"
                objModule.ModuleConfiguration = Me.ModuleConfiguration
                DynamicPlaceHolder.Controls.Clear()
                DynamicPlaceHolder.Controls.Add(objModule)
            End If
 
            If CurrentRecord.Text <> "New" Then
 
                Dim objRecordItemType As New ItemType()
                Dim colRecordData As List(Of ItemType) = New List(Of ItemType)
 
                colRecordData = DynamicForms_DAL.GetFormRecords(CInt(CurrentRecord.Text))
 
                Dim colControlCollection As ControlCollection
                Dim Item As Control
 
                For Each objRecordItemType In colRecordData
                    colControlCollection = CType(DynamicPlaceHolder.FindControl("DynamicPage"), Entities.Modules.PortalModuleBase).Controls
                    For Each Item In colControlCollection
                        If objRecordItemType.ItemName = Item.ID Then
                            Select Case Item.GetType().FullName
                                Case "System.Web.UI.WebControls.TextBox"
                                    tempTextBox = CType(DynamicPlaceHolder.FindControl("DynamicPage").FindControl(Item.ID), TextBox)
                                    tempTextBox.Text = objRecordItemType.ItemValue
                                Case "System.Web.UI.WebControls.DropDownList"
                                    tempDropDownList = CType(DynamicPlaceHolder.FindControl("DynamicPage").FindControl(Item.ID), DropDownList)
                                    Try
                                        tempDropDownList.SelectedValue = objRecordItemType.ItemValue
                                    Catch
                                    End Try
                                Case "System.Web.UI.WebControls.ListBox"
                                    tempListBox = CType(DynamicPlaceHolder.FindControl("DynamicPage").FindControl(Item.ID), ListBox)
                                    Try
                                        tempListBox.SelectedValue = objRecordItemType.ItemValue
                                    Catch
                                    End Try
                                Case "System.Web.UI.WebControls.CheckBox"
                                    tempCheckBox = CType(DynamicPlaceHolder.FindControl("DynamicPage").FindControl(Item.ID), CheckBox)
                                    tempCheckBox.Checked = objRecordItemType.ItemValue
                                Case "System.Web.UI.WebControls.CheckBoxList"
                                    tempCheckBoxList = CType(DynamicPlaceHolder.FindControl("DynamicPage").FindControl(Item.ID), CheckBoxList)
                                    tempCheckBoxList.Text = objRecordItemType.ItemValue
 
                                    Dim objListItemCollection As ListItemCollection = tempCheckBoxList.Items
                                    Dim objListItem As ListItem
                                    Dim i As Integer = 0
                                    Dim strSelectedItem() As String = objRecordItemType.ItemValue.Split(",")
                                    For i = 0 To (strSelectedItem.Length - 1)
                                        For Each objListItem In objListItemCollection
                                            If objListItem.Value = strSelectedItem(i) Then
                                                objListItem.Selected = True
                                            End If
                                        Next
                                    Next
 
                                Case "System.Web.UI.WebControls.RadioButton"
                                    tempRadioButton = CType(DynamicPlaceHolder.FindControl("DynamicPage").FindControl(Item.ID), RadioButton)
                                    Try
                                        tempRadioButton.Checked = objRecordItemType.ItemValue
                                    Catch
                                    End Try
                                Case "System.Web.UI.WebControls.RadioButtonList"
                                    tempRadioButtonList = CType(DynamicPlaceHolder.FindControl("DynamicPage").FindControl(Item.ID), RadioButtonList)
                                    Try
                                        tempRadioButtonList.SelectedValue = objRecordItemType.ItemValue
                                    Catch
                                    End Try
                            End Select
                        End If
                    Next
                Next
            End If
 
        End Sub
#End Region
 
#Region " Form Events "
        Protected Sub BtnNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnNew.Click
            CurrentRecord.Text = "New"
            CurrentPageIndex = 0
            CurrentRecord.Visible = True
            pnlNavigator.Visible = False
            LoadDynamicControl()
        End Sub
 
        Protected Sub ddlControl_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlControl.SelectedIndexChanged
            LoadDynamicControl()
        End Sub
 
        Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
            CurrentPageIndex = Convert.ToInt32(Me.lblCurrentPage.Text) - 1
            ShowData(CurrentPageIndex)
        End Sub
 
        Protected Sub Next_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
            CurrentPageIndex = Convert.ToInt32(Me.lblCurrentPage.Text) + 1
            ShowData(CurrentPageIndex)
        End Sub
#End Region
 
    End Class
 
End Namespace