使用LinkBut​​ton不会触发Repeater的ItemCommand

时间:2011-07-25 10:02:37

标签: c# asp.net

我在转发器中有一个Linkbutton,我想在用户点击Linkbutton时删除Item;在这种情况下,LinkBut​​ton的ItemCommand事件不会被触发,我的代码如下:

<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>

我的转发器的itemcommand事件处理程序是:

    protected void rptSubject_OnItemCommand(object source, RepeaterCommandEventArgs e)
{

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

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

}

当我点击图片按钮时,我的项目命令事件会触发但是当我点击链接按钮时它不会触发。

2 个答案:

答案 0 :(得分:2)

以下代码适用于我:

<%@ 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>

答案 1 :(得分:1)

您还需要确保明确绑定到转发器

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