11

I have followed some of the instructions to create a new LinkButton at runtime with an event trigger and add an event handler for the LinkButton generated. I have no idea why the new LinkButton does not trigger the event added to it. Anyone please guide/correct me.

This is what I have right now.

ASPX:

<form id="myForm" runat="server">
 <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

 <asp:UpdatePanel ID="UpdatePanel1" runat="server">

 <ContentTemplate>

 <table class="mel-table" style="text-align: center" runat="server" id="mytab1">

    <tbody>
        <tr>
            <td>Case Number :</td>

            <td>
            <asp:TextBox ID="caseNumber" runat="server"></asp:TextBox>
            </td>

        </tr>

    </tbody>
  </table>

   <asp:Button OnClick="btnOK1_Click" ID="btnOK1" runat="server" Text="Save" />

  </ContentTemplate>

  </asp:UpdatePanel>
 </form>

ASPX.VB:

Protected Overloads Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Session("tablecontent") IsNot Nothing Then
        mytab1 = CType(Session("tablecontent"), HtmlTable)
    End If
End Sub

Public Function checking() As Boolean

        Dim caseNo As Double
            Try
                caseNo = Convert.ToDouble((caseNumber.Text).Trim())
            Catch ex As Exception
            caseNo = 0
            End Try

        Dim r As New HtmlTableRow
        Dim c0 As New HtmlTableCell
        r.Cells.Add(c0)

        Dim but As New LinkButton
        but.ID = caseNo 
        but.Text = caseNo.ToString() 
        AddHandler but.Click, AddressOf LinkButton1_Click
        c0.Controls.Add(but)

        mytab1.Rows.Add(r)
        Session("tablecontent") = mytab1
        Return True
End Function

Protected Sub LinkButton1_Click(sender As Object, e As EventArgs)

    'My Code here

End Sub

Protected Sub btnOK1_Click(sender As Object, e As EventArgs)

    If (checking()) Then

        ScriptManager.RegisterStartupScript(Page, Page.GetType, "Alert", "<script type='text/javascript'>alert('Success');</script>", False)

    End If

End Sub
Tasos K.
  • 7,669
  • 7
  • 38
  • 57
Giri Dharan
  • 161
  • 4
  • 27
  • Why `Dim caseNo As Long` when you assign it a `Double`? My guess is it has to do with it being in an `UpdatePanel`. – OneFineDay Apr 30 '15 at 02:29
  • I have tried changing the type of caseNo as Double and also it didn't work. – Giri Dharan Apr 30 '15 at 02:36
  • I did not say that would fix the problem. Just wondering why the type was different than what you were setting it to? It should be a `Double` if that is the type you need it to be. – OneFineDay Apr 30 '15 at 02:43
  • On `Postback` that control and delegate do not exist. – OneFineDay Apr 30 '15 at 02:44
  • I did mistake in that part. But Could you help me how to overcome this? – Giri Dharan Apr 30 '15 at 02:47
  • In the Page Load event you have to have a medium where you can load them, be it a Database, Session variable, etc... and load them to the page. That way it can still exist. – OneFineDay Apr 30 '15 at 03:24
  • make sure checking() is called on every post back otherwise dynamic control will not available after postback on screen – Sandeep Apr 30 '15 at 05:19
  • All the elements are inside the UpdatePanel. So, there will be no postback for the whole page. Postback occurs only within the panel. When I click on save button, I get the new button added to the table. If I try to click on that button, the panel gets refreshed and the button disappears. How to handle this? – Giri Dharan Apr 30 '15 at 05:26
  • @GiriDharan try to add `UpdatePanel1.ContentTemplateContainer.Controls.Add(mytab1);` at last in Checking() function – Sandeep Apr 30 '15 at 06:07
  • @Sandeep: Doing that, will add the table with the new content. My query is to trigger the linkbutton_click event from the newly added button. I tried Doing that which also doesn't help. – Giri Dharan Apr 30 '15 at 06:13
  • checking() function doesn't return a value, fix this first. – Top Systems May 04 '15 at 02:19
  • I have updated the checking() to return a value. – Giri Dharan May 04 '15 at 03:26

2 Answers2

4

When you click on the "Save" button, save the data in either local storage or database and try to load the page again to fill the page with the content from the storage. In this case, you can have the content on the page and so, you can utilize the events also.

Giri Dharan
  • 161
  • 4
  • 27
1

Whenever you create dynamic control you have to reload it on postback. I am using method call ReloadRows() for reloading added case which i am saving in ViewState:

        <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional" >
            <ContentTemplate>
                <asp:Table class="mel-table" style="text-align: center" runat="server" id="mytab1">
                    <asp:TableRow>
                        <asp:TableCell>Case Number :</asp:TableCell>
                        <asp:TableCell>
                            <asp:TextBox ID="caseNumber" runat="server"></asp:TextBox>
                        </asp:TableCell>
                    </asp:TableRow>
                </asp:Table>
                <asp:Button OnClick="btnOK1_Click" ID="btnOK1" runat="server" Text="Save" />
            </ContentTemplate>
        </asp:UpdatePanel>

--------------------------------------------------------------------------------------------------------

Public Partial Class _Default
    Inherits System.Web.UI.Page

    Private listCaseNo As List(Of Double) = Nothing

    Protected Sub Page_Init(sender As Object, e As EventArgs)

    End Sub


    Protected Sub Page_Load(sender As Object, e As EventArgs)
        If ViewState("ListCases") IsNot Nothing Then
            listCaseNo = DirectCast(ViewState("ListCases"), List(Of Double))
        Else
            listCaseNo = New List(Of Double)()
        End If

        ReloadRows()
    End Sub

    Public Function checking() As Boolean

        Dim caseNo As Double = 0
        Try
            caseNo = Convert.ToDouble((caseNumber.Text).Trim())
        Catch ex As Exception
            caseNo = 0
        End Try

        Dim r As New TableRow()
        Dim c0 As New TableCell()
        r.Cells.Add(c0)

        Dim but As New LinkButton()
        but.ID = Convert.ToString(caseNo)
        but.Text = caseNo.ToString()
        AddHandler but.Click, AddressOf LinkButton1_Click
        c0.Controls.Add(but)

        mytab1.Rows.Add(r)

        listCaseNo.Add(caseNo)

        ViewState("ListCases") = listCaseNo

        Return True
    End Function


    Private Sub ReloadRows()
        For Each objCase As var In listCaseNo
            Dim r As New TableRow()
            Dim c0 As New TableCell()
            r.Cells.Add(c0)

            Dim but As New LinkButton()
            but.ID = Convert.ToString(objCase)
            but.Text = objCase.ToString()
            AddHandler but.Click, AddressOf LinkButton1_Click
            c0.Controls.Add(but)

            mytab1.Rows.Add(r)
        Next
    End Sub

    Protected Sub LinkButton1_Click(sender As Object, e As EventArgs)
        'My Code here

    End Sub


    Protected Sub btnOK1_Click(sender As Object, e As EventArgs)


        If (checking()) Then
        End If

    End Sub

End Class
Sandeep
  • 1,113
  • 3
  • 11
  • 24
  • For Each objCase As var In listCaseNo what do you mean by 'var' here? and also the "ReloadRows()" method adds a row to the table whenever the panel gets refreshed. Panel may get refreshed with the event from other elements also. – Giri Dharan Apr 30 '15 at 07:37
  • @GiriDharan you can use double instead of var. I have done this code in c# and then converted to vb. – Sandeep Apr 30 '15 at 07:44