DataList 未触发 ItemCommand 事件
我继承了一个正在测试的 VB.Net 应用程序,但 ItemCommand 事件未触发...它是一个 VB.Net 4.0 应用程序。
我在网上搜索了此错误,并仔细检查了应用程序中的代码。
我知道这个事件应该在 page_load 事件之后的回发时触发。但是,当我单击 ImageButton(强制回发并希望执行 ItemCommand 事件)时,Page.IsPostBack 属性仍设置为 FALSE,因此永远无法执行 ItemCommand 事件。我不知道为什么这个属性仍然被设置为FALSE。显然,我需要一种方法来向页面表明正在发生回发。 ImageButton 应该处理这个问题,因为它具有 runat="server" 标签。
下面是代码片段。有人可以告诉我我需要做什么才能触发 Item 命令吗?我上面说的都是真的,我相信。我不知道为什么在页面加载并按下 ImageButton 后该属性仍然设置为 FALSE。
超文本标记语言
<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()
I've inherited a VB.Net application that I'm testing and the ItemCommand event is not firing... It is a VB.Net 4.0 application.
I've searched the web for this error and doubhle checked the code in the application.
I know this event is supposed to fire on a postback after the page_load event. However, when I click on the ImageButton (to force a postback and hopefully execute the ItemCommand event), the Page.IsPostBack property is still set to FALSE, thereby never being able to execute the ItemCommand event. I don't know why this property would still be set to FALSE. Evidently, I need a way to signify to the page that a postback is occurring. The ImageButton should take care of this since it has the runat="server" tag.
Below are the code snippets. Can someone please inform me what I would need to do in order to fire the Item command? What I said above is true, I believe. I don't know why after the page loads and I press the ImageButton that the property would still be set to 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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我需要在 page_load 方法中添加一个事件处理程序,将 OnItemCommand 事件设置为触发:
在 C# 中,我注意到有时 aspx 文件并不总是触发事件触发。例如,如果您有 OnItemCommand="abc",则它不一定会触发(无论出于何种原因)。如果是这种情况,您应该强制 ASP.NET 在后面的代码中添加事件处理程序,如上所述。
I needed to add an event handler in the page_load method which would set the OnItemCommand event to fire:
In C#, I've noticed sometimes that the aspx file doesn't always trigger an event firing. For instance, if you have OnItemCommand="abc", it doesn't neccessarily fire (for whatever reason). If this is the case, you should force asp.net to add the event handler in the code behind as stated above.