0

这只是愚蠢的。我已经在这工作了 5 个多小时,无法弄清楚为什么我的该死的命令没有正确触发。唯一正确触发的是内置命令“编辑”和“取消”

标记

<asp:ListView ID="NewProduct" runat="server" DataSourceID="NewProductSDS" DataKeyNames="ID">
    <ItemTemplate>
        <div>
            <asp:LinkButton ID="accept" runat="server" CommandName="Accept" />
            <asp:LinkButton ID="edit" runat="server" CommandName="Edit" />
            <asp:LinkButton ID="delete" runat="server" CommandName="Reject" />
            <%# Eval("Product")%>
        </div>
    </ItemTemplate>
    <EditItemTemplate>
        <div>
            <asp:LinkButton ID="accept" runat="server" CommandName="Accept" />
            <asp:LinkButton ID="cancel" runat="server" CommandName="Cancel" />
            <asp:TextBox ID="NewProductName_tb" runat="server"></asp:TextBox>
        </div>
    </EditItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="NewProductSDS" runat="server"
    ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
    SelectCommand="select ID, Product from Products">
</asp:SqlDataSource>

代码隐藏

Protected Sub ItemBind(ByVal sender As Object, ByVal e As ListViewItemEventArgs) Handles NewProduct.ItemDataBound
    If e.Item.ItemType = ListViewItemType.DataItem Then
        sender.DataKeys(e.Item.DataItemIndex).Value.ToString() 'get the datakey
        'Display each key as it's created for troubleshooting.
        Label1.Text += "bound: " + sender.DataKeys(e.Item.DataItemIndex).Value.ToString() + "<br />"
    End If
End Sub
Protected Sub ItemClick(ByVal sender As Object, ByVal e As CommandEventArgs) Handles NewProduct.ItemCommand
    'Check if an event fired when a LinkButton is clicked.
    Label1.Text = "Command Event Fired"
    If e.CommandName = "Accept" Then
        Session.Add("PKey", sender.DataKeys(e.CommandArgument).Value.ToString)
        Label1.Text = "Accept key " + Session.Item("PKey")
    ElseIf e.CommandName = "Reject" Then
        Session.Add("PKey", sender.DataKeys(e.CommandArgument).Value.ToString)
        Label1.Text = "Reject key " + Session.Item("PKey")
    End If
End Sub

这就是我用来调试这些垃圾的所有代码。我想不通的最奇怪的事情是,所有的键都显示在新的页面加载上,就像这样......

bound: 9
bound: 12
bound: 27
bound: 31
bound: 32

然后突然当我单击一个内置命令(在这种情况下为编辑或取消,注意它们不在我的ItemCommand事件处理程序代码中)时,这个垃圾出现了,这意味着它在绑定之前看到了点击。

Command Event Firedbound: 9
bound: 12
bound: 27
bound: 31
bound: 32

不管发生了什么,我要解决的问题是我的自定义命令由于某种原因没有被识别。有任何想法吗?我已经搜索了高低的答案,但一无所获:(

如果您将所有这些代码复制到一个新项目中,它应该可以编译。我会非常感谢你的帮助。--- 我变得如此绝望,我正准备为ListView控制处理每一个可怕的事件,希望能揭示一些关于射击顺序的信息,也许能知道出了什么问题。--- :'(

更新:我做到了,哈哈。有趣,但不确定它告诉我什么新东西。以下是绑定所有事件的新页面加载时触发的内容:

Init
Load
DataBinding
ItemCreated
bound: 9
ItemCreated
bound: 12
ItemCreated
bound: 27
ItemCreated
bound: 31
ItemCreated
bound: 32
DataBound
PreRender
4

1 回答 1

1

我相信事件的顺序(ItemClick 然后 ItemDataBound)是正确的处理顺序。从客户端触发 ItemClick,然后,在将页面发送回用户之前,触发 ItemDataBound。

我建议尝试将特定的 OnClick 事件添加到您的每个自定义按钮以查看它们是否触发。

更新:

我怀疑您可能还会在 ItemClick 事件中遇到异常。如果您将标签设置移动到会话操作上方,您可能会看到标签正在使用您的自定义代码进行更新。

您应该将事件主体包装在异常处理程序中,并逐步调试以查看正在触发的异常。

通过将您正在使用的一些属性转换为它们的本机类型,您可能会得到更好的服务。例如:

Dim theListView As ListView

theListView = DirectCast(sender, ListView)

Dim theDataItem As ListViewDataItem

theDataItem = DirectCast(e.Item, ListViewDataItem)

If e.CommandName = "Accept" Then
    Label1.Text = "Accept key " + Session.Item("PKey")
    Session.Add("PKey", theListView.DataKeys(theDataItem.DisplayIndex).Value.ToString)
ElseIf e.CommandName = "Reject" Then
    Label1.Text = "Reject key " + Session.Item("PKey")
    Session.Add("PKey", theListView.DataKeys(theDataItem.DisplayIndex).Value.ToString)
End If
于 2011-11-05T02:10:07.380 回答