salazie
asked on
Selecting row in datagrid by clicking anywhere
Hopefully a quick and easy one for someone this!!
I want to be able to select a row from a datagrid (and trigger my dg1_ItemCommand -> Select event) when the user clicks anywhere in the row. My code is as follows:
Private Sub dg1_ItemCreated(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls. DataGridIt emEventArg s) Handles dg1.ItemCreated
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt em Or e.Item.ItemType = ListItemType.SelectedItem Then
e.Item.Attributes.Add("onc lick", "javascript:__doPostBack(' " & "dg1:" & "ctrl" & e.Item.ItemIndex & ":ctrl0','')")
End If
End Sub
But it doesn't work! I'm hoping it's something like a typo but I've tried a number of changes and nothing seems to work... It appears to post back but doesn't trigger the code it's supposed to. Incidentally, when someone selects the Select link the following is triggered and works correctly:
Private Sub dg1_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.ui.WebControls. DataGridCo mmandEvent Args) Handles dg1.ItemCommand
Select Case e.CommandName
Case Is = "Select"
Dim sRowId As String = dg1.DataKeys(e.Item.ItemIn dex).ToStr ing()
Response.Redirect("mypage. aspx?id=" & sRowId, True)
End Select
End Sub
Thanks in advance everyone!
I want to be able to select a row from a datagrid (and trigger my dg1_ItemCommand -> Select event) when the user clicks anywhere in the row. My code is as follows:
Private Sub dg1_ItemCreated(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt
e.Item.Attributes.Add("onc
End If
End Sub
But it doesn't work! I'm hoping it's something like a typo but I've tried a number of changes and nothing seems to work... It appears to post back but doesn't trigger the code it's supposed to. Incidentally, when someone selects the Select link the following is triggered and works correctly:
Private Sub dg1_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.ui.WebControls.
Select Case e.CommandName
Case Is = "Select"
Dim sRowId As String = dg1.DataKeys(e.Item.ItemIn
Response.Redirect("mypage.
End Select
End Sub
Thanks in advance everyone!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
salazie,
since you have a button to allow user select, then you can solve all the problem at datagrid's onItemCommand event.
Private Sub dg1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls. DataGridCo mmandEvent Args) Handles dg1.ItemCommand
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt em Then
'clear previous color
Dim dgItem As DataGridItem
For Each dgItem In dg1.Items
Dim tblClearRow As TableRow
tblClearRow = CType(dgItem, TableRow)
If dgItem.ItemType = ListItemType.Item Then
tblClearRow.BackColor = Color.FromName("#ccffcc") 'assign item's color
ElseIf dgItem.ItemType = ListItemType.AlternatingIt em Then
tblClearRow.BackColor = Color.FromName("#ffffcc") 'assign alternative item's color
End If
Next
'highlight selected row
Dim tblRow As TableRow
tblRow = CType(e.Item, TableRow)
tblRow.BackColor = Color.FromName("#cecece")
'the rest if you code
Select Case e.CommandName
Case Is = "Select"
.........
End Select
End If
End Sub
-But, your selected row is located at bottom of the page and when postback occured you may see the page will reload and stay at top of the page. To solve that problem, simply add SmartNavigation="True" in your page directive. After that, your selected row will be retain at the current position as well.
eg:
<%@ Page Language="vb" SmartNavigation="true" AutoEventWireup="false" .....%>
Regards
x_com
since you have a button to allow user select, then you can solve all the problem at datagrid's onItemCommand event.
Private Sub dg1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt
'clear previous color
Dim dgItem As DataGridItem
For Each dgItem In dg1.Items
Dim tblClearRow As TableRow
tblClearRow = CType(dgItem, TableRow)
If dgItem.ItemType = ListItemType.Item Then
tblClearRow.BackColor = Color.FromName("#ccffcc") 'assign item's color
ElseIf dgItem.ItemType = ListItemType.AlternatingIt
tblClearRow.BackColor = Color.FromName("#ffffcc") 'assign alternative item's color
End If
Next
'highlight selected row
Dim tblRow As TableRow
tblRow = CType(e.Item, TableRow)
tblRow.BackColor = Color.FromName("#cecece")
'the rest if you code
Select Case e.CommandName
Case Is = "Select"
.........
End Select
End If
End Sub
-But, your selected row is located at bottom of the page and when postback occured you may see the page will reload and stay at top of the page. To solve that problem, simply add SmartNavigation="True" in your page directive. After that, your selected row will be retain at the current position as well.
eg:
<%@ Page Language="vb" SmartNavigation="true" AutoEventWireup="false" .....%>
Regards
x_com
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok I got it, tusharashah - your suggestion was close but I needed to refer to refer to my web user control as well, this is probably where my problem came in - I imagine yours is directly on a web page. x_com - your solution didn't include any javascript so I'm not sure how this would work but please correct me if I'm wrong...
So the solution was in hismightiness's answer. I've added what I had to do here in case it's of any use to anyone:
Private Sub dg1_ItemCreated(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles dg1.ItemCreated
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt em Or e.Item.ItemType = ListItemType.SelectedItem Then
e.Item.Attributes.Add("onc lick", "javascript:__doPostBack(' Control1$d g1$_ctl" & (e.Item.ItemIndex + 2).ToString & "$_ctl0','')")
End If
End Sub
Thanks everyone!
So the solution was in hismightiness's answer. I've added what I had to do here in case it's of any use to anyone:
Private Sub dg1_ItemCreated(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles dg1.ItemCreated
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt
e.Item.Attributes.Add("onc
End If
End Sub
Thanks everyone!
salazie,
Sorry for the delay.. My provided solution is depend on server event. If you prefer client side, it's ok. However, you can enhance your current code if you prefer :
instead of
e.Item.Attributes.Add("onc lick", "javascript:__doPostBack(' Control1$d g1$_ctl" & (e.Item.ItemIndex + 2).ToString & "$_ctl0','')")
Change to
e.Item.Attributes.Add("onc lick", "javascript:__doPostBack(' " & e.item.clientid & "','')")
Sorry for the delay.. My provided solution is depend on server event. If you prefer client side, it's ok. However, you can enhance your current code if you prefer :
instead of
e.Item.Attributes.Add("onc
Change to
e.Item.Attributes.Add("onc
I had to add an Invisible 'SELECT button' in my DataGrid