|
-
Feb 16th, 2004, 08:31 AM
#1
Thread Starter
Frenzied Member
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
-
Feb 16th, 2004, 11:19 AM
#2
PowerPoster
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
-
Feb 17th, 2004, 01:15 AM
#3
Thread Starter
Frenzied Member
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:
Dim lb As New LinkButton
lb = CType(e.Item.Cells(0).Controls(0), LinkButton)
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
-
May 27th, 2004, 04:23 AM
#4
Fanatic Member
I was having this same trouble.
You solved the problem the same way I did apart from the line
VB Code:
lb = CType(e.Item.Cells(0).Controls(0), LinkButton)
I had to change it to
VB Code:
lb = CType(dgrResults.SelectedItem.Cells(0).Controls(0), LinkButton)
because I got the error
Item is not a member of system.eventargs
-
May 27th, 2004, 06:00 AM
#5
Thread Starter
Frenzied Member
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
-
May 27th, 2004, 09:04 AM
#6
Fanatic Member
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:
Private Sub dgrResults_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgrResults.SelectedIndexChanged
I guess there are different eventargs.
-
May 27th, 2004, 09:31 AM
#7
Frenzied Member
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.
-
May 27th, 2004, 09:37 AM
#8
Fanatic Member
How exactly do I do that then.
-
May 27th, 2004, 09:50 AM
#9
Frenzied Member
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.
-
May 27th, 2004, 09:53 AM
#10
PowerPoster
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.
-
May 27th, 2004, 10:13 AM
#11
Fanatic Member
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
-
May 27th, 2004, 10:20 AM
#12
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|