按钮OnItemCommand不在asp:Repeater中触发

时间:2016-10-05 10:31:30

标签: asp.net webforms postback onitemclick

我有一个Web表单项目,其中我将以下按钮包装在asp:Repeater

    <asp:Repeater ID="rptBookingSlots" OnItemCommand="BookingSlotOnItemCommand" runat="server">
    <ItemTemplate>
    <tr>
    <td>
        <asp:Button ID="Button3" CssClass="Delete" CommandName="delete" CommandArgument="<%# (Container.DataItem as RemoveGroup).Id %>" runat="server" Text="Delete" OnClientClick="return confirm('Are you sure you want to permanently delete this record);" />
    </td>
    </tr>
    </ItemTemplate>
    </asp:Repeater>

但是当我通过前端选择按钮时,它永远不会碰到我在后面的代码中指定的方法

protected void BookingSlotOnItemCommand(object source, RepeaterCommandEventArgs e)
    {
       switch (e.CommandName)
       {
         case "delete": //do work to delete record
       }
    }

我有一个page_preRender方法,总是在post pack上点击

    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (IsPostBack && !_dataBound)
        {
            BindAllData();
        }
    }

任何人都可以解释为什么我的onItemCommand方法在附加到进程时不会被命中,我无法删除记录?

谢谢

1 个答案:

答案 0 :(得分:1)

我这样做的方法是在page_load而不是pre_render中加载数据。

我测试了这个,按钮命令已进入

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            var myList = new List<string> {"foo", "bar"};
            rptBookingSlots.DataSource = myList;
            rptBookingSlots.DataBind();
        }
    }