Datagrid - need to detemine index of datarow on hyperlink click

D

debiken

I have an aspx page that loads a datagrid from a fairly
complex query. It grabs lots of order information but I
only display part of it on this "order summary" page. The
order number is a hyperlinkcolumn which opens a new
window, and that page displays much more order detail (but
from the same datatable as the original datagrid is bound
to...trying to save another trip to the DB). I am caching
the datatable, and want to populate the detail window with
that data, but don't know the table row. I can't get an
event to fire, onItemCommand never takes off on a
hyperlink, even if I use a TemplateColumn with an anchor
tag in the ItemTemplate and bind the data using the
container.DataItem method. Anyone know of a way to get
this table row on a hyperlink click? (I have tried using
the Container.DataItem as well, and can't get the row
index out of it...). Any help or ideas are greatly
appreciated!
 
M

Mike Moore [MSFT]

Hi,

You wrote that you need the row index. Usually, most folks just need the
key field. In any case, the grid's ItemDataBound event should do what you
want. Here's a sample snippet. In this sample I add a new link control. You
could instead have a link in the template and use FindControl to get a
reference to it. Then you could change its NavigateURL. In this sample, I
create a NavigateUrl using data from the database and the data grid's item
number for the row.

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim h As New HyperLink
h.NavigateUrl = "http:/" & e.Item.DataItem("au_lname") &
".aspx?a=" & e.Item.ItemIndex
h.Text = e.Item.DataItem("au_lname")
e.Item.Cells(0).Controls.Add(h)
End If
End Sub


Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.

This posting is provided "AS IS", with no warranties, and confers no rights.


--------------------
 

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

Forum statistics

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

Latest Threads

Top