Paul Paul - 2 months ago 5
ASP.NET (C#) Question

button OnItemCommand not firing inside asp:Repeater

I have a web forms project where i have the following button wrapped in an 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>


But when I select the button via the front end it never hits the method I have specified in my code behind

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


I have a page_preRender method which is always hit on the post pack

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


Can anyone explain why my onItemCommand method wont get hit when im attached to the process, i am unable to delete records?

Thanks

Answer

The way I would do this would be to load the data in the page_load rather than the pre_render.

I have tested this and the button command is stepped into

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