0

Below is my code for the table of contents to display. I get the content from the database properly to display on the web page. I have added the linkbuttons with respect to the content fetched from the database to the table. I don't know why, the linkbutton does not fire the event added to it. Could anyone tell me why and what causes the event not to fire?

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
DGibbs
  • 14,316
  • 7
  • 44
  • 83
Giri Dharan
  • 161
  • 4
  • 28

2 Answers2

0

Alright, at the time of answering there is no LinkButton visible to me at your code. I'll assume that it is outside the UpdatePanel, so you need to add it as an AsyncPostBackTrigger to your UpdatePanel:

For a link button like this:

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

Needs to be declared in the UpdatePanel as an AsyncPostBackTrigger for it to be able to update the UpdatePanel:

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

in order to add an AsyncPostBackTrigger at runtime : '... 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)


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

       up1.Triggers.Add(AsyncPBTrigger)
wooer
  • 1,677
  • 2
  • 16
  • 27
  • This one doesn't help. Any other way to do it? – Giri Dharan May 29 '15 at 09:11
  • I'm not getting any errors. But the content added at runtime goes off when I click the linkbutton generated. – Giri Dharan May 29 '15 at 09:18
  • My answer aimed to trigger UpdatePanel from outside, there are possible issues for dynamically created controls inside update panel. Please take a look at this answer: http://stackoverflow.com/a/4088174/306921 – wooer May 29 '15 at 12:13
0

remove

    If Not IsPostBack Then

condition and then it will work. This is because, the page will not be loaded back as the content is within the update panel. This condition makes the page to get to the initial stage without the content added.

Giri Dharan
  • 161
  • 4
  • 28