这个ASP.NET代码出了什么问题......? (Datagrid + Imagebutton)

时间:2011-01-02 11:19:17

标签: asp.net vb.net

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
    If e.CommandName = "Hold" Then
        Dim gvRow As GridViewRow = CType(CType(sender, Control).Parent.Parent, GridViewRow)
        Dim index As Integer = gvRow.RowIndex
        Dim myRow As GridViewRow = GridView1.Rows(index)
        'Find the checkbox
        Dim lab5 As Label = DirectCast(myRow.FindControl("Label5"), Label)
        Dim label2 As Label = DirectCast(myRow.FindControl("Label2"), Label)
        Dim label4 As Label = DirectCast(myRow.FindControl("Label4"), Label)
        Dim label22 As Label = DirectCast(myRow.FindControl("Label22"), Label)
        Me.Response.Redirect("Select_seats.aspx?s_no=" & label22.Text.ToString & "&" & "journey=" & Label6.Text & "&" & "seater=" & label4.Text & "&" & "sleeper=" & label2.Text & "&" & "service=" & lab5.Text.ToString)
    End If
End Sub

此代码给出错误:

在这一行

im gvRow As GridViewRow = CType(CType(sender,Control).Parent.Parent,GridViewRow)

错误:

无法将“ASP.vendors_select_service_aspx”类型的对象强制转换为“System.Web.UI.WebControls.GridViewRow”。

3 个答案:

答案 0 :(得分:2)

为什么要迭代Grid中的所有行?因此,您将重定向第一行而不是导致“Hold-Command”的行。 GridViewRow中任何控件的NamingContainer都是GridViewRow本身,这是FindControl获取标签引用所需的内容。

    If e.CommandName = "Hold" Then
        Dim row As GridViewRow = DirectCast(DirectCast(e.CommandSource, Control).NamingContainer, GridViewRow)
        Dim lab5 As Label = DirectCast(row.FindControl("Label5"), Label)
        Dim label2 As Label = DirectCast(row.FindControl("Label2"), Label)
        Dim label4 As Label = DirectCast(row.FindControl("Label4"), Label)
        Dim label6 As Label = DirectCast(row.FindControl("label6"), Label)
        Dim label22 As Label = DirectCast(row.FindControl("Label22"), Label)
        Me.Response.Redirect("Select_seats.aspx?s_no=" & label22.Text.ToString & "&" & "journey=" & label6.Text & "&" & "seater=" & label4.Text & "&" & "sleeper=" & label2.Text & "&" & "service=" & lab5.Text.ToString)
    End If

答案 1 :(得分:1)

TheChrisKent的答案是正确的方向,假设您正在使用TemplateField,您可以使用此类代码获取索引:

Dim gvRow As GridViewRow = CType(CType(sender, Control).Parent.Parent, GridViewRow)
Dim index As Integer = gvRow.RowIndex

如果仍然没有运气,因为您已被告知并发布您的.aspx代码,以便我们可以帮助而不是在黑暗中摸索。

答案 2 :(得分:0)

将代码切换为:

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
  If e.CommandName = "Hold" Then        
    Dim index As Integer = Convert.ToInt32(e.CommandArgument)
    Dim myRow As GridViewRow = GridView1.Rows(index)
    'Find the checkbox
    Dim lab5 As Label = DirectCast(myRow.FindControl("Label5"), Label)
    Dim label2 As Label = DirectCast(myRow.FindControl("Label2"), Label)
    Dim label4 As Label = DirectCast(myRow.FindControl("Label4"), Label)
    Dim label22 As Label = DirectCast(myRow.FindControl("Label22"), Label)
    Me.Response.Redirect("Select_seats.aspx?s_no=" & label22.Text.ToString & "&" & "journey=" & Label6.Text & "&" & "seater=" & label4.Text & "&" & "sleeper=" & label2.Text & "&" & "service=" & lab5.Text.ToString)
  End If
End Sub

另外添加(确保切换holdButton检索代码以匹配您正在使用的控件类型和索引)

Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowCreated

  ' The GridViewCommandEventArgs class does not contain a 
  ' property that indicates which row's command button was
  ' clicked. To identify which row's button was clicked, use 
  ' the button's CommandArgument property by setting it to the 
  ' row's index.
  If e.Row.RowType = DataControlRowType.DataRow Then

    ' Retrieve the LinkButton control from the first column.
    Dim holdButton As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton)

    ' Set the LinkButton's CommandArgument property with the
    ' row's index.
    holdButton.CommandArgument = e.Row.RowIndex.ToString()

  End If

End Sub

此代码来自此处的示例:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand(v=VS.80).aspx