Can you identify the cell clicked in an ASP.NET datagrid?

G

Guest

I've tried this approach but it's not quite what I want:

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles dg.ItemDataBound
Dim itemType As ListItemType = e.Item.ItemType
If ((itemType = ListItemType.Pager) Or _
(itemType = ListItemType.Header) Or _
(itemType = ListItemType.Footer)) Then
Return
Else
Dim button As LinkButton = _
CType(e.Item.Cells(0).Controls(0), LinkButton)
e.Item.Attributes("onclick") = _
Page.GetPostBackClientHyperlink(button, "")


End If

Private Sub dg_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dg.ItemCommand

Dim myCells As TableCellCollection
myCells = e.Item.Cells

For Each cel As TableCell In myCells

txtDisplay.Text += " " + cel.Text

Next

I want the value of the cell that the user clicks...
Any insight greatly appreciated...
Thanks,
johnny
 
A

Anthony Merante

Hey Johnny,

Well I think i understand what you want. All you really want to do is
display the text in a Cell in the Datagrid.

I would do this: Use the Itemcreated event rather than the ItemDataBound (
per your example,it doesnt look like you need access to the DataItem) Its
not a bid deal though.

I would have some like
Dim button As LinkButton = _
CType(e.Item.Cells(0).Controls(0), LinkButton)
e.Item.Attributes("onclick") = "MyClientSideCode(this);";

and on the page have a clientside js function that just displays the cell's
innerHTML

I made some design changes and I apologize if thats totally not what you
want. The reason is that I dont see a reason to have to postback to get at
the ItemCommand just to display the text of a cell when this can be done
clientside. Now you might need that value for processing but again i didtn
see that per your example.



Incidentally to add, i would also add that e.Item.FindControl("") is a
better way(not performance wise but in maintence wise) to get at controls.
If that control's ordinal ever changes you'll posibly get a YSD(yellow
screen of Death) or an action on the wrong column.

If you need help on the JS function let me know.

HTH,
-T
 
G

Guest

Thanks for your response! I'd be interested in seeing an example of the
javascript function if you had time.

jg
 

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

No members online now.

Forum statistics

Threads
474,039
Messages
2,570,376
Members
47,029
Latest member
EmiliaSton

Latest Threads

Top