Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi all, I have changed my code very samller than before and used arrays to fill tabs instead of DB data. You can remove the cssClass and stylesheet from the html page in order to work for you. Hope that helps you to help me:)

Note: I know the code is long but if you copy the code into your visual studio and run it, you can see what I experience here. Problem is in the InsertRowsToTables function where textboxes are xreated for the first time but do not appear after the first tab visit..

It started with creating a tab control in VB.net and I got some ideas online and changed them to my own version. Everything was fine except when I was going to a tab for the second time (or anytime after the first visit to that tab), the changes I made to its content (textboxes) did not exist. I found that the page is loading everytime I visit a tab and data would be refreshed from DB. So I changed my tab deisgn from Mutilview to Panels first. Then I did not load all data into my tabs on page load. The first tab was loaded from database on page load and when user was clicking a new tab, then its data was being loaded to that tab. I used a session variable this time to keep trak of number of visits to that tab stop loading data more than one time. So if it was more than once, I stopped loading data from DB. Well, it worked partially and changes to each textbox was there after I visited each tab several times.

Problem: In some of tabs, I use a function to loop thru several records and show them, like several Contact information for each person will be listed in group of data under that tab. The first contact textboxes is already in a <asp:table> in my page and if there is no contact info in DB, I just show the empty boxes along with their labels. But other records are created dynamically using TableRows and TableCells that are added to the existing <asp:table> in my page. Now, I can not see more than one record for the Contact tab and the rest of information disappears! Anyone knows how to slove this? Thanks in advance.


Here are some codes that might help you:



HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title>Test File</title>
    <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1"  runat="server">
<div>
<asp:Menu ID="TabMenu"  runat="server" cssClass="TabClass"  orientation="Horizontal" StaticEnableDefaultPopOutimage="false" OnMenuItemClick="TabMenu_MenuItemClick" Visible="false" ViewStateMode="Inherit">
<Items>
<asp:MenuItem  text=" "  Value="0"> </asp:MenuItem>
<asp:MenuItem  text=" " Value="1"></asp:MenuItem>
</Items>
</asp:Menu> 

<asp:Panel id="Panel1" runat="server" Visible="False">
        <table>
            <tr valign="top">
                <td id="GItd" class="TabArea"  >
                    <table id="GenInfotbl"  runat="server" cellspacing="10" >
                    <tr id="Tr2"  runat="server" >
                    <td>
                        <asp:Label ID="FNamelbl" cssClass="bold" runat="server" Text="First Name:" Width="200"></asp:Label>
                    </td>
                    <td><asp:TextBox ID="FNametxtbox" runat="server" Width="200"></asp:TextBox>
                    </td>
                    </tr>
                    </table>
                </td>
            </tr>
        </table>
</asp:Panel>

<asp:Panel id="Panel2" runat="server" Visible="False">
        <table>
            <tr valign="top">
                <td id="Ctd" class="TabArea" >
                    <asp:Table ID="CtblASP" runat="server" cellspacing="10">
                    <asp:TableRow>
                    <asp:TableCell>
                    <asp:Label ID="CLNamelbl1" cssClass="bold" runat="server" Text="Last Name:" Width="200"></asp:Label>
                    </asp:TableCell>
                    <asp:TableCell>
                    <asp:TextBox ID="CLNametxtbox1" runat="server"  Width="200"></asp:TextBox>
                    </asp:TableCell>
                    <asp:TableCell>
                    <asp:Label  ID="CHPhonelbl1" cssClass="bold" runat="server" Text="Home Phone:" Width="150"></asp:Label>
                    </asp:TableCell>
                    <asp:TableCell>
                    <asp:TextBox ID="CHPhonetxtbox1" runat="server"  Width="200"></asp:TextBox>
                    </asp:TableCell>
                    </asp:TableRow>
                    </asp:Table>
                </td>
            </tr>
        </table>
    </asp:Panel>
    </div>
    </form>
</body>
</html>


and in code section, I have :

VB
Public Class testTab
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        TabMenu.Items(0).Text = "" & "General Information |" & ""
        TabMenu.Items(1).Text = " Contact"
        Dim QStr
        QStr = 3
        Fillpanel1(QStr)

        FNametxtbox.Focus()
        Panel1.Visible = True
    End Sub

    Protected Function Fillpanel1(QStr As Integer)
        'Display General Information 
        Dim i
        TabMenu.Visible = True

        If Not IsPostBack Then

            Dim myarr(2, 1)
            myarr(0, 0) = "1"
            myarr(0, 1) = "Larry"
            myarr(1, 0) = "2"
            myarr(1, 1) = "Mike"
            myarr(2, 0) = "3"
            myarr(2, 1) = "Tim"

            For i = 0 To UBound(myarr)
                If myarr(i, 0) = QStr Then
                    FNametxtbox.Text = myarr(i, 1)
                End If
            Next
        End If
        Return True
    End Function

    Protected Sub TabMenu_MenuItemClick(ByVal sender As Object, _
         ByVal e As MenuEventArgs) Handles TabMenu.MenuItemClick

        Dim QStr = 3
        Panel1.Visible = False
        Panel2.Visible = False

        Select Case Int32.Parse(e.Item.Value)
            Case 0
                Panel1.Visible = True
                Session("Panel1") = Session("Panel1") + 1
                Fillpanel1(QStr)
            Case 1
                Session("Panel2") = Session("Panel2") + 1
                Panel2.Visible = True
                'Problem happens when this function is called for the second time                
                Fillpanel2()
        End Select
        'Disable the tab menuitem if user is not "V" Here I change the selected tab color
    End Sub

    Protected Function Fillpanel2()
        'Display Contact data

        If Session("Panel2") < 2 Then
            'here the tab is visited for the first time. So get data from DB and display them. I am wondering why the created cells does not stay on the page after the first tab visit.

            Dim cellCnt
            Dim cellCtr
            Dim lastCellItem
            Dim TabTableName As Table

            'Number of cells to be created if more than one row found
            cellCnt = 2
            cellCtr = 1
            lastCellItem = "Home Phone:"
            TabTableName = CtblASP

            Dim myarrContact(3, 1) As String
            myarrContact(0, 0) = "Wilson"
            myarrContact(0, 1) = "1-222-333-4444"
            myarrContact(1, 0) = "Miller"
            myarrContact(1, 1) = "1-333-444-5555"
            myarrContact(2, 0) = "Shoemaker"
            myarrContact(2, 1) = "1-444-555-6666"

            For i = 0 To 3
                If i > 0 Then
                    'Here is the case we have more than one record in Contact table
                    'So we need to add extra set of data/boxes for each new row in database/array
                    If i = 3 Then
                        Exit For
                    End If
                    InsertRowsToTables(TabTableName, i, cellCtr, cellCnt, myarrContact)
                Else
                    'The first record found in Contact table. We have the form boxes ready to be filled.
                    CLNametxtbox1.Text = myarrContact(0, 0)
                    CHPhonetxtbox1.Text = myarrContact(0, 1)
                End If
            Next
        End If
        Return True
    End Function
    Protected Function InsertRowsToTables(TabTableNameF As Table, iFun As Integer, cellCtrF As Integer, cellCntF As Integer, myarrContact As Array)

        Dim j As Integer

        For cellCtrF = 1 To cellCntF
            Dim tRow As New TableRow()
            j = cellCtrF - 1
            Dim tCell As New TableCell()
            ' Assign Label Text
            Select Case j
                Case 0
                    tCell.Text = "Last name:"
                Case 1
                    tCell.Text = "Home Phone:"
            End Select
            'Assign Label ID
            Select Case j
                Case 0
                    tCell.ID = "CLNamelbl" & iFun + 1
                Case 1
                    tCell.ID = "CHPhonelbl" & iFun + 1
            End Select
            tCell.Width = 150
            tRow.Cells.Add(tCell)
            'here is a new td cell that we are going to add a textbox control(tb) to it
            Dim tCellTxt As New TableCell()
            'By declaring and assigning the new textbox, we will prevent the runtime error
            Dim tb As TextBox = New TextBox
            'We will assign a value from DB to the new textbox control
            If Len(Convert.ToString(myarrContact(iFun, j))) = 0 Then
                tb.Text = ""
            Else
                tb.Text = myarrContact(iFun, j)
            End If
            'Both new textbox tb and new td cell would have a width of 200
            tb.Width = 200
            tCellTxt.Width = 200
            'Add a new textbox control to the new cell
            tCellTxt.Controls.Add(tb)
            'Assign an ID to the new textbox; tb.
            Select Case j
                Case 0
                    tb.ID = "CLNametxtbox" & iFun + 1
                Case 1
                    tb.ID = "CHPhonetxtbox" & iFun + 1
            End Select
            'Add the cell to the row
            tRow.Cells.Add(tCellTxt)
            'we need to force a new row to be created after each line (with two set of name/value pairs)
            If cellCtrF Mod 2 = 0 Then
                Dim tRowNL As New TableRow()
                Dim tCellNL As New TableCell()
                tCellNL.Text = ""
                tRowNL.Cells.Add(tCellNL)
                TabTableNameF.Rows.Add(tRowNL)
            ElseIf cellCtrF = cellCntF Then
                'Here we finished with our last label/value set. we need to show it here.
                TabTableNameF.Rows.Add(tRow)
            Else
                'Here only one name/value pair is added to the line. We need to add one more pair.
                'Add one to the loop var and continue with adding one more set of name/values
                cellCtrF = cellCtrF + 1
                If cellCtrF <= cellCntF Then
                    'After we make sure we have not reached to the end of cells that need to be displayed, we
                    'can add one more label and textbox
                    j = cellCtrF - 1
                    'Here is the label cell with its text and ID assigned to it
                    Dim tCell2 As New TableCell()
                    'Assign a text to the second label in the row/line
                    Select Case j
                        Case 0
                            tCell2.Text = "Last name:"
                        Case 1
                            tCell2.Text = "Home Phone:"
                    End Select
                    'Assign an ID to the label.
                    Select Case j
                        Case 0
                            tCell2.ID = "CLNamelbl" & iFun + 1
                        Case 1
                            tCell2.ID = "CHPhonelbl" & iFun + 1
                    End Select
                    tCell2.Width = 150
                    tRow.Cells.Add(tCell2)
                    'here is a new td cell that we are going to add a textbox control(tb) to it
                    Dim tCellTxt2 As New TableCell()
                    'By declaring and assigning the new textbox, we will prevent the runtime error
                    Dim tb2 As TextBox = New TextBox
                    tb2.Width = 200
                    'We will assign a value from array to the new textbox control
                    If Len(Convert.ToString(myarrContact(iFun, j))) = 0 Then
                        tb2.Text = ""
                    Else
                        tb2.Text = myarrContact(iFun, j)
                    End If
                    tCellTxt2.Width = 200
                    'Add textbox control to the tablecell
                    tCellTxt2.Controls.Add(tb2)
                    'Assign an ID to the new textbox; tb2.
                    Select Case j
                        Case 0
                            tb.ID = "CLNametxtbox" & iFun + 1
                        Case 1
                            tb.ID = "CHPhonetxtbox" & iFun + 1
                    End Select
                    tRow.Cells.Add(tCellTxt2)
                    TabTableNameF.Rows.Add(tRow)
                End If
            End If
        Next
        Return True
    End Function

End Class
Posted
Updated 12-Jul-12 4:22am
v9
Comments
Sergey Alexandrovich Kryukov 9-Jul-12 15:46pm    
How can we know where did you screw up? How about a short but complete code sample to reproduce the problem? If you can make it, use "Improve question".
--SA
Sandeep Mewara 10-Jul-12 10:02am    
Could you just post the issue related code snippet?
[no name] 10-Jul-12 10:33am    
Adding all this unneccessary code confuses the problem even more than the confusing narrative.
Sergey Alexandrovich Kryukov 10-Jul-12 13:55pm    
I would say, too much code. Could you focus only on the particular issue? Perhaps, create a special code sample, just to reproduce one problem. And please format it, using <pre lang="?">...</pre>.
--SA
[no name] 12-Jul-12 10:42am    
"if you copy the code into your visual studio and run it"... no thank you. I am not paid to debug your code for you.

1 solution

Using Page_load does not do the job here. Use Page_Init to fill tabs in order to see the results after using tabs several times.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900