2011-07-25 126 views
1

火我有一個LinkBut​​ton中繼器內,我想,當用戶點擊LinkBut​​ton的刪除的項目;在這種情況下,不會觸發LinkBut​​ton的ItemCommand事件,我的代碼如下:ItemCommand不與LinkBut​​ton的

<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

下面的代碼對我的作品:

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

您還需要確保你綁定到中繼明確

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