Can't get text of e.Item.Cells(x)

B

botham782

Hi,

I cannot grab the text of a datagrid row's cell. I am trying to access
the value in the datagrid's ItemCreated event. I use a conditional
statement in the ItemCreated event to ensure that I'm "looking at" an
Item or AlternatingItem. I have checked out numerous posts related to
this, but for some reason just cannot get anything useful other than an
empty string. Sorry in advance for asking a fairly popular question.

I want to get the text of the row's cell(0), which is the first column
that I defined in my datagrid. I will ultimately use this later in the
code of this same event when I add an attribute to the entire row. The
attribute will be a javascript onclick event which allows the entire
row to be clicked, taking the user to a "detail" page relating to the
information in that datagrid row. The value of that cell(0) will be
concatenated to the querystring referenced by the onclick event.

At first I had the column defined as a TemplateColumn. I had a
HyperLink control inside the column which I used temporarily as a way
for the user to get to the detail page. I tried to reference that cell
by using:

Dim CellControl As HyperLink =
CType(e.Item.Cells(0).FindControl("hlStockNumber"), HyperLink)
Dim TestStockNumber As String = CellControl.NavigateUrl
'--> debugger says TestStockNumber's value is: ""

Then I tried to change the column to a BoundColumn. I tried to
reference that column by using:

Dim TestStockNumber As String = e.Item.Cells(0).Text
'--> debugger says TestStockNumber's value is: ""

Next I changed the column back to a TemplateColumn. I changed the
control that the data was stored in in the cell to a label and tried to
reference it like this:

Dim TestStockNumber As String =
CType(e.Item.Cells(0).FindControl("lblStockNumber"), Label).Text
'--> debugger says TestStockNumber's value is: ""

I finally tried a method similar to the above, but taking the data out
of a control entirely:

Dim TestStockNumber As String = CType(e.Item.Cells(0).Controls(0),
DataBoundLiteralControl).Text
'--> debugger says TestStockNumber's value is: ""

What in the world am I doing wrong? Any help would be greatly
appreciated.

Thanks
 
T

Teemu Keiski

Hi,

In ItemCreated data has not been restored from ViewState yet, there is
nothing in the cell when that event runs. Related to your problems, seems as
if those control's Text hasn't been set in the databinding expression or
ItemDataBound correctly? Unless you have tried all that too in ItemCreated,
which would explain.

Anyways, sure way to get to it, is to have code in DataGrid's PreRender
event and then loop through all items in Items collection, search for the
respective data from each item (or controls if that's the need) and set
those properties attributes whatever you are trying to set.

FYI: I've demonstrated creating a row-clickable datagrid
http://blogs.aspadvice.com/joteke/archive/2005/01/30/2493.aspx

though it does a postback based on the row which was clicked and you can do
anything in the postback event (you probably need direct linking), however
if postback is not an issue then why not.
 
B

botham782

Thanks Brock and Teemu for your suggestions. I took Teemu's advice
which was to do this in the datagrid's PreRender event. Once I had it
in there it took me (with some help from a friend) a bit to realize I
need to be looping through the items in the datagrid, not the e
parameter - duh. Although I haven't cleaned it up and prepared it for
all rows yet, here is what I put in the PreRender event to accomplish
what I needed (first five rows only):

Dim RowCount As Integer
Dim TestStockNumber As String
For RowCount = 0 To 5
TestStockNumber =
CType(dgSearchResults.Items(RowCount).Cells(0).FindControl("hlStockNumber"),
HyperLink).NavigateUrl
dgSearchResults.Items(RowCount).Attributes.Add("onclick",
"javascript:window.location.href='" & TestStockNumber & "'")
Next

The above code grabs the stock number's text (found in a HyperLink
control in the first column - cell(0)) and builds a javascript onclick
event for the entire datagrid row.

Thank you very much for your help. I also liked your method of
implementing a clickable row - I will try it next time.

botham782 (Mike)
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top