Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Datagrid cell returning "" in ItemDataBound

Author  Topic 

Billkamm
Posting Yak Master

124 Posts

Posted - 2006-03-21 : 15:19:33
I have a problem where my cells in the datagrid are returning "" in the ItemDataBound event. When there is data in the cells, but in my code both e.Item.Cells(5).Text and CType(e.Item.Cells(5).Controls(0), LiteralControl).Text. Does anyone have any ideas? Here is my code below.


Private Sub dgLicenses_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgLicenses.ItemDataBound
Dim strExpiryDate As String

' Set any expired documents to red. Skip over the header and footer rows.
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
' Get the expiration date of the current row
If e.Item.ItemIndex <> dgLicenses.EditItemIndex Then
REM - why is these returning ""?
strExpiryDate = CType(e.Item.Cells(5).Controls(0), LiteralControl).Text
strExpiryDate = Trim(e.Item.Cells(5).Text)
Else
strExpiryDate = Trim(CType(e.Item.Cells(5).Controls(0), TextBox).Text)
End If

' Set the background color to red if the document is expired
If IsDate(strExpiryDate) Then
If CDate(strExpiryDate) > System.DateTime.Today Then
e.Item.BackColor = System.Drawing.Color.Red
End If
End If
End If
End Sub

Billkamm
Posting Yak Master

124 Posts

Posted - 2006-03-21 : 16:47:23
CType(e.Item.Cells(5).Controls(1), Label).Text solved my problem. Since I'm using a template column it is creating a Label (a.k.a. SPAN tags) in the table cell instead of just putting the text directly into the cell.

Controls(0) didn't work for some reason because it returned "". Most likely the non-existent space between the TD and SPAN tags is the that literal control
Go to Top of Page
   

- Advertisement -