2012-09-19 43 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

还意味着您可以在后端代码中添加事件,而无需每次都更改前端代码(如果使用回发)。 –