【问题标题】:Button ClickEvent is not triggered未触发按钮 ClickEvent
【发布时间】:2012-09-19 10:53:52
【问题描述】:

下午,

我的网页上有两个按钮,用于锁定和解锁网页,以便用户可以锁定页面并对其进行编辑,而其他用户无法访问该记录,然后解锁该记录,以便其他用户可以编辑这个。

我遇到的问题是按钮不起作用,我不知道为什么。我正在使用图像按钮,但看起来事件没有被触发,我看不到问题,它让我发疯。有人可以看看我的代码...

   <asp:ImageButton ID="btnLock" runat="Server" 
       AlternateText="Click to lock record" ImageUrl="~/images/lock.png" />


   <asp:ImageButton ID="btnUnlock" runat="Server" 
       AlternateText="Click to unlock record" ImageUrl="~/images/unlock.png" />

   <asp:Label ID="lblUserName" runat="server" Font-Bold="True" Font-Size="Medium" 
            ForeColor="#CC3300"></asp:Label>
        <asp:HiddenField ID="hdnIsLockedBy" runat="server" />


 'VB Code for lock button...
  Protected Sub btnLock_Click(sender As Object, e As System.EventArgs) Handles btnLock.Click

    Dim lock As New WeeklyClass

    'Check that the Loggedby field is set to null so the user can then lock the record
    If String.IsNullOrEmpty(lock.LockedBy) Then
        'lock and add the username
        lock.LockedBy = User.Identity.Name
        'global variable islockedby
        hdnIsLockedBy.Value = User.Identity.Name
        'AgendaID required as part of the stored procedure 
        lock.AgendaID = Integer.Parse(lblAgendaNumber.Text)


    End If
    'Save to the database using the Class DAL and the Stored Procedure
    WeeklyClassDAL.LockWeeklyAgenda(lock)

    'Display buttons as expected result
    btnLock.Visible = False
    btnUnlock.Visible = True

    ' Refreshes fields on the page
    Response.Redirect("~/WeeklyAgenda.aspx?Edit=" & lblAgendaNumber.Text)

End Sub

  'VB Code for unlock button...
   Protected Sub btnUnlock_Click(sender As Object, e As System.EventArgs) Handles btnUnlock.Click

    Dim unlock As New WeeklyClass

    ' Check to see if the system has a username
    If hdnIsLockedBy.Value = User.Identity.Name Then
        'set the lockedby field to null
        unlock.LockedBy = hdnIsLockedBy.Value
        'pass the relevent agendaid
        unlock.AgendaID = Integer.Parse(lblAgendaNumber.Text)
    End If


    ' save to the database using the Class DAL
    WeeklyClassDAL.unLockWeeklyAgenda(unlock)

    'Display buttons as expected result
    btnLock.Visible = True
    btnUnlock.Visible = False

    ' Refreshes fields on the page
    Response.Redirect("~/WeeklyAgenda.aspx?Edit=" & lblAgendaNumber.Text)

End Sub

任何帮助都非常受欢迎。我一直在研究这个问题,但似乎找不到问题。

问候 贝蒂

【问题讨论】:

  • 考虑重新命名您的问题。这根本没有意义。

标签: asp.net .net vb.net visual-studio-2010 asp.net-controls


【解决方案1】:

您还没有订阅点击事件。您的控件不知道当用户单击它们时它必须调用这些函数。

订阅这些事件如下:

<asp:ImageButton ID="btnLock" runat="Server" 
       AlternateText="Click to lock record" ImageUrl="~/images/lock.png"  
       OnClick="btnLock_Click" />


   <asp:ImageButton ID="btnUnlock" runat="Server" 
       AlternateText="Click to unlock record" ImageUrl="~/images/unlock.png"            
       OnClick="btnUnloc_Click />

【讨论】:

    【解决方案2】:

    您需要在按钮中指定 Click 事件。

    OnClick="Button1_Click"
    

    所以你的按钮应该是:

    <asp:ImageButton 
                ID="btnLock" 
                runat="Server" 
                AlternateText="Click to lock record" 
                ImageUrl="~/images/lock.png" 
                OnClick="btnLock_Click" />
    
    <asp:ImageButton 
               ID="btnUnlock" 
               runat="Server" 
               AlternateText="Click to unlock record" 
               ImageUrl="~/images/unlock.png" 
               OnClick="btnUnloc_Click />
    

    【讨论】:

      【解决方案3】:

      您需要将 autopostback="true" 添加到按钮:

      <asp:ImageButton ID="btnLock" runat="Server" autopostback="true"
             AlternateText="Click to lock record" ImageUrl="~/images/lock.png" />
      

      否则后面的代码不会被触发。

      【讨论】:

      • 之后OP需要指定OnClick="MethodName",没有事件监听者会响应事件?
      • @DarshanJoshi ASP.net 将在发送者中捕获事件作为对象,即作为 System.EventArgs。如果您使用回发,则不需要在按钮上指定每个事件。
      • 也意味着如果你使用回发,你可以在后端代码中添加事件,而不必每次都更改前端代码。
      • 如果是这样,那么代码隐藏中的哪一部分代码将捕获该事件?表示哪个方法会响应?
      • 通过哪个事件,如果有人单击启用了回发的按钮,则 _click 甚至会触发。回传传递事件。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      • 2018-08-23
      • 2014-08-17
      相关资源
      最近更新 更多