Dynamic Controls in VB.NET.............................

Unknown | 6:08 AM |

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
       
                If Not Page.IsPostBack Then
   If hdnFacilityID.Value <> "" Then
                   LoadLaneAssigmnentDoors(Convert.ToInt64(hdnFacilityID.Value))
    End If
                End If
         

                End If

        Catch ex As Exception
            pnlGlobalMessage.Visible = True
            lblGlobalMessage.Text = "A technical issue has occurred.  A message has been sent to the development team.  Sorry for the inconvenience."
            lblGlobalMessage.Visible = True
            Dim dt As DataTable = Session("user_session")
            Worker.HandleError(ex, "Lane Assignment", Int64.Parse(dt.Rows(0)("user_id")), Request.Browser.Browser.ToString())
        End Try
    End Sub




Public Sub LoadLaneAssigmnentDoors(ByVal facility_ID As Int64)
        If facility_ID <> 0 Then
            hdnfacilitylineid.Value = ""
            Dim dt As New DataTable
            Dim cmd As New SqlCommand
            cmd.CommandType = CommandType.Text
            cmd.CommandText = "select * from facility_lane where facility_ID=" & facility_ID
            dt = DataConnect.GetInstance.GetDt(cmd)
            Dim tcl As New TableCell()
            Dim trl As New TableRow()
            Dim tbmain As New Table()
            Dim trmain As New TableRow()
            Dim trmain1 As New TableRow
            Dim tb As New Table()
            tb.BorderColor = Drawing.Color.SkyBlue
            tb.BorderWidth = 1
            Dim i As Int64 = 0
            Dim j As Int64 = 0
            If dt.Rows.Count <> 0 Then
                For i = 1 To dt.Rows.Count
                    If j = 10 Then
                        j = 5
                    End If
                    j = j + 1
                    Dim tr As New TableRow()
                    Dim tr1 As New TableRow()
                    Dim tr2 As New TableRow()
                    Dim tr3 As New TableRow()
                    Dim tr4 As New TableRow()
                    Dim tr5 As New TableRow()
                    Dim tr6 As New TableRow()
                    Dim tr7 As New TableRow()

                    If hdnfacilitylineid.Value = "" Then
                        hdnfacilitylineid.Value = i & "," & dt.Rows(i - 1)("facility_lane_ID") & "|"
                    Else
                        hdnfacilitylineid.Value = hdnfacilitylineid.Value & i & "," & dt.Rows(i - 1)("facility_lane_ID") & "|"
                    End If


                    Dim lbldoor As New Label()
                    lbldoor.Text = "Door #:"
                    Dim tc As New TableCell()
                    tc.Controls.Add(lbldoor)

                    Dim lbldoor1 As New Label()
                    lbldoor1.ID = "lbldoor1" & i.ToString()
                    lbldoor1.Text = dt.Rows(i - 1)("door_no")
                    lbldoor1.Width = 125
                    Dim tc1 As New TableCell()
                    tc1.Controls.Add(lbldoor1)

                    Dim lbldoorname As New Label()
                    lbldoorname.Text = "Name:"
                    Dim tc15 As New TableCell()
                    tc15.Controls.Add(lbldoorname)

                    Dim lbldoorname1 As New Label()
                    lbldoorname1.ID = "lbldoorname1" & i.ToString()
                    lbldoorname1.Width = 125
                    lbldoorname1.Text = dt.Rows(i - 1)("door_name")
                    Dim tc16 As New TableCell()
                    tc16.Controls.Add(lbldoorname1)

                    Dim lbldirection As New Label()
                    lbldirection.Text = "Direction:"
                    Dim tc2 As New TableCell()
                    tc2.Controls.Add(lbldirection)

                    Dim rdArriving As New RadioButton()
                    rdArriving.Text = "In"
                    rdArriving.ID = "rdArriving" & i.ToString()
                    rdArriving.GroupName = "rd"
                    Dim rdDeparting As New RadioButton()
                    rdDeparting.Text = "Out"
                    rdDeparting.ID = "rdDeparting" & i.ToString()
                    rdDeparting.GroupName = "rd"
                    Dim tc3 As New TableCell()
                    tc3.Controls.Add(rdArriving)
                    tc3.Controls.Add(rdDeparting)

                    Dim lblTrailer As New Label()
                    lblTrailer.Text = "Trailer:"
                    Dim tc4 As New TableCell()
                    tc4.Controls.Add(lblTrailer)

                    Dim txtTrailer As New TextBox()
                    txtTrailer.ID = "txtTrailer" & i.ToString()
                    txtTrailer.Width = 125
                    Dim tc5 As New TableCell()
                    tc5.Controls.Add(txtTrailer)

                    Dim lblCarrier As New Label()
                    lblCarrier.Text = "Carrier:"
                    Dim tc6 As New TableCell()
                    tc6.Controls.Add(lblCarrier)

                    Dim txtCarrier As New TextBox()
                    txtCarrier.ID = "txtCarrier" & i.ToString()
                    txtCarrier.Width = 125
                    Dim tc7 As New TableCell()
                    tc7.Controls.Add(txtCarrier)

                    Dim lblGoingto As New Label()
                    lblGoingto.Text = "Destination:"
                    Dim tc8 As New TableCell()
                    tc8.Controls.Add(lblGoingto)

                    Dim ddGoingto As New DropDownList()
                    ddGoingto.ID = "ddGoingto" & i.ToString()
                    ddGoingto.AutoPostBack = True
                    AddHandler ddGoingto.SelectedIndexChanged, AddressOf OnSelectIndexChanged
                    Dim lst As New ListItem()
                    Dim lst1 As New ListItem()
                    Dim lst2 As New ListItem()
                    lst.Text = "Select"
                    lst.Value = "0"
                    lst1.Text = "Supplier"
                    lst1.Value = "1"
                    lst2.Text = "Customer"
                    lst2.Value = "2"
                    ddGoingto.Items.Add(lst)
                    ddGoingto.Items.Add(lst1)
                    ddGoingto.Items.Add(lst2)
                    ddGoingto.Width = 130
                    Dim tc9 As New TableCell()
                    tc9.Controls.Add(ddGoingto)

                    Dim lblLocationsc As New Label()
                    lblLocationsc.ID = "lblLocationsc" & i.ToString()
                    lblLocationsc.Text = "Company"
                    Dim tc10 As New TableCell()
                    tc10.Controls.Add(lblLocationsc)



                    Dim ddSC As New DropDownList()
                    ddSC.ID = "ddSC" & i.ToString()
                    'ddSC.AutoPostBack = True
                    'AddHandler ddSC.SelectedIndexChanged, AddressOf OnSelectIndexChanged1
                    Dim lstt As New ListItem()
                    lstt.Text = "Select"
                    lstt.Value = "0"
                    ddSC.Items.Add(lstt)
                    ddSC.Width = 130
                    Dim tc11 As New TableCell()
                    tc11.Controls.Add(ddSC)


                    Dim btnsubmit As New Button()
                    btnsubmit.Text = "Submit"
                    btnsubmit.ID = "btnsubmit" & i.ToString()
                    AddHandler btnsubmit.Click, AddressOf btnsubmit_click
                    Dim tc12 As New TableCell()
                    tc12.Controls.Add(btnsubmit)



                    tr.Controls.Add(tc)
                    tr.Controls.Add(tc1)

                    tr7.Controls.Add(tc15)
                    tr7.Controls.Add(tc16)

                    tr1.Controls.Add(tc2)
                    tr1.Controls.Add(tc3)

                    tr2.Controls.Add(tc4)
                    tr2.Controls.Add(tc5)

                    tr3.Controls.Add(tc6)
                    tr3.Controls.Add(tc7)

                    tr4.Controls.Add(tc8)
                    tr4.Controls.Add(tc9)

                    tr5.Controls.Add(tc10)
                    tr5.Controls.Add(tc11)

                    Dim tc14 As New TableCell()
                    tr6.Controls.Add(tc14)
                    tr6.Controls.Add(tc12)

                    tb.Controls.Add(tr)
                    tb.Controls.Add(tr7)
                    tb.Controls.Add(tr1)
                    tb.Controls.Add(tr2)
                    tb.Controls.Add(tr3)
                    tb.Controls.Add(tr4)
                    tb.Controls.Add(tr5)
                    tb.Controls.Add(tr6)

                    Dim tcc As New TableCell()
                    tcc.Controls.Add(tb)
                    If j > 5 And j < 11 Then
                        If j = 11 Or j = 6 Then
                            trmain1 = New TableRow()
                        End If
                        trmain1.Controls.Add(tcc)
                        tbmain.Controls.Add(trmain1)
                    Else
                        trmain.Controls.Add(tcc)
                        tbmain.Controls.Add(trmain)
                    End If
                    tb = New Table()
                    tb.BorderColor = Drawing.Color.SkyBlue
                    tb.BorderWidth = 1
                    tcl.Controls.Add(tbmain)
                    trl.Cells.Add(tcl)
                    tbl.Rows.Add(trl)
                    tbl.EnableViewState = True
                    ViewState("tbl") = True
                Next

            End If
        End If
    End Sub






Private Sub OnSelectIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Try
            Dim ddl As DropDownList = DirectCast(sender, DropDownList)
            Dim ID As String = ""
            ID = ddl.ID
            Dim cid As String = ""
            cid = ddl.ID.Replace("ddGoingto", "")
            Dim type As Int64 = 0
            type = ddl.SelectedValue

            Dim ddSC As New DropDownList()
            ddSC = CType(pnllane.FindControl("ddSC" & cid), DropDownList)
            If type = "1" Then
                Dim cmd As New SqlCommand
                cmd.CommandType = CommandType.Text
                cmd.CommandText = "select Supplier_id,Supplier_name from Supplier where client_id=" & Convert.ToInt64(Session("laneclientId"))
                Worker.combofill(cmd, ddSC, "Supplier_id", "Supplier_name")
            Else
                Dim cmd As New SqlCommand
                cmd.CommandType = CommandType.Text
                cmd.CommandText = "select customer_id,customer_name from customer where Client_id=" & Convert.ToInt64(Session("laneclientId"))
                Worker.combofill(cmd, ddSC, "customer_id", "customer_name")
            End If
            'End If
        Catch ex As Exception
            pnlGlobalMessage.Visible = True
            lblGlobalMessage.Text = "A technical issue has occurred.  A message has been sent to the development team.  Sorry for the inconvenience."
            lblGlobalMessage.Visible = True
            Dim dt As DataTable = Session("user_session")
            Worker.HandleError(ex, "Lane Assignment", Int64.Parse(dt.Rows(0)("user_id")), Request.Browser.Browser.ToString())
        End Try
    End Sub




Protected Overrides Function SaveViewState() As Object
        Dim newViewState As Object() = New Object(1) {}

        Dim txtValues As New List(Of String)()

        For Each row As TableRow In tbl.Controls
            For Each cell As TableCell In row.Controls
                For Each tb As Table In cell.Controls
                    For Each rw As TableRow In tb.Controls
                        For Each cl As TableCell In rw.Controls
                            For Each tbb As Table In cl.Controls
                                For Each rww As TableRow In tbb.Controls
                                    For Each cll As TableCell In rww.Controls
                                        If cll.Controls.Count <> 0 Then
                                            If TypeOf cll.Controls(0) Is DropDownList Then
                                                txtValues.Add(DirectCast(cll.Controls(0), DropDownList).SelectedIndex.ToString())
                                            End If
                                        End If
                                    Next
                                Next
                            Next
                        Next
                    Next
                Next
            Next
        Next

        newViewState(0) = txtValues.ToArray()
        newViewState(1) = MyBase.SaveViewState()
        Return newViewState
    End Function
    Protected Overrides Sub LoadViewState(ByVal savedState As Object)

        'if we can identify the custom view state as defined in the override for SaveViewState
        If TypeOf savedState Is Object() AndAlso DirectCast(savedState, Object()).Length = 2 AndAlso TypeOf DirectCast(savedState, Object())(0) Is String() Then
            Dim newViewState As Object() = DirectCast(savedState, Object())
            Dim txtValues As String() = DirectCast(newViewState(0), String())
            If txtValues.Length > 0 Then
                're-load tables
                If Session("lanefacilityId") IsNot Nothing Then
                    LoadLaneAssigmnentDoors(Convert.ToInt64(Session("lanefacilityId")))
                    Dim i As Integer = 0
                    For Each row As TableRow In tbl.Controls
                        For Each cell As TableCell In row.Controls
                            For Each tb As Table In cell.Controls
                                For Each rw As TableRow In tb.Controls
                                    For Each cl As TableCell In rw.Controls
                                        For Each tbb As Table In cl.Controls
                                            For Each rww As TableRow In tbb.Controls
                                                For Each cll As TableCell In rww.Controls
                                                    If cll.Controls.Count <> 0 Then
                                                        If TypeOf cll.Controls(0) Is DropDownList AndAlso i < txtValues.Length - 1 Then
                                                            DirectCast(cll.Controls(0), DropDownList).SelectedIndex = Convert.ToInt32(txtValues(System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)))
                                                        End If
                                                    End If
                                                Next
                                            Next
                                        Next
                                    Next
                                Next
                            Next
                        Next
                    Next
                End If

            End If
            'load the ViewState normally
            MyBase.LoadViewState(newViewState(1))
        Else
            MyBase.LoadViewState(savedState)
        End If
    End Sub

Category:

About http://dotnetvisual.blogspot.in/:
DOT NET TO ASP.NET is a web application framework marketed by Microsoft that programmers can use to build dynamic web sites, web applications and web services. It is part of Microsoft's .NET platform and is the successor to Microsoft's Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime, allowing programmers to write ASP.NET code using any Microsoft .NET language. create an application very easily ....