我正在寻找如何在 DataGrid 中选择整行的方法,但我只看到了GridView
代码。以下是示例代码:
Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes("onmouseover") = "this.style.cursor='pointer';this.style.textDecoration='underline';"
e.Row.Attributes("onmouseout") = "this.style.textDecoration='none';"
e.Row.ToolTip = "Click to select row"
e.Row.Attributes("onclick") = Me.Page.ClientScript.GetPostBackClientHyperlink(Me.GridView1, "Select$" & e.Row.RowIndex)
End If
End Sub
所以为了让它在我的DataGrid上工作,我对我的代码进行了一些更改并使它像这样:
Private Sub DataGrid_ItemCreated(sender As Object, e As DataGridItemEventArgs) Handles DataGrid.ItemCreated
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
e.Item.Attributes("onmouseover") = "this.style.cursor='pointer';this.style.textDecoration='underline';"
e.Item.Attributes("onmouseout") = "this.style.textDecoration='none';"
e.Item.ToolTip = "Click to select row"
e.Item.Attributes("onclick") = Me.Page.ClientScript.GetPostBackClientHyperlink(Me.DataGrid, "Select$" & e.Item.ItemIndex)
End If
End Sub
但它似乎没有解雇,(我认为onclick
部分存在问题),但它突出显示。我想要做的是在CommandName = Select
的javascript中放置onclick
,但是如何?
编辑:这是我现在的新代码。
Private Sub DataGrid_ItemCreated(sender As Object, e As DataGridItemEventArgs) Handles DataGrid.ItemCreated
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
e.Item.Attributes("onmouseover") = "this.style.cursor='pointer';this.style.textDecoration='underline';"
e.Item.Attributes("onmouseout") = "this.style.textDecoration='none';"
e.Item.ToolTip = "Click to select row"
Dim button As LinkButton
button = DirectCast(e.Item.Cells(0).Controls(0), LinkButton)
Dim js As String = Page.ClientScript.GetPostBackClientHyperlink(button, "")
e.Item.Attributes("onclick") = js
End If
End Sub
但唯一改变的是(我所观察到的)它只是回发。
答案 0 :(得分:0)
确保您处理DataGrid.ItemCommand
事件:
Private Sub DataGrid_ItemCommand(source As Object, e As DataGridCommandEventArgs) Handles DataGrid.ItemCommand
Me.DataGrid.SelectedIndex = e.Item.ItemIndex
End Sub
使用DataGrid.ItemDataBound
代替DataGrid.ItemCreated
:
Private Sub DataGrid_ItemDataBound(sender As Object, e As DataGridItemEventArgs) Handles DataGrid.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
e.Item.Attributes("onmouseover") = "this.style.cursor='pointer';this.style.textDecoration='underline';"
e.Item.Attributes("onmouseout") = "this.style.textDecoration='none';"
e.Item.ToolTip = "Click to select row"
'e.Item.Attributes("onclick") = Me.Page.ClientScript.GetPostBackClientHyperlink(Me.DataGrid, "Select$" & e.Item.ItemIndex)
Dim button As LinkButton
button = DirectCast(e.Item.Cells(0).Controls(0), LinkButton)
'Dim js As String = Page.ClientScript.GetPostBackClientHyperlink(Me.DataGrid, "Select$" & e.Item.ItemIndex)
Dim js As String = Page.ClientScript.GetPostBackClientHyperlink(button, "")
e.Item.Attributes("onclick") = js
End If
End Sub
然后我将以下内容添加到DataGrid的标记中,只是为了突出显示选择:
<SelectedItemStyle BackColor="Red" />