2012-01-03 55 views
0

我繼承了一個VB.Net應用程序,我正在測試並且ItemCommand事件不會觸發......它是一個VB.Net 4.0應用程序。ItemCommand事件不會觸發DataList

我在網上搜索了這個錯誤,並且在應用程序中檢查了代碼。

我知道這個事件應該在page_load事件後的回發上觸發。但是,當我單擊ImageButton(強制回發並希望執行ItemCommand事件)時,Page.IsPostBack屬性仍設置爲FALSE,因此永遠無法執行ItemCommand事件。我不知道爲什麼這個屬性仍然會設置爲FALSE。顯然,我需要一種方式來表示回傳正在發生。由於它具有runat =「server」標籤,因此ImageButton應該注意這一點。

下面是代碼片段。有人能告訴我爲了發射物品命令我需要做什麼嗎?我相信,上面所說的是真實的。我不知道爲什麼在頁面加載後,我按下ImageButton屬性仍將設置爲FALSE。

HTML

<asp:DataList ID="lstReferrals" runat="server" DataKeyField="ReferringAffiliateID" 
    OnItemCommand="lstReferrals_ItemCommand" CellPadding="4" Summary="Referral Design Table" 
    Width="800"><ItemTemplate> 
     <tr class="small" bgcolor="#FFFFFF"> 
      <td> 
       <asp:ImageButton ID="btnSelect" AlternateText="Select" ImageUrl='<%# NodeImage(1) %>' 
        CommandName="select" runat="server" />CODE BEHINDPrivate Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 



'Put user code to initialize the page here 
     If Not (Request.Params("ItemIndex") Is Nothing) Then 
      itemIndex = Int32.Parse(Request.Params("ItemIndex")) 
     Else 
      itemIndex = Convert.ToInt32(Null.SetNull(itemIndex)) 
     End If 


     If Not Page.IsPostBack Then 
      LoadReferrals() 

      If Not Null.IsNull(itemIndex) Then 
       lstReferrals.SelectedIndex = itemIndex 
       LoadReferrals() 
      End If 
     End If 


    End Sub 



Protected Sub lstReferrals_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles lstReferrals.ItemCommand 

     Try 
      errormessage.Visible = False 
      ' Determine the command of the button (either "select" or "collapse") 
      Dim command As String = CType(e.CommandSource, ImageButton).CommandName 



      ' Update asp:datalist selection index depending upon the type of command 
      ' and then rebind the asp:datalist with content 
      Select Case command 
       Case "collapse" 
        lstReferrals.SelectedIndex = -1 
        LoadReferrals() 
       Case "select" 
        lstReferrals.SelectedIndex = e.Item.ItemIndex 
        LoadReferrals() 

回答

1

我需要在這將在OnItemCommand事件設置爲火Page_Load方法添加事件處理程序:

lstProducts.ItemCommand += new DataListCommandEventHandler(lstProducts_ItemCommand); 

在C#中,我有時會注意到,ASPX文件並不總是觸發事件觸發。例如,如果你有OnItemCommand =「abc」,它不會一直觸發(無論什麼原因)。如果是這種情況,你應該強制asp.net在後面的代碼中添加事件處理程序,如上所述。