【发布时间】:2015-12-03 18:38:40
【问题描述】:
我正在寻找一种如何在 DataGrid 中选择整行的方法,但我只看到了一个 GridViewcode。下面是示例代码:
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
但它似乎没有触发,(我认为onclickpart 存在问题)但它正在突出显示。我想做的是在javascript中的onclick上放置一个CommandName = Select,但是如何?
编辑:这在我的新代码中。
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 中已经有很多代码。所以我需要一直使用它。如果我将网格更改为 GridView,我会有很多修改:(
-
您建议的链接使用按钮的静态值。但我的 datagrid 的值各不相同。
-
什么是静态值?如果我没看错,他只是假设第一列中的链接按钮。他使用
AutoGenerateColumns=false和BoundFields,但数据源是动态的。
标签: asp.net vb.net gridview datagrid