在GridView中选择单元格时获取列索引

时间:2011-12-14 23:05:15

标签: asp.net gridview onclick eventvalidation

以下是我用来选择每个单元格的代码。但是,当我选择该单元格时,我需要该单元格中的数据,即rowIndex和columnIndex。不幸的是,在完成所有研究并尝试其他代码之后..我仍然坚持如何在gridView中获取所选单元格的columnIndex。

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    For i As Integer = 0 To grdResults.Rows.Count - 1
        ClientScript.RegisterForEventValidation(grdResults.UniqueID, "Select$" & i)
    Next

    MyBase.Render(writer)
End Sub

Protected Sub grdResults_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdResults.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then

        For i As Integer = 1 To e.Row.Cells.Count - 1

            e.Row.Cells(i).Attributes("OnMouseOver") = "ToggleHighlight(this, true);"
            e.Row.Cells(i).Attributes("OnMouseOut") = "ToggleHighlight(this, false);"
            e.Row.Cells(i).Style("cursor") = "hand"
            e.Row.Cells(i).Attributes("onclick") = ClientScript.GetPostBackEventReference(grdResults, "Select$" & e.Row.RowIndex)
            e.Row.Cells(i).Attributes("style") += "cursor:pointer;curosor:hand;"
        Next
    End If
End Sub

Protected Sub grdResults_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdResults.SelectedIndexChanged
    LoadDetails(columnIndex, grdResults.selectedRow)
End Sub

这也是我尝试过的

*我尝试使用commandName“ColumnClick”执行buttonField。然后使用grdResults_RowCommand尝试获取rowIndex和columnIndex,但是我得到了enableEventValidation错误页面。即使我有一个覆盖的渲染,看起来像:

'Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
'    For Each r As GridViewRow In grdResults.Rows
'        If r.RowType = DataControlRowType.DataRow Then
'            For columnIndex As Integer = 0 To r.Cells.Count - 1
'                ClientScript.RegisterForEventValidation(r.UniqueID + "$ct100", columnIndex.ToString())
'*                'ClientScript.RegisterForEventValidation(r.UniqueID, "Select$" & columnIndex)
'            Next
'        End If
'    Next

'    MyBase.Render(writer)
'End Sub

0 个答案:

没有答案