Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid cell returning "" in ItemDataBound

Status
Not open for further replies.

Billkamm

Programmer
Feb 9, 2006
74
US
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.

Code:
    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
 
What are you trying to do here?
If e.Item.ItemIndex <> dgLicenses.EditItemIndex Then

If you are doing this on the itemdatabound, then there is no edititemindex.

Jim
 
Whenever I postback after clicking the "edit" button the datagrid is rebound to teh dataset, so I have to determine if the row has literal controls or textboxes
 
Try using:
Code:
If e.Item.ItemType <>ListItemType.EditItem Then

End If
 
all the cells are still returning "". I stepped through this one row at a time and it skips over the header and footer and when it goes into the only row that is to be displayed for that page every cell is "", but when the page loads every cell has text in it.
 
It enters the block with:
strExpiryDate = Trim(e.Item.Cells(5).Text)

Which is what I'm expecting. I'm just not expecting e.Item.Cells(5).Text to be ""
 
I am unclear as too why you have these 2 lines:
Code:
strExpiryDate = CType(e.Item.Cells(5).Controls(0), LiteralControl).Text
strExpiryDate = Trim(e.Item.Cells(5).Text)

I think you just need this:
Code:
strExpiryDate = Trim(e.Item.Cells(5).Text)

Also, make sure your cell index is correct.
 
The cells index is correct. I had the two lines because I was trying different approaches. You are correct I should only need the second line.

This is the rendered code. The table cell with 3/10/2006 is what I am trying to get at

Code:
<table cellspacing="0" cellpadding="2" rules="all" border="1" id="dgLicenses" style="border-collapse:collapse;">
	<tr style="color:White;background-color:SteelBlue;">
		<td>State</td><td>Status</td><td>Name On License</td><td>License Number</td><td>Issue Date</td><td>Expiry Date</td><td>&nbsp;</td>
	</tr><tr>
		<td>
<span>NC</span>
</td><td>
<span id="dgLicenses__ctl2_Label1">Not Received</span>
</td><td>
<span>My Company</span>
</td><td>
<span>AF48467635GW537GH</span>
</td><td>
<span>2/10/2006</span>
</td><td>
<span>3/10/2006</span>
</td><td>
<a href="javascript:__doPostBack('dgLicenses$_ctl2$_ctl5','')">{ Edit }</a>
</td>
	</tr>
</table>
 
I am stummped, it worked in my test. It should work for you. Why are you trimming the date? I don't think you need too.
 
You are correct I don't need to. I just got so used to using Trim() around anything that ended in .Text, because I pull a lot of information from textboxes on the web page that I didn't even think before I added it there.

I have a watch on e.Item.Cells() though and every index of cells has .Text = "" throughout the entire time I am in _ItemDataBound.

Every example I have found on the web uses e.Item.Cells().Text, so I'm completely lost right now. It is probably something extremely simple that I'm missing. :(
 
This is my code for the datagrid if that helps:

Code:
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Expiry Date">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Format(DataBinder.Eval(Container, "DataItem.ExpirationDate"), "Short Date") %>'></asp:Label>
</ItemTemplate>

<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# Format(DataBinder.Eval(Container, "DataItem.ExpirationDate"), "Short Date") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
 
BINGO.. there is your problem. The label is in a template column.

You need to do something like this:
Code:
dim lbl as new label
lbl = CType(e.Item.FindControl("YourLabel"),label)
strExpiryDate = lbl.text

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top