简体   繁体   中英

ItemCommand of Repeater is not fire with LinkButton

I have a Linkbutton inside a repeater and I want to delete the Item when the user clicks on the Linkbutton; in this case the LinkButton's ItemCommand event is not fired, my code is below:

<asp:Repeater ID="rptSubject" runat="server" OnItemCommand="rptSubject_OnItemCommand">
        <ItemTemplate>
          <tr>
           <td><asp:CheckBox id="chkAll" runat="server"/></td>
            <td><%#Eval("SubjectName") %></td>
            <td> 
                <asp:ImageButton ID="imgbtnDelete" ImageUrl="~/assets/images/icons/delete.png" runat="server" CommandName="Delete" CommandArgument='<%#Eval("SubjectID") %>'/>
               <asp:LinkButton ID="lnkEditCategory" runat="server" CommandName="EditCategory" CommandArgument='<%#Eval("SubjectID") %>' Text="Edit Category"></asp:LinkButton> 

             </td>
          </tr>
     </ItemTemplate>

my repeater's itemcommand event handler is:

    protected void rptSubject_OnItemCommand(object source, RepeaterCommandEventArgs e)
{

    if (e.CommandName.Equals("Delete"))  
    {
        // some code
    }

    if (e.CommandName.Equals("EditCategory")) 
    {
   // some code
    }

}

when I click on the image button my item command event fires but when I click on the link button it doesn't.

The following code works for me:

<%@ Page Language="C#" %>
<script type="text/c#" runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var data = new[]
            {
                new 
                {
                    SubjectID = "1",
                    SubjectName = "subject name 1" 
                },
                new 
                {
                    SubjectID = "2",
                    SubjectName = "subject name 2" 
                },
            };
            rptSubject.DataSource = data;
            rptSubject.DataBind();
        }
    }

    protected void RptSubject_OnItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.Equals("Delete"))
        {
            // some code
        }

        if (e.CommandName.Equals("EditCategory"))
        {
            // some code
        }
    }    
</script>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form id="Form1" runat="server">
        <asp:Repeater ID="rptSubject" runat="server" OnItemCommand="RptSubject_OnItemCommand">
            <ItemTemplate>
                <div>
                    <asp:CheckBox id="chkAll" runat="server"/>
                    <%#Eval("SubjectName") %>
                    <asp:LinkButton ID="imgbtnDelete" runat="server" CommandName="Delete" CommandArgument='<%#Eval("SubjectID") %>' Text="Delete" />
                    <asp:LinkButton ID="lnkEditCategory" runat="server" CommandName="EditCategory" CommandArgument='<%#Eval("SubjectID") %>' Text="Edit" />
                </div>
            </ItemTemplate>
        </asp:Repeater>
    </form>
</body>
</html>

You also need to make sure you bind to the repeater explicitly

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    AddHandler rptPages.ItemCommand, AddressOf rptPages_ItemCommand
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM