asp.net vb.net gridview,按下模板字段按钮时获取行ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20077307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
asp.net vb.net gridview, get row id when pressing template field button
提问by R2 Builder
I have a template field with a button. I want to press the button and get the row Id so I can return the users selection. I have a regular select button which works fine but the user want the people who are not employee's to hide the select button. I have this working but since it's a template field it fires the RoW_command procedure and I cannot seem to get the row index since it is a template field and not the regular Select button. Or I cannot seem to name the Regular select button since it Command field does not have a Name or ID property?
我有一个带按钮的模板字段。我想按下按钮并获取行 ID,以便我可以返回用户选择。我有一个正常的选择按钮,它工作正常,但用户希望非员工的人隐藏选择按钮。我有这个工作,但因为它是一个模板字段,它会触发 RoW_command 过程,我似乎无法获得行索引,因为它是一个模板字段而不是常规的 Select 按钮。或者我似乎无法命名常规选择按钮,因为它的命令字段没有名称或 ID 属性?
Like I said this works hiding the template field called btnSelect Private Sub GridView3_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView3.RowDataBound
就像我说的,这可以隐藏名为 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
I need to find the Row index when I press btnSelect
Private Sub GridView3_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView3.RowCommand
当我按下 btnSelect
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
‘ in other words when pressing the tem-plate field there is no e.CommandArgument ‘But clicking the regular select there is
' 换句话说,当按下模板字段时没有 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)
采纳答案by R2 Builder
Ok I figured it out myself, I made the default Select button on the grid a Template field. since it was the default button it had the necessary code to make it fire. This was the problem with making a template field from scratch. And by making it a template field it then also gave it a name. So in the RowDataBound code the FindControl method above hides it when a person is not an employee. – R2 Builder 1 min ago edit
好的,我自己弄明白了,我将网格上的默认选择按钮设为模板字段。因为它是默认按钮,所以它有必要的代码来触发它。这是从头开始制作模板字段的问题。通过将其设为模板字段,它还为其命名。所以在 RowDataBound 代码中,当一个人不是雇员时,上面的 FindControl 方法会隐藏它。– R2 Builder 1 分钟前编辑
回答by Karl Anderson
The GridViewRowobject knows its row index via the RowIndexproperty in the RowCommandevent, like this:
该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

