在ASP.NET / VB.NET中将GridView单元格值传递给子例程

时间:2011-02-15 18:02:51

标签: asp.net vb.net gridview

我有一个带有Cell 0的GridView,其中包含我需要传递给Public Sub的ID。

我无法弄清楚如何从Cell 0中选择值以将其传递给Sub。我尝试过试验(参见下面的Dimmed EventID),但都失败了。这是我的代码:

Protected Sub gvAppointmentsCalls_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvAppointmentsCalls.RowCommand

        Dim EventID As String = gvAppointmentsCalls.Rows(e.RowIndex).Cells(0).Text

        If e.CommandName = "gvAdd2Outlook" Then
            Send_iCal_Call(EventID)
        End If
    End Sub

如果我直接输入值,例如Send_iCal_Call(123)然后它完美无缺。

Public Sub Send_iCal_Call(ByRef Event_ID As Integer)
' My code in here
End Sub

2 个答案:

答案 0 :(得分:1)

使用CommandArgument将ID传递给RowCommand-Handler。

例如:

CommandName="gvAdd2Outlook" CommandArgument='<%# Bind("EventID")%>'

答案 1 :(得分:1)

在我看来,你应该在GridView的DataKeyNames属性中获取你的ID值。您应该在网格标记中定义此属性,如此处

<asp:GridView DataKeyNames="EvenID">

然后将在后面的代码中访问它:

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand

   If e.CommandName = "gvAdd2Outlook" Then
        Dim EventIDString As String = GridView1.DataKeys.Item(e.RowIndex).Value.ToString()
        Dim EventID As Integer
        If Integer.TryParse(EventIDString, EventID) = False Then Throw New ArgumentException("Wrong EventID=" & EventID)

        Send_iCal_Call(EventID)
   End If
End Sub