我正在尋找一種方法來選擇DataGrid中的整行,但我只看到GridView
代碼。下面是示例代碼:如何使整行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
但唯一的改變(就我觀察)是它只是回發。
爲什麼你不使用'GridView'第一個地方? DataGrid是非常古老和多餘的。與GridView相比,沒有什麼優勢。 –
@TimSchmelter,因爲我有很多代碼已經在這個DataGrid中。所以我需要使用這一路。如果我將Grid改爲GridView,我將有很多修訂:( – Dale
然後在這裏查看這個方法:https://www.safaribooksonline.com/library/view/aspnet-cookbook/0596003781/ch01s17.html –