Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

asp.net - Button ClickEvent is not triggered

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...