2015-09-08 37 views
2

我正在尋找一種方法來選擇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 

但唯一的改變(就我觀察)是它只是回發。

+1

爲什麼你不使用'GridView'第一個地方? DataGrid是非常古老和多餘的。與GridView相比,沒有什麼優勢。 –

+0

@TimSchmelter,因爲我有很多代碼已經在這個DataGrid中。所以我需要使用這一路。如果我將Grid改爲GridView,我將有很多修訂:( – Dale

+0

然後在這裏查看這個方法:https://www.safaribooksonline.com/library/view/aspnet-cookbook/0596003781/ch01s17.html –

回答

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" /> 
+0

您只提供的代碼如果我按下「選擇」按鈕,但是我必須做的是全部行應該是可點擊的,而不是僅僅是選擇按鈕,因爲我也會隱藏那個。 – Dale

+0

好吧,沒有意識到你需要整個行可點擊。我將編輯答案 – Pasilda

+0

好,謝謝,我會等你的答案@Pasilda – Dale