2012-01-03 51 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在后面的代码中添加事件处理程序,如上所述。