Problem with background color of table cell

T

Thanks

I have a routine that is called on Page_Init. It retrieves folder records
from a database which I display as Link Buttons in a table cell. I set the
table cell's bgcolor to a default color (say black for example). I am
dynamically creating the LinkButton controls and adding them into the table
cell and I've also hooked up an event handler for each LinkButton's Click
event. This all works fine.

Now in the Link Button's Click event handler, I set the background of the
LinkButton's parent (which is the table cell I initially color Black) to
white. This too works fine. My problem occurs when I click another
LinkButton. The table cell it resides also turns white, but the previous one
doesn't restore back to black. Can someone help? I've posted the code below.
I figured that the click would cause a trip to the server, which would call
page_init which inturn would call DisplayFolders which would set the table
cell to black.

Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls

Public Class StoredProcedures : Inherits System.Web.UI.Page

#Region " Variable Declarations "

Private SelectedFolder As String = ""
Private designerPlaceholderDeclaration As System.Object
Protected WithEvents Form1 As System.Web.UI.HtmlControls.HtmlForm
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents FolderList As
System.Web.UI.HtmlControls.HtmlTableCell
Protected WithEvents FolderContents As
System.Web.UI.HtmlControls.HtmlTableCell

Private ConnectionString As String = "Data Source=localhost;" & _
"Initial Catalog=Yahoo;" & _
"User Id=sa;" & _
"Password=lvteopeh;" & _
"Connect Timeout=15;" & _
"Network Library=dbmssocn;"

#End Region

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
InitializeComponent()

End Sub

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

#End Region

#Region " Methods / Properties "

Public Sub DisplayFolders()

' Declarations.
Dim ds As DataSet
Dim cm As SqlCommand
Dim cn As SqlConnection
Dim da As SqlDataAdapter

Dim intFolderCount As Long

' Initialization.
ds = New DataSet
da = New SqlDataAdapter
cn = New SqlConnection(ConnectionString)
cm = New SqlCommand("GetFolders", cn)

' **************************************************
' Setup the command object (Type & Parameters)
' **************************************************
With cm
.CommandType = CommandType.StoredProcedure
.Parameters.Add("@UserID", SqlDbType.Int).Direction =
ParameterDirection.Input
.Parameters.Add("@FolderCount", SqlDbType.Int).Direction =
ParameterDirection.Output
.Parameters.Add("@Return_Value", SqlDbType.Int).Direction =
ParameterDirection.ReturnValue
.Parameters("@UserID").Value = 2
End With

' Set the command to execute to the stored procedure command object.
da.SelectCommand = cm

' Fill our dataset and place the results into a temporary table
namespace, "Results"
da.Fill(ds, "Results")

' **************************************************
' Create the table and display the folders.
' **************************************************
CreateFolderTable(ds)

cn.Close()
ds.Dispose()
da.Dispose()
cn.Dispose()
cm.Dispose()

End Sub

Public Sub DisplayFolderContents(ByVal FolderID As Integer)

' Declarations.
Dim ds As DataSet
Dim cm As SqlCommand
Dim cn As SqlConnection
Dim da As SqlDataAdapter

Dim intFolderCount As Long

' Initialization.
ds = New DataSet
da = New SqlDataAdapter
cn = New SqlConnection(ConnectionString)
cm = New SqlCommand("GetNotes", cn)

' **************************************************
' Setup the command object (Type & Parameters)
' **************************************************
With cm
.CommandType = CommandType.StoredProcedure
.Parameters.Add("@intUser", SqlDbType.Int).Direction =
ParameterDirection.Input
.Parameters.Add("@intFolder", SqlDbType.Int).Direction =
ParameterDirection.Input
.Parameters.Add("@intNotesCount", SqlDbType.Int).Direction =
ParameterDirection.Output
.Parameters.Add("@Return_Value", SqlDbType.Int).Direction =
ParameterDirection.ReturnValue
.Parameters("@intUser").Value = 1
.Parameters("@intFolder").Value = 1
End With

' Set the command to execute to the stored procedure command object.
da.SelectCommand = cm

' Fill our dataset and place the results into a temporary table
namespace, "Results"
da.Fill(ds, "Results")

' **************************************************
' Create the table and display the folder's notes.
' **************************************************
CreateFolderContentsTable(ds)

cn.Close()
ds.Dispose()
da.Dispose()
cn.Dispose()
cm.Dispose()

End Sub

Private Sub CreateFolderTable(ByRef ds As DataSet)

Dim dr As DataRow
Dim dt As DataTable
Dim lnk As LinkButton
Dim tbl As New HtmlTable
Dim intFolderCount As Integer

' Get the number of folders returned.
intFolderCount = ds.Tables(0).Rows.Count()

' Setup the table object properties.
With tbl
.Width = "200"
.CellSpacing = "1"
.CellPadding = "1"
End With

' Create an "All" folder for each user.
CreateFolderRow(tbl, -1, "All")

' If there were rows, display the folders.
If intFolderCount > 0 Then

' Get a reference to the results table in the dataset.
dt = ds.Tables(0)

' Loop through each row in the dataset and display it to page.
For Each dr In dt.Rows

CreateFolderRow(tbl, CType(dr(0), Integer), CType(dr(1),
String))

Next

' Now, add this table to the table cell we've designated to hold
the folders table.
FolderList.Controls.Add(tbl)

End If

dr = Nothing
dt = Nothing
tbl = Nothing

End Sub

Private Sub CreateFolderContentsTable(ByRef ds As DataSet)

Dim dr As DataRow
Dim dt As DataTable
Dim lnk As LinkButton
Dim tr1 As HtmlTableRow
Dim tc1 As HtmlTableCell
Dim tc2 As HtmlTableCell
Dim tbl As New HtmlTable
Dim intFolderCount As Integer

' Get the number of folders returned.
intFolderCount = ds.Tables(0).Rows.Count()

' If there were rows, display the folders.
If intFolderCount > 0 Then

' Get a reference to the results table in the dataset.
dt = ds.Tables(0)

With tbl
.Width = "200"
.CellSpacing = "1"
.CellPadding = "1"
End With

' Loop through each row in the dataset and display it to page.
For Each dr In dt.Rows

tr1 = New HtmlTableRow
tc1 = New HtmlTableCell
tc2 = New HtmlTableCell

' Created a padded cell to give the folder list a padded
feel.
With tc1
.Width = "20"
.Align = "right"
.Controls.Add(New LiteralControl("&nbsp;"))
End With

' Create a new link button.
lnk = New LinkButton

' Add a property to hold the SQL ID of the folder.
lnk.Attributes.Add("fid", Trim(dr(0)))
lnk.ForeColor = Color.Black

' Add a Click event handler.
AddHandler lnk.Click, AddressOf FolderItem_Click

' Set the text to be the folder's name.
lnk.Text = Trim(dr(1))

' Display the link.
With tc2
.Width = "180"
.Align = "left"
.Controls.Add(lnk)
.Style.Add("font-family", "verdana")
.Style.Add("font-size", "10px")
End With

' Add the cell to the table row.
With tr1
.Cells.Add(tc1)
.Cells.Add(tc2)
End With

' Add the table row to the table.
tbl.Rows.Add(tr1)

Next

' Now, add this table to the table cell we've designated to hold
the folders table.
FolderList.Controls.Add(tbl)

Else

tr1 = New HtmlTableRow
tc1 = New HtmlTableCell

' We want to display one row with "No Folders".
With tc1
.InnerHtml = "No Folders"
.Width = 200
End With

' Add the cell to the table row.
tr1.Cells.Add(tc1)

' Add the table row to the table.
tbl.Rows.Add(tr1)

End If

tr1 = Nothing
tc1 = Nothing
tc2 = Nothing
tbl = Nothing

' Clean up our table iteration objects.
dr = Nothing
dt = Nothing

End Sub

Private Function CreateLinkButton(ByVal ID As Integer, ByVal Text As
String) As Control

Dim lnk As LinkButton

' Create a new link button.
lnk = New LinkButton

' Add a property to hold the SQL ID of the folder.
lnk.Attributes.Add("fid", ID)

' Set the text for the item black.
lnk.ForeColor = Color.Black

' Set the text to be the folder's name.
lnk.Text = Text

Return lnk

End Function

Private Sub CreateFolderRow(ByRef tbl As HtmlTable, ByVal ID As Integer,
ByVal Text As String)

Dim tr1 As HtmlTableRow
Dim tc1 As HtmlTableCell
Dim tc2 As HtmlTableCell
Dim lnk As LinkButton

tr1 = New HtmlTableRow
tc1 = New HtmlTableCell
tc2 = New HtmlTableCell

' Created a padded cell to give the folder list a padded feel.
With tc1
.Width = "20"
.Align = "right"
.BgColor = "#d5d0cc"
.Controls.Add(New LiteralControl("&nbsp;"))
If ID.ToString = SelectedFolder Then .BgColor = "#FFFFFF"
End With

' Display the link.
With tc2

.Width = "180"
.Align = "left"
.BgColor = "#d5d0cc"
.Style.Add("font-size", "10px")
.Style.Add("font-family", "verdana")

' Create a link button for this folder.
lnk = CreateLinkButton(ID, Text)

' Hook up the plumbing so we can handle click events.
AddHandler lnk.Click, AddressOf FolderItem_Click

' Add the link button to the table's controls collection.
.Controls.Add(lnk)

' Change the background of the item if this is the selected
folder.
If ID.ToString = SelectedFolder Then .BgColor = "#FFFFFF"

End With

' Add the cell to the table row.
With tr1
.Cells.Add(tc1)
.Cells.Add(tc2)
End With

' Add the table row to the table.
tbl.Rows.Add(tr1)

tr1 = Nothing
tc1 = Nothing
tc2 = Nothing

End Sub

#End Region


#Region " Events "

Private Sub FolderItem_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

Dim strID As String = ""
Dim lb As LinkButton = CType(sender, LinkButton)

' Get the FID attribute of the folder link button.
strID = lb.Attributes.Item("FID").ToString()

Dim tc As HtmlTableCell
tc = lb.Parent
If Not (tc Is Nothing) Then tc.BgColor = "#ffffff"


' Set the selected folder.
SelectedFolder = strID

' If the ID is numeric, display the folder's contents.
If IsNumeric(strID) Then DisplayFolderContents(CType(strID,
Integer))

End Sub

#End Region

End Class
 
T

Thanks

I hate it when I figure things out after writing a lengthy post!
Setting the table's EnableViewState property to false rectified the problem.
Thanks

------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top