Button ClickEvent is not triggered

徘徊边缘 提交于 2019-12-25 18:17:23

问题


Afternoon All,

I have two buttons on my web page that is used to lock and unlock a web page so that a user can lock the page and edit this without other users being able to access the record and then unlock this record so other users can edit this.

The problem i have is that the buttons doesnt work and im not to sure why. I am using image buttons but it looks like the event is not being triggered, i cant see the problem and its driving me crazy. Can some one please take a look at my code...

   <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

Any help is much appriechiated. I have been looking at this for ages and cant seem to find the issue.

Regards Betty


回答1:


You have not subscribed to the click event. Your control does not know that it has to call those functions when a user clicks them.

Subscribe to those events as follows:

<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:


You need to specify the Click event in your buttons.

OnClick="Button1_Click"

So your buttons should be:

<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:


You need to add autopostback="true" to the buttons:

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

Otherwise the code behind will not be triggered.



来源:https://stackoverflow.com/questions/12493298/button-clickevent-is-not-triggered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!