RadListView ItemCommand 未触发

发布于 2024-12-07 22:52:49 字数 6870 浏览 1 评论 0原文

我有一个 RadListView 以及一个绑定到 lstGameWaypoints_ItemCommand 的事件处理程序,如下所示。在网格首次显示时,单击删除按钮成功触发回发并触发 ItemCommand。

回发完成后,事件处理程序似乎不会再次连接,并且单击删除按钮不会导致任何操作发生。

以前有人见过类似的问题吗?

<telerik:RadListView runat="server" ID="RadListView1" 
DataSourceID="ObjectDataSource1" AllowPaging="true" 
OnDataBound="lstGameWaypoints_DataBound" 
OnItemCreated="lstGameWaypoints_ItemCreated" 
OnItemCommand="lstGameWaypoints_ItemCommand">
<EmptyDataTemplate>
    <div style="width: 100px;margin: 10px auto auto auto;"><button class="AddWaypoint">Add Waypoint</button></div>
</EmptyDataTemplate>
<ItemTemplate>
    <td>
        <table style="border:1px solid black;width: 232px;height: 70px;margin:auto;">
            <tr>
                <td>
                    Time:
                </td>
                <td>
                    <%# string.Format("{0:h:mm tt}", Eval("DateTime")) %>
                </td>
            </tr>
            <tr>
                <td>
                    Date:
                </td>
                <td>
                    <%# string.Format("{0:d}", Eval("DateTime")) %>
                </td>
            </tr>
            <tr>
                <td>
                    Chip Stack:
                </td>
                <td>
                    <%# string.Format("{0:C}", Eval("ChipStack")) %>
                </td>
            </tr>
            <tr>
                <td>
                    Notes:
                </td>
                <td>
                    <%# Eval("Notes") %>
                </td>
            </tr>
            <tr>    
                <td colspan="2" style="text-align:right;">
                    <asp:Button runat="server" ID="btnDelete" CssClass="deleteButton"  CommandArgument='<%# Eval("WaypointID") %>' CommandName="Delete" Text="X" OnClientClick="return confirm('Are you sure you want to delete this waypoint?');" />
                </td>
            </tr>
        </table>
    </td>
</ItemTemplate>
</telerik:RadListView>

protected void lstGameWaypoints_ItemCommand(object sender, Telerik.Web.UI.RadListViewCommandEventArgs e)
{
    if (e.CommandName == "Delete")
    {
        //Do stuff
    }
}

更新:

看来这是我用来托管该控件面板的 RadAjaxManager 的问题。如果我在面板上禁用 ajax,一切都会按预期工作。

这是完整的代码...

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"> 
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="panGameWaypoints">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="panGameWaypoints" LoadingPanelID="RadAjaxLoadingPanel1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="panGameDetails">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="panGameDetails" LoadingPanelID="RadAjaxLoadingPanel1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
        <ClientEvents OnRequestStart="RequestStart" OnResponseEnd="RequestEnd" />
    </telerik:RadAjaxManager>

    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Transparency="60">
        <div class="loading">
            <asp:Image ID="Image1" runat="server" ImageUrl="~/Images/loading3.gif" AlternateText="loading" />
        </div>
    </telerik:RadAjaxLoadingPanel>

<asp:Panel runat="server" ID="panGameWaypoints" CssClass="GameWaypoints">
<table style="width:900px;margin:auto;">
        <tr>
            <telerik:RadListView runat="server" ID="lstGameWaypoints" AllowPaging="false" 
                OnDataBound="lstGameWaypoints_DataBound" 
                OnItemCreated="lstGameWaypoints_ItemCreated" 
                OnNeedDataSource="lstGameWaypoints_NeedDataSource">

                <EmptyDataTemplate>
                    <div style="width: 100px;margin: 10px auto auto auto;"><button class="AddWaypoint">Add Waypoint</button></div>
                </EmptyDataTemplate>
                <ItemTemplate>
                    <td>
                        <table style="border:1px solid black;width: 232px;height: 70px;margin:auto;">
                            <tr>
                                <td>
                                    Time:
                                </td>
                                <td>
                                    <%# string.Format("{0:h:mm tt}", Eval("DateTime")) %>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Date:
                                </td>
                                <td>
                                    <%# string.Format("{0:d}", Eval("DateTime")) %>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Chip Stack:
                                </td>
                                <td>
                                    <%# string.Format("{0:C}", Eval("ChipStack")) %>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Notes:
                                </td>
                                <td>
                                    <%# Eval("Notes") %>
                                </td>
                            </tr>
                            <tr>    
                                <td colspan="2" style="text-align:right;">
                                    <asp:Button runat="server" ID="btnDelete" CssClass="deleteButton" CommandArgument='<%# Eval("WaypointID") %>' CommandName="Delete" Text="X" OnClientClick="return confirm('Are you sure you want to delete this waypoint?');" />
                                </td>
                            </tr>
                        </table>
                    </td>
                </ItemTemplate>
            </telerik:RadListView>
    </tr>
</table>
</asp:Panel>

I have a RadListView and also an event handler bound to lstGameWaypoints_ItemCommand as shown below. On First display of the grid clicking the delete button successfully triggers a postback and the ItemCommand firing.

Once the postback completes it appears that the event handlers are not being wired up again and clicking the delete button causes no action to occur.

Has anyone seen a similar issue before?

<telerik:RadListView runat="server" ID="RadListView1" 
DataSourceID="ObjectDataSource1" AllowPaging="true" 
OnDataBound="lstGameWaypoints_DataBound" 
OnItemCreated="lstGameWaypoints_ItemCreated" 
OnItemCommand="lstGameWaypoints_ItemCommand">
<EmptyDataTemplate>
    <div style="width: 100px;margin: 10px auto auto auto;"><button class="AddWaypoint">Add Waypoint</button></div>
</EmptyDataTemplate>
<ItemTemplate>
    <td>
        <table style="border:1px solid black;width: 232px;height: 70px;margin:auto;">
            <tr>
                <td>
                    Time:
                </td>
                <td>
                    <%# string.Format("{0:h:mm tt}", Eval("DateTime")) %>
                </td>
            </tr>
            <tr>
                <td>
                    Date:
                </td>
                <td>
                    <%# string.Format("{0:d}", Eval("DateTime")) %>
                </td>
            </tr>
            <tr>
                <td>
                    Chip Stack:
                </td>
                <td>
                    <%# string.Format("{0:C}", Eval("ChipStack")) %>
                </td>
            </tr>
            <tr>
                <td>
                    Notes:
                </td>
                <td>
                    <%# Eval("Notes") %>
                </td>
            </tr>
            <tr>    
                <td colspan="2" style="text-align:right;">
                    <asp:Button runat="server" ID="btnDelete" CssClass="deleteButton"  CommandArgument='<%# Eval("WaypointID") %>' CommandName="Delete" Text="X" OnClientClick="return confirm('Are you sure you want to delete this waypoint?');" />
                </td>
            </tr>
        </table>
    </td>
</ItemTemplate>
</telerik:RadListView>

protected void lstGameWaypoints_ItemCommand(object sender, Telerik.Web.UI.RadListViewCommandEventArgs e)
{
    if (e.CommandName == "Delete")
    {
        //Do stuff
    }
}

UPDATE:

It appears that this is a issue with the RadAjaxManager that I am using to host the panel for this control. If I disable ajax on the panel, everything works as expected.

This is a full code...

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"> 
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="panGameWaypoints">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="panGameWaypoints" LoadingPanelID="RadAjaxLoadingPanel1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="panGameDetails">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="panGameDetails" LoadingPanelID="RadAjaxLoadingPanel1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
        <ClientEvents OnRequestStart="RequestStart" OnResponseEnd="RequestEnd" />
    </telerik:RadAjaxManager>

    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Transparency="60">
        <div class="loading">
            <asp:Image ID="Image1" runat="server" ImageUrl="~/Images/loading3.gif" AlternateText="loading" />
        </div>
    </telerik:RadAjaxLoadingPanel>

<asp:Panel runat="server" ID="panGameWaypoints" CssClass="GameWaypoints">
<table style="width:900px;margin:auto;">
        <tr>
            <telerik:RadListView runat="server" ID="lstGameWaypoints" AllowPaging="false" 
                OnDataBound="lstGameWaypoints_DataBound" 
                OnItemCreated="lstGameWaypoints_ItemCreated" 
                OnNeedDataSource="lstGameWaypoints_NeedDataSource">

                <EmptyDataTemplate>
                    <div style="width: 100px;margin: 10px auto auto auto;"><button class="AddWaypoint">Add Waypoint</button></div>
                </EmptyDataTemplate>
                <ItemTemplate>
                    <td>
                        <table style="border:1px solid black;width: 232px;height: 70px;margin:auto;">
                            <tr>
                                <td>
                                    Time:
                                </td>
                                <td>
                                    <%# string.Format("{0:h:mm tt}", Eval("DateTime")) %>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Date:
                                </td>
                                <td>
                                    <%# string.Format("{0:d}", Eval("DateTime")) %>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Chip Stack:
                                </td>
                                <td>
                                    <%# string.Format("{0:C}", Eval("ChipStack")) %>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Notes:
                                </td>
                                <td>
                                    <%# Eval("Notes") %>
                                </td>
                            </tr>
                            <tr>    
                                <td colspan="2" style="text-align:right;">
                                    <asp:Button runat="server" ID="btnDelete" CssClass="deleteButton" CommandArgument='<%# Eval("WaypointID") %>' CommandName="Delete" Text="X" OnClientClick="return confirm('Are you sure you want to delete this waypoint?');" />
                                </td>
                            </tr>
                        </table>
                    </td>
                </ItemTemplate>
            </telerik:RadListView>
    </tr>
</table>
</asp:Panel>

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

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

发布评论

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

评论(5

z祗昰~ 2024-12-14 22:52:49

我创建了一个小测试用例(包含在下面)来看看是否可以重现您的问题,您的实现和我的实现之间存在三个主要区别:

  1. 我没有指定 RadAjaxLoadingPanel
  2. 我没有指定任何 ClientEvents(即 RequestStartRequestEnd
  3. 我没有使用 OnClientClick 确认对话框

Solution # 1

经过之后并将这些内容添加到我的测试用例中,我发现使用 RadAjaxManager 的确认对话框存在问题。一些研究提供了以下解决方案:

将您的 OnClientClick 代码更改为以下内容:

OnClientClick="if(!confirm('Are you sure you want delete this?')) return false;"

这可以解决您的问题吗?

以下是有关该问题的一些文档:
www.telerik.com/help/aspnet-ajax/ajax-confirmation.html


上面应该可以解决您的问题,但如果没有,请尝试暂时删除 RequestStartRequestEnd 客户端事件,并继续剥离“多余的”,直到找到问题为止。

这是一个按预期工作的测试用例:

ASPX:

<telerik:RadAjaxManager ID="rda" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="pnlTest">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="pnlTest" />
            </UpdatedControls>              
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<asp:Panel ID="pnlTest" runat="server">
    <telerik:RadListView ID="lst" runat="server" OnItemCommand="lst_Command" OnNeedDataSource="lst_NeedDataSource">
        <ItemTemplate>
            <%#Eval("Test")%>
            <asp:Button ID="btnDelete" runat="server" CommandArgument='<%#Eval("Test")%>' OnClientClick="if(!confirm('Are you sure you want delete this?')) return false;" CommandName="Delete" Text="Test" />
        </ItemTemplate>
    </telerik:RadListView>
    <asp:Label ID="lblCommandArg" runat="server"></asp:Label>
</asp:Panel>

代码隐藏:

protected void lst_NeedDataSource(object sender, RadListViewNeedDataSourceEventArgs e)
{
    DataTable table = new DataTable();
    table.Columns.Add("Test", typeof(string));

    DataRow row = table.NewRow();
    row.SetField<string>("Test", "Testing");
    table.Rows.Add(row);

    row = table.NewRow();
    row.SetField<string>("Test", "Testing again");
    table.Rows.Add(row);

    lst.DataSource = table;
}

protected void lst_Command(object sender, RadListViewCommandEventArgs e)
{
    lblCommandArg.Text = e.CommandArgument.ToString();
}

使用 RadAjaxPanel 而不是 RadAjaxManager

查看您的代码,它看起来像最简单的解决方案可能是使用 RadAjaxPanel 而不是 RadAjaxManager。我不确定这是否适合您,但这可能是让您的代码正常工作的最简单方法。

使用 RadAjaxPanel 作为 panGameWaypoints 而不是常规面板,这将确保其中包含的所有内容都将使用 AJAX:

<telerik:RadAjaxPanel ID="panGameWaypoints" runat="server">
    <telerik:RadListView ID="RadListView1" runat="server" ... >
</telerik:RadAjaxPanel>

I created a little test case (included below) to see if I could reproduce your issue, and there were three main differences between your implementation and mine:

  1. I didn't specify a RadAjaxLoadingPanel
  2. I didn't specify any ClientEvents (i.e. RequestStart, RequestEnd)
  3. I didn't use an OnClientClick confirm dialog

Solution #1

After going through and adding each of these to my test case, I discovered that there was an issue using a confirm dialog with the RadAjaxManager. A little research provided the following solution:

Change your OnClientClick code to the following:

OnClientClick="if(!confirm('Are you sure you want delete this?')) return false;"

Does this fix your issue?

Here's some documentation on the issue:
www.telerik.com/help/aspnet-ajax/ajax-confirmation.html


The above should fix your problem, but if not try temporarily removing the RequestStart and RequestEnd client events, and continue to peel "extras" until you find the problem.

Here is a test case that works as expected:

ASPX:

<telerik:RadAjaxManager ID="rda" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="pnlTest">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="pnlTest" />
            </UpdatedControls>              
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<asp:Panel ID="pnlTest" runat="server">
    <telerik:RadListView ID="lst" runat="server" OnItemCommand="lst_Command" OnNeedDataSource="lst_NeedDataSource">
        <ItemTemplate>
            <%#Eval("Test")%>
            <asp:Button ID="btnDelete" runat="server" CommandArgument='<%#Eval("Test")%>' OnClientClick="if(!confirm('Are you sure you want delete this?')) return false;" CommandName="Delete" Text="Test" />
        </ItemTemplate>
    </telerik:RadListView>
    <asp:Label ID="lblCommandArg" runat="server"></asp:Label>
</asp:Panel>

Code-behind:

protected void lst_NeedDataSource(object sender, RadListViewNeedDataSourceEventArgs e)
{
    DataTable table = new DataTable();
    table.Columns.Add("Test", typeof(string));

    DataRow row = table.NewRow();
    row.SetField<string>("Test", "Testing");
    table.Rows.Add(row);

    row = table.NewRow();
    row.SetField<string>("Test", "Testing again");
    table.Rows.Add(row);

    lst.DataSource = table;
}

protected void lst_Command(object sender, RadListViewCommandEventArgs e)
{
    lblCommandArg.Text = e.CommandArgument.ToString();
}

Using RadAjaxPanel instead of RadAjaxManager

Looking at your code, it looks like the easiest solution might be to use the RadAjaxPanel instead of the RadAjaxManager. I'm not sure if this is an option for you, but that's probably the easiest way to get your code working.

Use a RadAjaxPanel for panGameWaypoints instead of a regular Panel, which will ensure that everything contained within will use AJAX:

<telerik:RadAjaxPanel ID="panGameWaypoints" runat="server">
    <telerik:RadListView ID="RadListView1" runat="server" ... >
</telerik:RadAjaxPanel>
素罗衫 2024-12-14 22:52:49

您是否尝试将以下代码添加到 Page_Init 事件?

RadListView1.OnDataBound += new EventHandler(lstGameWaypoints_DataBound);
RadListView1.OnItemCreated += new EventHandler(lstGameWaypoints_ItemCreated);
RadListView1.OnItemCommand += new EventHandler(lstGameWaypoints_ItemCommand);

这应该在每次回发时挂钩您的事件。

Did you try to add following code to Page_Init event ?

RadListView1.OnDataBound += new EventHandler(lstGameWaypoints_DataBound);
RadListView1.OnItemCreated += new EventHandler(lstGameWaypoints_ItemCreated);
RadListView1.OnItemCommand += new EventHandler(lstGameWaypoints_ItemCommand);

This should hook your events on each postback.

指尖微凉心微凉 2024-12-14 22:52:49

我认为您需要告诉 RadAjaxManager 从自身更新列表...?这是没有意义的,但请尝试将其添加到您的 RadAjaxManager 中:

<telerik:AjaxSetting AjaxControlID="lstGameWaypoints">
   <UpdatedControls>
      <telerik:AjaxUpdatedControl ControlID="lstGameWaypoints" LoadingPanelID="RadAjaxLoadingPanel1" />
   </UpdatedControls>
</telerik:AjaxSetting>

这样 RadListView 将重新连接它的事件。

I think you need to tell RadAjaxManager to update the list from itself...? That makes no sense but try adding this to your RadAjaxManager:

<telerik:AjaxSetting AjaxControlID="lstGameWaypoints">
   <UpdatedControls>
      <telerik:AjaxUpdatedControl ControlID="lstGameWaypoints" LoadingPanelID="RadAjaxLoadingPanel1" />
   </UpdatedControls>
</telerik:AjaxSetting>

That way the RadListView will rewire it's events.

等你爱我 2024-12-14 22:52:49

我不认为您需要添加

<telerik:AjaxSetting AjaxControlID="panGameWaypoints">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="panGameWaypoints" LoadingPanelID="RadAjaxLoadingPanel1" />
            </UpdatedControls>
        </telerik:AjaxSetting>

Use this 而不是上面

<telerik:AjaxSetting AjaxControlID="lstGameWaypoints">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="lstGameWaypoints" LoadingPanelID="RadAjaxLoadingPanel1" />
            </UpdatedControls>
        </telerik:AjaxSetting>

,而且我没有看到 lstGameWaypoints ItemCommand 事件已连接,您也可以使用 Itemcommand 的 OnItemDeleting 事件。

I don't thing you need to add

<telerik:AjaxSetting AjaxControlID="panGameWaypoints">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="panGameWaypoints" LoadingPanelID="RadAjaxLoadingPanel1" />
            </UpdatedControls>
        </telerik:AjaxSetting>

Use this instead of above

<telerik:AjaxSetting AjaxControlID="lstGameWaypoints">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="lstGameWaypoints" LoadingPanelID="RadAjaxLoadingPanel1" />
            </UpdatedControls>
        </telerik:AjaxSetting>

and i don't see lstGameWaypoints ItemCommand event is wired and you also can use OnItemDeleting event insted of itemcommand.

请爱~陌生人 2024-12-14 22:52:49

我认为问题出在您附加在 btnDelete 上的 OnClientClick 事件上。

我尝试模拟您的情况,并用 RadButton 替换了 Button(您处于 Telerik 环境中,对吧?),它公开了 OnClientClicking 事件。所以我创建了一个 js 函数:

function RadConfirm_Delete(sender, args)
{
    var callBackFunction = Function.createDelegate(sender, function (shouldSubmit)
    {
        if (shouldSubmit)
        {
            this.click();
        }
    });

    radconfirm("Are you sure you want to delete this waypoint?", callBackFunction, 350, 100, null, "Confirm delete");
    args.set_cancel(true);
}

并且我将 RadButton 设置为:

<telerik:RadButton runat="server" ID="btnDelete" CssClass="deleteButton" CommandArgument='<%# Eval("WaypointID") %>' CommandName="Delete" Text="X" OnClientClicking="RadConfirm_Delete" />

一切似乎都工作正常。

让我知道!

I think the problem is on the OnClientClick event you attached on the btnDelete.

I tried to simulate your situation, and I've replaced the Button with a RadButton (you're in a Telerik environment, right?), that expose the event OnClientClicking. So I've created a js function:

function RadConfirm_Delete(sender, args)
{
    var callBackFunction = Function.createDelegate(sender, function (shouldSubmit)
    {
        if (shouldSubmit)
        {
            this.click();
        }
    });

    radconfirm("Are you sure you want to delete this waypoint?", callBackFunction, 350, 100, null, "Confirm delete");
    args.set_cancel(true);
}

and I've setted the RadButton like:

<telerik:RadButton runat="server" ID="btnDelete" CssClass="deleteButton" CommandArgument='<%# Eval("WaypointID") %>' CommandName="Delete" Text="X" OnClientClicking="RadConfirm_Delete" />

All seems to work right.

Let me know!

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