2012-09-19 98 views
-1

下午所有,按鈕ClickEvent不會被觸發

我有我的網頁上的兩個按鈕,用於鎖定和解鎖一個網頁,以便用戶可以鎖定頁面,編輯此沒有其他用戶能夠訪問記錄,然後解鎖此記錄,以便其他用戶可以編輯此記錄。

我的問題是,按鈕不工作,我不知道爲什麼。我正在使用圖像按鈕,但它看起來像事件沒有被觸發,我不能看到問題,它讓我瘋狂。有人可以看看我的代碼...

<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 

任何幫助很多appriechiated。我一直在尋找這一點,並且似乎無法找到問題。

問候 貝蒂

+1

考慮重新標題你的問題。它根本沒有意義。 –

回答

0

您還沒有訂閱點擊事件。您的控件不知道用戶點擊它們時必須調用這些函數。

訂閱這些事件如下:

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

您需要在您的按鈕指定的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 /> 
0

您需要的AutoPostBack = 「真」 添加到按鈕:

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

否則後面的代碼不會被觸發。

+0

之後,OP需要指定OnClick =「MethodName」,沒有事件偵聽器誰會迴應事件? – 2012-09-19 10:59:57

+1

@DarshanJoshi ASP.net將捕獲sender As Object中的事件,e As System.EventArgs。如果使用回發,則不需要在按鈕上指定每個事件。 –

+0

還意味着您可以在後端代碼中添加事件,而無需每次都更改前端代碼(如果使用回發)。 –