简体   繁体   English

中继器的ItemCommand不能与LinkBut​​ton一起触发

[英]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; 我在中继器中有一个Linkbutton,当用户单击Linkbutton时,我想删除该项目。 in this case the LinkButton's ItemCommand event is not fired, my code is below: 在这种情况下,不会触发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>

my repeater's itemcommand event handler is: 我的中继器的itemcommand事件处理程序为:

    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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 中继器不会触发ItemCommand - Repeater does not fire ItemCommand Repeater中的按钮不会触发ItemCommand - Button in a Repeater does not fire ItemCommand 在Repeater Itemcommand事件中更改LinkBut​​ton的CSS类 - Change CSS class of LinkButton in Repeater Itemcommand Event ItemCommand事件不会与Repeater控件一起触发 - ItemCommand event doesn't fire with the Repeater control ItemCommand事件不会与母版页中的中继器控件一起触发 - ItemCommand event doesn't fire with repeater control in master page 在中继器中动态创建的LinkBut​​tons不会触发ItemCommand事件 - LinkButtons Created Dynamically in a Repeater Don't Fire ItemCommand Event 单击复合控件内的按钮时,不会触发Repeater ItemCommand - Repeater ItemCommand does not fire when clicking a button inside composite control 如何在c#.net中的嵌套转发器控件中触发ItemCommand参数子转发器 - How to fire ItemCommand argument child repeater in nested repeater control in c#.net 在按下Enter键时,在Repeater中创建一个asp:TextBox会触发Repeater的ItemCommand - Make an asp:TextBox inside a Repeater fire the Repeater's ItemCommand when Enter pressed 如何使用转发器的OnItemCommand触发文本框的事件,而不使用LinkBut​​ton? - How to fire event of textbox with OnItemCommand of a repeater , without using LinkButton?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM