您好我正在使用gridview控件来显示数据。点击gridview特定行我想获得那行ID。
为此,我使用了这段代码:
Protected Sub grdReport_SelectedIndexChanged(sender As Object, e As EventArgs) Handles grdReport.SelectedIndexChanged
Dim row As GridViewRow = Me.grdReport.SelectedRow
Dim InvoiceId As Integer = Convert.ToInt32(grdReport.DataKeys(row.RowIndex).Value)
Session("InvoiceId") = InvoiceId
End Sub
Protected Sub grdReport_RowCreated(sender As Object, e As GridViewRowEventArgs) Handles grdReport.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(Me.grdReport, "Select$" + e.Row.RowIndex.ToString()))
End If
End Sub
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
Dim gvr As GridViewRow
For Each gvr In grdReport.Rows
gvr.Attributes("onclick") = ClientScript.GetPostBackClientHyperlink(
grdReport,
String.Concat("Select$", gvr.RowIndex),
True)
Next
MyBase.Render(writer)
End Sub
上面的代码对我来说工作正常,但每当我点击gridview行然后再次加载页面并且页面变慢。
请让我知道我能做什么,这样整个页面就不会一次又一次地加载行。
答案 0 :(得分:0)
首先,如果您想要对鼠标onClick事件做出反应,那么selectedindexchanged不是一个好事。
在填充数据
时,始终对网格中的每一行执行selectedindexchanged所以如果你有100行,代码会在你看到网格之前被执行100次
我建议使用cellmousedown或cellmouseclick
Private Sub DataGridView_CellMouseDown(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView.CellMouseDown
If e.Button = Windows.Forms.MouseButtons.Left And e.RowIndex <> -1 And e.ColumnIndex <> -1 Then
Dim InvoiceId As Integer = DataGridView.Rows(e.RowIndex).Cells("InvoiceId").value
End Sub
对于您的重新编码问题,我猜它与您的渲染子调用
有关imho对于你想要完成的简单任务是不必要的