Results 1 to 12 of 12

Thread: How to get the value text from linkbutton in datagrid?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    How to get the value text from linkbutton in datagrid?

    I can't get this to work properly (onItemCommand):

    [Highlight=VB]
    Public Sub SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
    Response.Write("Text" & e.Item.Cells.Item(0).Text)
    End Sub


    The text should be the text field property of the linkbutton... which is linked to a database value..


    How can I get it??? It doesn't work the way I want

    kind regards
    Henrik

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    There is text and there are controls. I think you are looking at a control (htmlcontrol, or the asp.net link, doesn't matter)

    I haven't tried this, but poke around and you may have luck:
    e.Item.Cells.Item(0).Controls(0).Text

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Sorry, but your code didn't work.. I thought some more about it and finally I came up with an idea that worked


    VB Code:
    1. Dim lb As New LinkButton
    2. lb = CType(e.Item.Cells(0).Controls(0), LinkButton)
    3. Response.Write("Text" & lb.Text)

    Im not sure what the "extreme programmers" have to say about this solution... but it works. I don't know if it is a good solution or an optimal one...



    kind regards
    Henrik

  4. #4
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    I was having this same trouble.

    You solved the problem the same way I did apart from the line
    VB Code:
    1. lb = CType(e.Item.Cells(0).Controls(0), LinkButton)
    I had to change it to
    VB Code:
    1. lb = CType(dgrResults.SelectedItem.Cells(0).Controls(0), LinkButton)
    because I got the error
    Item is not a member of system.eventargs


    Things I do when I am bored: DotNetable

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Hm... when you invoke the edit or delete events on the datagrid, you get a reference to the selected row as an eventarg.. it's strange that you have to get that data from the grid... it's also bad from an architectural point of view if you mix view specific code with model specific code...


    kind regards
    Henrik

  6. #6
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    That may explain it then.

    I am not invoking edit or delete, I am using a linkbutton to know when the selectedindex has changed.

    VB Code:
    1. Private Sub dgrResults_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgrResults.SelectedIndexChanged

    I guess there are different eventargs.


    Things I do when I am bored: DotNetable

  7. #7
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    What do you mean extreme?

    MrNorth

    I get the key for my table out of my datagrid on SelectedIndex change so I can work with my data. I had the linkbutton problem as well. I just made my button a button that wasn't bound though and then I could read the text in the cell ,because it wasn't a button any longer.


    davidrobin
    I think your solution is sound unless you always use the grid data since it could change. Store the key or what ever in a private property that uses ViewState(key) to be safe, and you know not have to type that entire grid.Rows(....(...)).Text all the time. Plus, you know if you move your cells around you'll only have to change the cell index in the ItemSelected event or whatever it's called exactly.
    Magiaus

    If I helped give me some points.

  8. #8
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    How exactly do I do that then.


    Things I do when I am bored: DotNetable

  9. #9
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    Code:
    private property string _selectedKey
    {
        get{return (string)ViewState["-selectedKey"];}
        set{ViewState["-selectedKey"] = value;}
    }
    I quit vb sorry. But it would Be ViewState("-selectedKey") = value and however you change types with vb. Then you know when your Grid fires the selected index changed event
    Code:
    			_selectedKey = dg_products.Items[dg_products.SelectedIndex].Cells[2].Text;
    Thats what you would do now minus the getting the linkbutton. But if you add a button and set it Command to Select you can use the ItemCommand Event wich lets you have multi buttons that do multi things. You just do a Select Case/switch on e.CommandName and you have e.Item.Cells[x]
    Magiaus

    If I helped give me some points.

  10. #10
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    I didn't read the entire thread, but you should really be putting the value in the CommandArgument property and pull it out of the linkbutton this way.

  11. #11
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    Originally posted by Lethal
    I didn't read the entire thread, but you should really be putting the value in the CommandArgument property and pull it out of the linkbutton this way.
    I can't seem to get the linkbutton command bit to work on my databound datagrid


    Things I do when I am bored: DotNetable

  12. #12
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    There are two ways to pull the data out. You can either let the event bubble up through the datagrid's event chain, or you can set the ItemCommand for the linkbutton in your aspx to the event handler. If you are handling the event through the datagrid, you will need to do a FindControl() and pull it out this way, or if you are using the second way, the 'e' argument has a CommandArgument property you can access.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width