Search code examples
c#.netasp.netdatalist

Cannot fire the DeleteCommand in a Datalist


I want to fire the Delete Command in Datalist Control .. but it is not firing .. Help please ..

this is my code :

protected void DeleteCommand(object source, DataListCommandEventArgs e)
{
        Label2.Text = "hello";
}

and This is my html code :

   <asp:DataList ID="DLImages" runat="server" BorderStyle="None" 
         DataKeyField="fId" RepeatColumns="4" 
         RepeatDirection="Horizontal" ShowFooter="False" ShowHeader="False" 
         OnDeleteCommand="DeleteCommand" 
         onitemdatabound="DLImages_ItemDataBound">
             <ItemTemplate>
                   <asp:ImageButton ID="IBDelete" runat="server" BorderStyle="None" CommandName="Delete" ImageUrl="~/Dashboard/Images/dldelete.png" />
             </ItemTemplate>
    </asp:DataList>

..


Solution

  • Your code looks like OK to me. It should fire the DeleteCommand.

    But the problem is that I am sure you are binding the Datalist in your page_load event, but not under If(!IsPostBack) condition. What happens when you hit Delete button is your page_load event fires before your DeleteCommand and it rebinds the DataList and your event is lost

    Your page_load event code should look like...

    If(!IsPostBack) 
    {
      DataList binding code goes here......
      ...........................
    }
    
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Bind the DataList here....
    }