RadListView ItemCommand没有触发

时间:2011-10-04 08:29:20

标签: c# asp.net events telerik telerik-grid

我有一个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>

5 个答案:

答案 0 :(得分:1)

我创建了一个小测试用例(包含在下面)以确定我是否可以重现您的问题,并且您的实现和我的实现之间存在三个主要差异:

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

  4. 解决方案#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。我不确定这是否适合您,但这可能是让您的代码正常工作的最简单方法。

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

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

答案 1 :(得分:0)

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

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

这应该将你的事件挂钩在每个回发上。

答案 2 :(得分:0)

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

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

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

答案 3 :(得分:0)

我不需要添加

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

使用此代替上面的

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

我没有看到lstGameWaypoints ItemCommand事件是有线的,你也可以使用item命令的OnItemDeleting事件。

答案 4 :(得分:0)

我认为问题出在你附加在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" />

一切似乎都正常。

让我知道!