0

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

Kuldip Rana
  • 131
  • 1
  • 7
  • 21
Paul
  • 620
  • 11
  • 35

1 Answers1

1

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();
        }
    }
Rob C
  • 818
  • 1
  • 12
  • 26
  • Hi, that does seems to resolve the issue and i can hit the command if i use load. can you please explain why this may be? – Paul Oct 05 '16 at 13:46
  • 1
    Hi, the page_load is where the server controls are created and initialized. See this link for more info on the two page events: https://msdn.microsoft.com/en-us/library/aa719775(vs.71).aspx – Rob C Oct 05 '16 at 14:35