【问题标题】:error deleting items from listview从列表视图中删除项目时出错
【发布时间】:2013-03-18 09:44:10
【问题描述】:

嗨,

我这里有问题。我得到了一个数据表,数据显示在列表视图中。这是后面的代码功能。

Protected Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As ListViewCommandEventArgs)
        If (e.CommandName) = "Sort" Then
            Dim txteno As Label = DirectCast(e.Item.FindControl("lblID"), Label)
            /* Error here Dim deletecommand As String = "delete from dt where ID=" & Convert.ToInt32(txteno.Text)
            Session("dt").DeleteCommand = deletecommand
        End If
    End Sub

问题是当我按下删除按钮时,出现错误提示“输入字符串的格式不正确”。有谁知道这里有什么问题吗?

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    问题是由

    引起的

    Convert.ToInt32(txteno.Text)

    并且txteno.Text 的值不是有效整数。

    当您尝试运行代码时,txteno.Text 的值是多少?

    作为一种解决方法,请改用Integer.TryParse

    Dim number As Integer
    
    If Integer.TryParse(txteno.Text, number) Then
    
     ' Do the delete....
    End If
    

    【讨论】:

    • 它的值为 ""。我对这个问题有所了解,但不知道如何解决。在 delte 命令中,我需要指定表名,但问题是表中的数据是我从用户那里收到的,这意味着 sqldatasource 是数据表。我的问题是如何在列表视图中声明数据表的表名?
    • 没错。该行中的断点并检查 txteno 的实际值是多少。
    【解决方案2】:

    您的问题是您无法通过ListView_ItemCommand 方法访问e.Item.FindControl

    FindControl 仅适用于渲染时间,即在ListView_ItemDataBound 方法中。

    一种可能的解决方案是从列表中或直接从数据库中检索项目。为此,您可以使用e.Item.DataItemIndex 来检索行的id。

    【讨论】:

    • 我在可用命令列表中找不到命令 e.item.dataitemindex。
    • 您是否尝试从ListView_ItemCommand 方法内部访问它(它不是“命令”,它是一个属性)?我不假设...它存在:P -> stackoverflow.com/questions/2974236/…
    猜你喜欢
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多