ItemCommand 在回发时在 ItemDataBound 之前触发?啥?

发布于 2024-12-13 16:41:30 字数 3262 浏览 3 评论 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

This is just stupid. I've been at this for over 5 hours and can't figure out why my freaking commands aren't firing properly. The only ones that fire properly are the Built-in commands "Edit" & "Cancel"

Markup

<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>

Codebehind

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

That's all the code I'm using to try to debug this garbage. The weirdest thing that I can't figure out is that all the keys display on a fresh page load like so...

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

Then suddenly when I click a built-in Command (Edit or Cancel in this case, notice they are not in my ItemCommand event handler code) this garbage shows up, implying to me that it's seeing the click before binding.

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

Whatever the heck is going on, the problem that I'm trying to solve is that my custom Commands are not being recognized for some reason. Any ideas? I've searched high and low for answers and nothing :(

If you copied all this code into a new project it should compile. I would be EVER so grateful for your help. --- I'm getting so desperate I'm about to bind to handle every freaking event for the ListView control in hopes of revealing something about the firing order and perhaps get an idea what's going wrong. --- :'(

UPDATE: I did it lol. Interesting, but not sure it tells me anything new. Here's what fires on a fresh page load with all events bound:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衬遮猫 2024-12-20 16:41:30

我相信事件的顺序(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

I believe that the order of events (ItemClick then ItemDataBound) is the correct order of processing. The ItemClick is fired from the client, then, before the page is sent back to the user, ItemDataBound is fired.

I would suggest trying to add specific OnClick events to each of your custom buttons to see if they fire.

Update:

I suspect that you may also be encountering exceptions within the ItemClick event. If you move the setting of the label above the Session manipulation, you may see the label being updated with your custom code.

You should wrap the body of the event in an exception handler and step through in debug to see what exception is being fired.

You would probably also be better served by casting some of the properties you are working with to their native types. For example:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文