By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,481 Members | 3,170 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,481 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Mar 17 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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


"johnnyG" <jo*****@discussions.microsoft.com> wrote in message
news:CD**********************************@microsof t.com...
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

Mar 17 '06 #2

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

jg

"Anthony Merante" wrote:
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


"johnnyG" <jo*****@discussions.microsoft.com> wrote in message
news:CD**********************************@microsof t.com...
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


Mar 17 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.