【问题标题】:What's wrong with this ASP.NET code.... ? (Datagrid + Imagebutton)这个 ASP.NET 代码有什么问题....? (数据网格 + 图像按钮)
【发布时间】:2011-06-02 11:09:38
【问题描述】:
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”类型。

【问题讨论】:

  • 为什么是& "&" &"<string>" 而不是& "&<string>"?为什么你在lab5.Text 上调用ToString 而没有别的?为什么What's 用小写的w 拼写z 而不是s 并且没有撇号?
  • 上面已经提到了代码,,,,,
  • @user559800 来自.aspx 的代码是@bAN 需要看到的。
  • ASP 部分.. 与 gridview.. 你只提到了 VB 中的代码
  • 为什么要遍历 Grid 中的所有行?因此,您在第一行重定向,而不是在导致“Hold-Command”的行。

标签: asp.net vb.net


【解决方案1】:

将您的代码切换为:

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

【讨论】:

  • 在 Dim 索引中显示错误 As Integer = Convert.ToInt32(e.CommandArgument) 输入字符串的格式不正确。
  • 抱歉,忘记添加 RowCreated 事件处理程序
  • 这两个事件处理程序都应该在您的代码后面。如果您愿意,可以将 RowCreated 事件处理程序剪切并粘贴到 RowCommand 事件处理程序下方。如果您将 ASPX 页面的部分与 GridView 一起提供给我们,我可以更新事件处理程序以正确找到 holdButton。
【解决方案2】:

ChrisKent 的答案是正确的方向,假设你使用的是 TemplateField 你可以用这样的代码获取索引:

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

如果仍然没有运气,请按照您已经告知的方式发布您的 .aspx 代码,以便我们可以提供帮助,而不是在黑暗中摸索。

【讨论】:

    【解决方案3】:

    为什么要迭代 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
    

    【讨论】:

    • 无法将“ASP.vendors_select_service_aspx”类型的对象转换为“System.Web.UI.WebControls.GridViewRow”类型。
    • 我的错,发件人是gridView,它的NamingContainer是Page。使用 e.CommandSource 属性获取对 Imagebutton 的引用。编辑了我的答案。
    猜你喜欢
    • 2015-12-08
    • 1970-01-01
    • 2014-03-02
    • 2012-08-07
    • 2014-08-29
    • 2013-06-05
    • 2014-06-04
    • 2011-07-07
    • 1970-01-01
    相关资源
    最近更新 更多