Adding Rows & Cells to Dynamic Table

FDisk

Newcomer
Joined
Apr 17, 2004
Messages
9
Web Directory (Problems with dynamic table)

I'm trying to create a web directory. Basically I have a list box with a list of categories, a "Go" button next to it and a table underneath that lists the companies based on category.

When the user picks a category, the table is populated with the information from an Access DB by category.

I have encountered 2 problems so far:

1) I have successfully managed to get the table populated with the category in Index 1 of the listbox (Accounting). The table populates as it should, however, the cell sizes are all over the place and it looks really ugly.

Is there a way I can change the cell width after it's created?

2) The "Go" button doesn't work.

Do I have to create a new page every time the user picks a new category and presses "Go" or can I somehow refresh the same page, clear the table and repopulate it with the new category?

I have included my code:
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        lboCategory.Items.Clear() 'Clears the listbox

        objOleDbConnection.Open() 'Opens the DB connection

        Dim objReader As OleDbDataReader = Me.objDropBox.ExecuteReader 'Declares and executes the reader

        Do While objReader.Read()
            lboCategory.Items.Add(objReader("category").ToString) 'Populates the list box
        Loop

        objReader.Close() 'Closes reader
        objOleDbConnection.Close() 'Closes connection

        lboCategory.SelectedIndex = 1 'Selects the first category
        lblCategory.Text = lboCategory.Value.ToString 'Gives value to the category label

        PopulateTable() 'Calls the PopulateTable procedure
    End Sub

    Private Sub PopulateTable()

        tblDirectory.Rows.Clear()

        Dim intRowIndex As Integer 'Will keep track of rows added
        Dim intCellCount As Integer 'Will keep track of cells added

        objReadData.Parameters("category").Value = lboCategory.Value.ToString 'Sets the value for the category parameter

        objOleDbConnection.Open() 'Opens the connection

        Dim objReader As OleDbDataReader = Me.objReadData.ExecuteReader 'Executes the reader

        Do While objReader.Read()
            Dim tRow As New TableRow 'Creates new rows

            tblDirectory.Rows.Add(tRow) 'Adds 1 row

            Do While intCellCount < 10 'Adds 9 Cells
                Dim tCell As New TableCell 'Creates a new cell
                tRow.Cells.Add(tCell) 'Adds 1 Cell
                intCellCount += 1 'Counter
            Loop

            tblDirectory.Rows(intRowIndex).Cells(0).Text = objReader("compname").ToString
            tblDirectory.Rows(intRowIndex).Cells(1).Text = objReader("address").ToString
            tblDirectory.Rows(intRowIndex).Cells(2).Text = objReader("city").ToString
            tblDirectory.Rows(intRowIndex).Cells(3).Text = objReader("state").ToString
            tblDirectory.Rows(intRowIndex).Cells(4).Text = objReader("zip").ToString
            tblDirectory.Rows(intRowIndex).Cells(5).Text = objReader("phone").ToString
            tblDirectory.Rows(intRowIndex).Cells(6).Text = objReader("fax").ToString
            tblDirectory.Rows(intRowIndex).Cells(7).Text = objReader("email").ToString
            tblDirectory.Rows(intRowIndex).Cells(8).Text = objReader("title").ToString & " " & objReader("fname").ToString _
            & " " & objReader("mname").ToString & " " & objReader("lname")
            tblDirectory.Rows(intRowIndex).Cells(9).Text = objReader("website").ToString

            intRowIndex += 1
            intCellCount = 0
        Loop

        objOleDbConnection.Close()

    End Sub

    Private Sub btnGo_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.ServerClick
        lblCategory.Text = lboCategory.Value.ToString 'Renames the label
        PopulateTable() 'Repopulates the table
    End Sub

Thanks in advance for any help
 
Last edited:
kahlua001 said:
Are you checking for IsPostBack in your load event?

No, I'm really new to VB.NET, all I've taken is a VB1 class and most of the stuff I've used in this web app is self-taught. So I really don't know what you are talking about, could you elaborate or give me some useful links I could follow?

Thanks for the help.
 
In your page_load event, check for ispostback...

If Not IsPostBack Then
'First request of the page, do stuff such as initial population of lists, etc..
Else
'The page has been posted back, no need to do initial stuff becuase it has been done already.
End If

Any search online for Page.IsPostBack will yield many results. My suggestion to you is to get a good understanding of asp.net's page cycle, it will save you alot of headaches in the future debugging code.
 
Back
Top