Paul Paul - 1 year ago 101
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">
<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);" />

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)

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


Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download