【问题标题】:ItemCommand event not firing for DataList没有为 DataList 触发 ItemCommand 事件
【发布时间】:2012-01-03 17:58:04
【问题描述】:

我继承了一个我正在测试的 VB.Net 应用程序,但没有触发 ItemCommand 事件...这是一个 VB.Net 4.0 应用程序。

我在网上搜索了这个错误,并仔细检查了应用程序中的代码。

我知道这个事件应该在 page_load 事件之后在回发时触发。但是,当我单击 ImageButton(强制回发并希望执行 ItemCommand 事件)时,Page.IsPostBack 属性仍设置为 FALSE,因此永远无法执行 ItemCommand 事件。我不知道为什么这个属性仍然会设置为 FALSE。显然,我需要一种方法来向页面表明正在发生回发。 ImageButton 应该处理这个问题,因为它有 runat="server" 标记。

下面是代码sn-ps。有人可以告诉我我需要做什么才能触发 Item 命令吗?我上面说的是真的,我相信。我不知道为什么在页面加载后我按下 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()

【问题讨论】:

    标签: webforms datalist itemcommand


    【解决方案1】:

    我需要在 page_load 方法中添加一个事件处理程序,它将设置 OnItemCommand 事件触发:

    lstProducts.ItemCommand += new DataListCommandEventHandler(lstProducts_ItemCommand);
    

    在 C# 中,我注意到有时 aspx 文件并不总是触发事件触发。例如,如果您有 OnItemCommand="abc",它不一定会触发(无论出于何种原因)。如果是这种情况,您应该强制 asp.net 在后面的代码中添加事件处理程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多