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

时间:2015-05-29 08:15:11

标签: asp.net vb.net linkbutton

以下是我要显示的目录的代码。我正确地从数据库中获取内容以显示在网页上。我已经将关于从数据库中提取的内容的链接按钮添加到表中。我不知道为什么,linkbutton不会触发添加到它的事件。谁能告诉我为什么以及导致事件不被解雇的原因?

ASPX:

<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

2 个答案:

答案 0 :(得分:0)

好的,在回答时,我的代码中没有LinkButton可见。我会假设它在UpdatePanel之外,因此您需要将AsyncPostBackTrigger添加到UpdatePanel

对于像这样的链接按钮:

<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:             ” ...             Dim editBtn As New LinkBut​​ton             editBtn.Text =“编辑”             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)

答案 1 :(得分:0)

除去

    If Not IsPostBack Then

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