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

Discussion in 'ASP .Net Datagrid Control' started by botham782, Jul 28, 2005.

  1. botham782

    botham782 Guest

    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
    botham782, Jul 28, 2005
    #1
    1. Advertising

  2. botham782

    Brock Allen Guest

    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
    >
    Brock Allen, Jul 28, 2005
    #2
    1. Advertising

  3. botham782

    Teemu Keiski Guest

    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.

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

    "botham782" <> wrote in message
    news:...
    > 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
    >
    Teemu Keiski, Jul 28, 2005
    #3
  4. botham782

    botham782 Guest

    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)
    botham782, Jul 28, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hajime Kusakabe
    Replies:
    1
    Views:
    579
    Mike Moore [MSFT]
    Oct 23, 2003
  2. Replies:
    0
    Views:
    522
  3. Johannes Zellner
    Replies:
    1
    Views:
    498
    Alex Martelli
    Jan 17, 2006
  4. Joel Finkel

    Cells[].Text or Cells[].Controls[0]

    Joel Finkel, Sep 1, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    306
    Joel Finkel
    Sep 1, 2003
  5. SoftLion
    Replies:
    2
    Views:
    126
    George
    May 6, 2004
Loading...

Share This Page