VB.Net 中的 UpdatePanel 内未触发 Linkbutton onclick 事件

Linkbutton onclick event not firing inside UpdatePanel in VB.Net

下面是我要显示的 table 内容的代码。我从数据库中正确获取内容以显示在网页上。我已经将关于从数据库中获取的内容的链接按钮添加到 table。我不知道为什么,链接按钮不会触发添加到它的事件。谁能告诉我事件没有触发的原因和原因?

A​​SPX:

<form id="myForm" runat="server">
<asp:ScriptManager runat="server" ID="spm1" />
<asp:UpdatePanel runat="server" ID="up1">
<ContentTemplate>
<table runat="server" id="doctorList" class="mel-table" style="text-    align:center">
<tr>
<td class="mel-table-header">Doctor Code</td>
<td class="mel-table-header">First Name</td>
<td class="mel-table-header">Last Name</td>
<td class="mel-table-header">Is Active?</td>
<td class="mel-table-header">Update</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</form>

ASPX.VB:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim content As String = ""

    If Not IsPostBack Then

        Dim docBOs As List(Of BO_Doctors) = docList.getDoctors()

        // This brings the Doctors List from Database properly

        For Each docB As BO_Doctors In docBOs
            Dim tr As New HtmlTableRow
            Dim td1 As New HtmlTableCell
            td1.InnerText = docB.docCode
            tr.Cells.Add(td1)
            Dim td2 As New HtmlTableCell
            td2.InnerText = docB.docfName
            tr.Cells.Add(td2)
            Dim td3 As New HtmlTableCell
            td3.InnerText = docB.doclName
            tr.Cells.Add(td3)
            Dim td4 As New HtmlTableCell
            td4.InnerText = docB.isActive
            tr.Cells.Add(td4)
            Dim td5 As New HtmlTableCell
            Dim editBtn As New LinkButton
            editBtn.Text = "Edit"
            editBtn.ID = docB.docCode

            AddHandler editBtn.Click, AddressOf editBtn_Click
            td5.Controls.Add(editBtn)
            tr.Cells.Add(td5)
            doctorList.Rows.Add(tr)
        Next
    End If
End Sub

Protected Sub editBtn_Click(sender As Object, e As EventArgs)
    Dim btn As LinkButton = CType(sender, LinkButton)
    Dim doc As BO_Doctors = docList.getaDoctor(btn.ID)

End Sub

好的,在回答时,我在您的代码中看不到 LinkButton。我假设它在 UpdatePanel 之外,所以您需要将它作为 AsyncPostBackTrigger 添加到您的 UpdatePanel:

对于这样的 link 按钮:

<asp:LinkButton ID="editBtn" runat="server" OnClick="editBtn_Click">Edit</asp:LinkButton>

需要在 UpdatePanel 中声明为 AsyncPostBackTrigger 才能更新 UpdatePanel:

<asp:UpdatePanel ID="up1" runat="server">
      <Triggers>
           <asp:AsyncPostBackTrigger ControlID="editBtn" EventName="Click"/>
      </Triggers>
      <ContentTemplate>
      '....
      </ContentTemplate>
</asp:UpdatePanel>

为了在运行时添加 AsyncPostBackTrigger: '... 将 editBtn 调暗为新的 LinkBut​​ton editBtn.Text = "Edit" editBtn.ID = docB.docCode

        AddHandler editBtn.Click, AddressOf editBtn_Click
        td5.Controls.Add(editBtn)
        tr.Cells.Add(td5)
        doctorList.Rows.Add(tr)


        Dim AsyncPBTrigger As New AsyncPostBackTrigger
        AsyncPBTrigger.ControlID = editBtn.ID
        AsyncPBTrigger.EventName = "Click"

       up1.Triggers.Add(AsyncPBTrigger)

删除

    If Not IsPostBack Then

条件,然后它会起作用。这是因为,由于内容在更新面板内,因此不会加载页面。这种情况使得页面进入到没有添加内容的初始阶段。