Pass GridView Cell Value to Sub Routine in ASP.NET / VB.NET
I have a GridView with Cell 0 containing the ID that I need to pass to a Public Sub.
I cannot figure out how to pick the value from Cell 0 in order to pass it to the Sub. I have tried experimenting (see the Dimmed EventID below) but have failed. Here is my code:
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
If I type the value directly e.g. Send_iCal_Call(123)
then it works perfectly.
Public Sub Send_iCal开发者_运维技巧_Call(ByRef Event_ID As Integer)
' My code in here
End Sub
Use the CommandArgument to pass the ID to the RowCommand-Handler.
For example:
CommandName="gvAdd2Outlook" CommandArgument='<%# Bind("EventID")%>'
In my opinion you should obtain your ID value within GridView's DataKeyNames property. You should define it in your grid markup this property like here
<asp:GridView DataKeyNames="EvenID">
and then will access it in code behind:
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
精彩评论