Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Can't get text of e.Item.Cells(x)

Reply
Thread Tools

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

 
 
botham782
Guest
Posts: n/a
 
      07-28-2005
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

 
Reply With Quote
 
 
 
 
Brock Allen
Guest
Posts: n/a
 
      07-28-2005
Check the e.Items.Cells(x).Controls collection.

-Brock
DevelopMentor
http://staff.develop.com/ballen



> 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
>




 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      07-28-2005
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/ar...1/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.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

"botham782" <> wrote in message
news: oups.com...
> 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
>



 
Reply With Quote
 
botham782
Guest
Posts: n/a
 
      07-28-2005
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).Fin dControl("hlStockNumber"),
HyperLink).NavigateUrl
dgSearchResults.Items(RowCount).Attributes.Add("on click",
"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)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get ubuntu ! Get ubuntu ! Get ubuntu ! Get ubuntu ! Getubuntu Windows 64bit 1 06-01-2009 08:54 AM
Re: Tkinter.Text widget - how to get text cursor position? Alex9968 Python 0 03-19-2008 07:07 PM
Tkinter.Text widget - how to get text cursor position? Alex9968 Python 0 03-19-2008 05:37 PM
Controlling text in a Text Area or Text leo ASP General 1 12-05-2005 01:13 AM
How to get the text in html tag.like<div...><font...>Text</font></ =?Utf-8?B?Tmlja3k=?= ASP .Net 2 02-20-2005 03:03 PM



Advertisments