asp.net vb.net gridview,按模板字段按钮时获取行ID

时间:2013-11-19 16:51:15

标签: asp.net vb.net gridview templatefield

我有一个带按钮的模板字段。我想按下按钮并获取行ID,以便我可以返回用户选择。我有一个常规选择按钮,工作正常,但用户希望非员工的人隐藏选择按钮。我有这个工作,但因为它是一个模板字段,它会触发RoW_command过程,我似乎无法获得行索引,因为它是一个模板字段而不是常规的Select按钮。或者我似乎无法命名常规选择按钮,因为它的命令字段没有名称或ID属性?

就像我说的那样,隐藏了名为btnSelect的模板字段   Private Sub GridView3_RowDataBound(ByVal sender As Object,ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)处理GridView3.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then

        If (DataBinder.Eval(e.Row.DataItem, "LegacyPersonType") <> "Employee") Then
            e.Row.ForeColor = Drawing.Color.Black
            e.Row.BackColor = Drawing.Color.Yellow ' This will make row back color yellow 
            e.Row.FindControl("btnSelect").Visible = False

        Else
            e.Row.ForeColor = Drawing.Color.Black
            e.Row.BackColor = Drawing.Color.White   ' the normal employees make white
        End If

    End If

End Sub

当我按btnSelect
时,我需要找到Row索引 Private Sub GridView3_RowCommand(ByVal sender As Object,ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)处理GridView3.RowCommand

    Dim index As Integer = Convert.ToInt32(e.CommandArgument) ‘ Error invalid 

'换句话说,当按下模板字段时,没有e.CommandArgument '但点击常规选择

    Dim row As GridViewRow = GridView3.Rows(index)
    Session("EnterpriseID") = HttpUtility.HtmlDecode(GridView3.Rows(index).Cells(2).Text)
    Dim EmployeeType As String = HttpUtility.HtmlDecode(GridView3.Rows(index).Cells(7).Text)
    Dim CommonName As String = HttpUtility.HtmlDecode(GridView3.Rows(index).Cells(1).Text)

    Dim EnterpriseID = HttpUtility.HtmlDecode(GridView3.Rows(index).Cells(6).Text)

2 个答案:

答案 0 :(得分:0)

GridViewRow对象通过RowIndex事件中的RowCommand属性知道其行索引,如下所示:

Private Sub GridView3_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView3.RowCommand
    ' Get the row by finding the grandparent of the control (button) 
    ' that initiated the command
    Dim theGridViewRow As GridViewRow
    theGridViewRow = CType(CType(e.CommandSource, Button).Parent.Parent, GridViewRow)

    ' Get the index value from the grid view row
    Dim index As Integer = theGridViewRow.RowIndex
End Sub

答案 1 :(得分:0)

好的我自己想通了,我在网格上的模板字段中创建了默认的选择按钮。因为它是默认按钮,所以它有必要的代码来激活它。这是从头开始创建模板字段的问题。通过使它成为模板字段,它还给它起了一个名字。因此,在RowDataBound代码中,当一个人不是员工时,上面的FindControl方法会隐藏它。 - R2 Builder 1分钟前编辑