我继承了一个正在测试的VB.Net应用程序,但ItemCommand事件没有触发...它是一个VB.Net 4.0应用程序。
我在网上搜索了这个错误,并怀疑检查了应用程序中的代码。
我知道这个事件应该在page_load事件之后的回发中触发。但是,当我单击ImageButton (强制回发并希望执行ItemCommand事件)时,Page.IsPostBack属性仍然设置为FALSE,因此永远不能执行ItemCommand事件。我不知道为什么这个属性仍然设置为FALSE。显然,我需要一种方法来向页面表示正在发生回发。服务器应该会处理这个问题,因为它有runat=“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()发布于 2012-01-06 06:17:17
我需要在page_load方法中添加一个事件处理程序,它会将OnItemCommand事件设置为触发:
lstProducts.ItemCommand += new DataListCommandEventHandler(lstProducts_ItemCommand);在C#中,我有时注意到aspx文件并不总是触发事件触发。例如,如果你有OnItemCommand="abc",它不一定会触发(不管是什么原因)。如果是这种情况,您应该强制asp.net在代码中添加事件处理程序,如上所述。
https://stackoverflow.com/questions/8716578
复制相似问题