How to Select Datagrid Row

G

Guest

I currently have a datagrid with several columns. The first column in the
DataGrid is a HyperLinkColumn. When I select a row, I would like for this
HyperlinkColumn to fire. I have the mouseover colors changing, and I have the
routine, but I can't seem to get the HyperLinkColumn to fire....

Private Sub MyDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
MyDataGrid.ItemDataBound
'turn off highlighting...
If bHighlightDataGrid = False Then
Exit Sub
End If

If ((e.Item.ItemType = ListItemType.Pager) Or (e.Item.ItemType =
ListItemType.Header) Or (e.Item.ItemType = ListItemType.Footer)) Then
Exit Sub
End If

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
e.Item.Attributes.Add("onmouseover",
"this.style.backgroundColor='" & HighlightColor & "'")
If e.Item.ItemType = ListItemType.AlternatingItem Then
e.Item.Attributes.Add("onmouseout",
"this.style.backgroundColor='" & AlternatingColor & "'")
Else
e.Item.Attributes.Add("onmouseout",
"this.style.backgroundColor='" & DefaultColor & "'")
End If
e.Item.Attributes("onclick") =
Page.GetPostBackClientHyperlink(e.Item.Cells(0).Controls(0), "")

'THIS IS THE SECTION THAT I'M ATTEMPTING TO GET THIS TO WORK.
'IF I CHANGE THE HYPERLINK TO HYPERLINKCOLUMN I GET A SYNTAX
'ERROR.
Dim MYLINK As HyperLink = CType(e.Item.Cells(0).Controls(0), HyperLink)
e.Item.Attributes("onclick") =
Page.GetPostBackClientHyperlink(MYLINK, "")
End If

End Sub
 
G

Guest

Thanks for your help.

Maybe I'm not explaining this correct, let's say that this is my code

If e.Item.ItemType = ListItemType.AlternatingItem Then
e.Item.Attributes.Add("onmouseout",
"this.style.backgroundColor='" & AlternatingColor & "'")
Else
e.Item.Attributes.Add("onmouseout",
"this.style.backgroundColor='" & DefaultColor & "'")
End If

Dim MYLINK As HyperLink = CType(e.Item.Cells(0).Controls(0),
HyperLink)
e.Item.Attributes("onclick") =
Page.GetPostBackEventReference(MYLINK, "")

When I select any row in the grid (excluding the first column, which has the
link in it), I would like it to fire the link in the first column. My
current code, seems like it's firing the ColumHeader "Edit" (which is the
header for the first column) and re-sorting stuff. i.e. if I select an item
in the datagrid which is in the 6th row, 3rd column, it is not firing the
Hyperlink located in the 6th row 1st column. This hyperlink should actually
take me to another page....
 
G

Guest

If you are only interested in jumping to the URL of the Hyperlink then why
are you trying to generate a server postback?

You can instead do this:

e.Item.Attributes("onclick") = _
"javascript:document.URL='" & _
Ctype(e.Item.Cells(0).Controls(0), HyperLink).NavigateUrL & "';"
e.Item.Attributes("title") = "Click anywhere on the row to jump to the link
associated with this row"

I did that and put a new example (with the source code) to demonstrate it
at: http://www.societopia.net/Samples/DataGrid_DataList2.aspx
 
G

Guest

That's exactly what I want, I knew it was in the NavigateURL, but wasn't sure
how to access it......Thanks alot!!!
 

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,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top